Create a custom process to read all site-wide sett...
# help-with-umbraco
o
I'm trying to create a custom process, that works similar to reading config from appsettings.json when application starts. The list of settings are stored in umbraco content tree. Let's say the setting is at UmbracoContentRoot/Site-Settings Under it, there's 3 setting item Student Book Publication When user visit a page with 'student' in the URL, the page will add a meta tag: meta name='page-type' content='student' The issue here is that I do not want to read all settings under 'UmbracoContentRoot/Site-Settings' whenever a page loads, then compare the setting with page URL and decide which page-type it belongs to. This is very bad performance. How could I read the setting items in umbraco at application level and cache it, or like put in a static class, just like reading the appsettings.json at start? And can retrieve the values whenever needed without the need to get the setting items in umbraco and loop through them for every page load? I'd really appreciate if anyone can help :)))))
s
If the values are fixed values you could either store them in a static dictionary somewhere, try get the value. If it does not contain the value, get the settings node to read the value and store it in the dictionary. Another possibility is to use roughly the same aproach, but store them in a redis store for example
s
My usual approach is to create a site settings service that reads these values and stores to the application cache. This service has a Singleton scope. It can be injected in services and even razor views (tut tut). You use it via _siteSettingsService.Settings.SiteCode etc etc I've cut down a very basic example. https://cdn.discordapp.com/attachments/1244901058027716608/1244908485934710785/sitesettingsexample.txt?ex=6656d2e0&is=66558160&hm=1712dc4e7171c084edb39c69e8455bfb5d92c9c3959f93852d7ca63e29f25b63&
k
Is "tut tut" good or bad? In Swedish it means "honk honk", which is also ambiguous.
o
@Sander L unfortunately the value can change, not fixed values @SiempreSteve thank u so much this is exactly what im looking for Thank you both this is super helpful :)))))))))
j
If the values change, you will need to add some cache invalidation logic in there with the cached "site settings" approach. Can you expand on what this means @Onasi? > When user visit a page with 'student' in the URL
i.e. are we talking about content that's already heirarchically structured? like www.example.com/student/something/something-else? If so, and you have a "student" node, then that's the place to store this setting. Umbraco will be able to look up any properties stored on an ancestor very quickly.
o
The student setting item is like a tag ( but client want to automatically tag the page) the setting item has multiple value --> so for example student tag item has value: student, course, campus-life. And any page URL that has student, course, or campus-life will be labeled as 'student' tagged page, and in search page user can filter all 'student' tagged pages. Initially we recommend just add tags to the page but client prefer this automatic labeling @Jason
In terms of performance I already implemented not the best approach ( loop through all tags when page loads and output it's page tag in meta data) so far I haven't spot any slowless yet maybe not a concern :))
j
How are the tags stored? Are they all on one node?
Bear in mind, Umbraco's content is all stored in an in-memory cache. The only performance consideration is how long it takes to look up content in the tree by looping through documents/nodes. If all your tags are stored on one node in a blocklist, you're not really going to see poor performance until you hit a lot of tags - so long as getting that node doesn't take too long in the first place.
There's never really any need to cache property data outside Umbraco's content cache. Sure, navigating to a settings node through the tree may take some time, but the best way to deal with that is to index the settings node (i.e. cache the Id, or Guid etc.) to make lookups faster rather than duplicate all your property data into another cache (that you then need to manage/invalidate yourself).
s
For Jason's main problem - I invalidate this cache item on any publish on this node / the children nodes below. Do take this into account. He's correct that you could just use Umbraco helpers and get this node. I have a very complex product catalogue, currency settings, CRM nodes etc below the site settings so having this simple service I can inject and just "get" access to my "settings" is extremely useful - though I have had to be careful not to bloat it. Ensure you never cache an umbraco node though - this setups a "view model" copy of the data in a format I need.
Tut-tut is a disapproving noise. I'm highlighting that injecting services into Razor views might not be 100% best practice (though it does work and it does solve many issues).
k
Aah. It's called "app app app" in Swedish. 🙂 I had a bug today where an async http call from cshtml caused strange and crashy behavior, so +1 on the tut-tut
s
apt apt apt 🙂
20 Views