The Sitecore.CacheExtensions library is a series of extensions to provide configuration based caches, based on the helix design principles.
Using nuget: Install-Package Sitecore.CacheExtensions
The following classes are automatically injected into the Sitecore OTB Service Collection:
- SitecoreCacheManager (ICacheManager) - Adds layer to expose configured caches
- TransientCache (BaseTransientCache) - Adds caching per request within the HttpContext.Items
- SessionCache (BaseSessionCache) - Adds caching per session within the HttpContext.Session
To grab a reference to either the SitecoreCacheManager, TransientCache, or SessionCache, add a class that extends the IServicesConfigurator and add your classes that you would like to be resolved.
public class RegisterDependencies : IServicesConfigurator
{
public void Configure(IServiceCollection serviceCollection)
{
// Add your classes here using the serviceCollection methods
}
}
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<services>
<configurator type="[Class], [Assembly Name]" />
</services>
</sitecore>
</configuration>
For more information, see: http://kamsar.net/index.php/2016/08/Dependency-Injection-in-Sitecore-8-2/
Add a new cache by adding a path configuration like so:
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<caches>
<cache name="example-cache" maxSize="100MB" lifespan="60" expirationType="sliding" />
</caches>
</sitecore>
</configuration>
All fields are required
Must be unique and is used to identify the cache with the Cache Manager
Max size of the cache in string representation
Examples:
- 100K
- 10MB
- 1GB
Lifetime of a cache entry, read in the number of seconds.
Examples:
- 60 = 1 minute
- 360 = 6 minutes
- 3600 = 1 hour
Determines how the cache entry should expire
Supported Expiration Types:
- Sliding - expires entry if it hasn't been accessed within the lifespan
- Absolute - expires entry after a set amount of time
- Sticky (not implemented)