The Agility Dependency Cache is designed to help developers manage content caching in conjunction with Agility content. This means you can cache whatever you'd like and set a dependency on specific Agility content. When the Agility content has been updated, then your cache will expire automatically and you can re-hydrate your cache.
An additional feature of the AgilityDependencyCache is that the content will be cached in the HttpContext.Cache only if the website is in βLiveβ mode. If the website is in development or preview mode the content will be cached in the HttpContent.Items.
The code snippet below illustrates how you can wrap a data-access call in an AgilityDependency Cache so that your content is cached while the associated Agility content is cached, and when the Agility content is updated, so will your custom cache key.
public DataView GetData() { // Check the cache to see if we already have an item added string cacheKey = "some string" object someData = Agility.Web.Utils.AgilityDependencyCache.Select(cacheKey); if (someData == null) { // Nothing found in the cache someData = GetSomeData(); //the content you want to cache // Add the data into the cache ready for next time we want to access it and base it on an Agility content list (by reference name) Agility.Web.Utils.AgilityDependencyCache.Insert(cacheKey, someData , new List { "SomeAgilityContentReferenceName"}, DateTime.Now.AddHours(1)); } return someData; }
Comments
Would it pose issues or be redundant to cache AgilityContentRepository items in Agility's dependency cache?
Great question! When you are calling the AgilityContentRepository, a cache dependency is already registered for that. So in most cases, this is redundant. However, if you are doing complex filtering or sorting on a list, it can be beneficial to cache those results and put a dependency on the associated content list.
Hi! Another question, can the Agility Dependency Cache be tied to the publishing of specific modules? Where you could pass the module content definition name to: AgilityDependencyCache.Insert(cacheKey, someData , new List { "ModuleReferenceName"}, DateTime.Now.AddHours(1));?
Or does this only work with syncing content with Shared Content lists? Additionally, could you have this cache sync up to page publishing?
Thanks!
Yes, it can. It works with a module reference name or a content reference name. The way it works is when that item is updated in Agility CMS (i.e. published) then any cache you have that is based on that reference name is cleared.
It's also important to note that everything you call in the AgilityContentRepository is already cached - so its only worth adding this extra caching layer if you are doing some complex filtering in linq or joining it with other data.
I can't really think of case where you would want to cache a module or page though, mainly caching is for when working with large content lists. Could you elaborate a little on your use case?
Hey James, that is great to know!
The use case would be to refresh some other kind of cached data unrelated to Agility content when a particular module is updated and published.
Oh, interesting... yes that should work then. You can cache something and then base it on the module reference name. Then, when that module is published, your cache will be cleared π
Does the AgilityDependencyCache still exist in your .net core sdk?
Please sign in to leave a comment.