Sitecore’s powerful cache system gives developers lots of options, but it’s not always obvious how to use or extend it. Conversely, ASP.NET’s cache system has more flexibility when it comes to caching duration and de-caching events.
Constellation.Foundation.Caching is a facade that allows you to access Sitecore or .NET caching through a single interface.
When to Use
- Cache complex objects based on Sitecore Items.
- Cache objects based on a site or database context.
- Cache objects with absolute or sliding cache timeouts.
- Cache objects with a cache removal callback to re-cache.
This component is a re-work of Christopher Giddings’ post from 2012, with updates for Sitecore 8 and 9, some inline documentation, and some streamlining of the overall API.
Why to Use
Unlike the HttpRuntime Cache, Sitecore’s caching mechanism contains multiple “cache” objects. Sometimes finding which cache you want to use or search can be confusing. Additionally, it is possible to create your own unique cache that can be managed by Sitecore’s caching subsystem. This custom cache can be sensitive to various Sitecore events such as internal cache clearing calls and publication events.
Unfortunately, getting or creating a Sitecore cache isn’t exactly straightforward. This library abstracts that process away behind four simple methods that allow you to program against Sitecore caching in a way that is very familiar if you’re a .NET Developer used to the HttpRuntime cache.
Constellation.Foundation.Caching makes use of Dependency Injection. by default, it will register ICacheManager with your Service Locator, supplying an instance of CacheManager. For testing or customization, you are free to substitute your own ICacheManager implementation.
To get an instance of ICacheManager from a Controller, include it in your Controller’s constructor:
public myController(ICacheManager cacheManager)
this.Cache = cacheManager;
To get an instance of ICacheManager outside of a Constructor, use the ServiceLocator:
var cache =(ICacheManager)ServiceLocator.ServiceProvider.GetService(typeof(ICacheManager));
To override Constellation.Foundation.Caching’s service registration, replace the service configurator referenced in /App_Config/Include/Constellation/Constellation.Foundation.Caching.config
We recommend using a patch file for the override, as the above config file is shipped with the library and will be overwritten with each update.
Return an object from the cache of the type provided. Additional parameters allow you to specify:
- The Sitecore cache or the ASP.NET HttpRuntime cache (default is Sitecore)
- Whether to search the “global” cache (default is false)
- Whether to restrict the key search to a specific Sitecore site name
- Whether to restrict the key search to a particular Sitecore database name
Add(key, cachingData, cacheTimer)
Adds an object to the cache for the specified time, which can be either a DateTime or a TimeSpan. Additional parameters allow you to specify:
- Whether to use a sliding expiration strategy
- Whether to use the Sitecore cache or the Runtime cache (default is Sitecore)
- Whether to use the “global” cache (default is false)
- Whether to remove the cached object after a Publish:End event (default is true)
- Key the cached entry to a specific Sitecore Site name
- Key the cached entry to a specific Sitecore Database name
- Supply cache dependencies (Runtime cache only)
- Specify cache item priority (Runtime cache only)
- Specify a cached object removed callback (Runtime cache only)
Removes the object from the cache immediately. Additional parameters allow you to specify:
- Whether to use the Sitecore cache or the Runtime cache
- Whether to use the “global” cache
- Search the cache based on a Sitecore Site name
- Search the cache based on a Sitecore Database name
Constellation.Foundation.Caching is managed via NuGet.
In Visual studio, fire up the Package Manager console and install into the appropriate Sitecore projects:
PM> Install-Package Constellation.Foundation.Caching
At this point you can now add ICacheManager to your components’ constructors and start using it.
Consider reviewing your existing code and replacing any calls to the HttpRuntime cache with Constellation. At a bare minimum this will give you more predictable de-caching results during publishing.
Check out the source code available on GitHub.