Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely CMS
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

Cache functionality, including remote synchronization, is part of the EPiServer framework available for all EPiServer products. To allow cache management to be effectively shared between for instance CMS and Commerce. There are two interfaces, IObjectInstanceCache and ISynchronizedObjectInstanceCache, that can be used to access this functionality. Changes made using the ISynchronizedObjectInstanceCache interface are synchronized between servers using events in load balanced environments.

Note The recommendation is to take a dependency on ISynchronizedObjectInstanceCache instead if you previously were using the CacheManager class in CMS, the new interface provides the same functionality. The IObjectInstanceCache is intended to directly supersede IRuntimeCache previously only used by CMS, the new interface has been rewritten to support different cache implementations by abstracting the CacheDependency class among other things. The underlying default implementation of IObjectInstanceCache still rely on HttpRuntime.Cache.

CacheEvictionPolicy

The CacheDependency class is tightly coupled to the HttpRuntime implementation and if you want to have a custom cache implementation you cannot rely on CacheDependency. For the object caching support in EPiServer framework you have the CacheEvictionPolicy class instead. It is very similar to CacheDependency but is completely de-coupled from any specific cache implementation.

Note CacheEvictionPolicy is immutable so it is possible (and encouraged) to re-use existing instances of the class.

Example on how to use CacheEvictionPolicy

XML
public void AddToCache(string key, object val)
{
    // If you don't care about specific cache management, use CacheEvictionPolicy.Empty
    _cache.Insert(key, val, CacheEvictionPolicy.Empty);

    // To enable sliding expiration for 10s, depending on "otherKey"
    _cache.Insert(key, val, new CacheEvictionPolicy(
        new string[] { "otherKey" }, // Cache key dependency
        new TimeSpan(0, 0, 10),      // Always a timespan for timeouts
        CacheTimeoutType.Sliding);
}

Master keys

There is frequently a need to define a common dependency for cached data, frequently referred to as a master dependency or master key. To simplify the management of master keys this has been rolled into CacheEvictionPolicy – by including a list of key names these will automatically be added to the cache (if they do not exist) as regular cache entries. This means that you don't need to add code to conditionally add these entries before inserting your specific data into the cache.

In the example below the difference between the cache key dependency and the master key dependency is that if “otherKey” is not present in the cache then the data will never be inserted. If en entry with key “MasterKey” does not exist, then a dummy value will be added with the given key, thus allowing the data to be inserted successfully.

XML
public void AddToCacheWithMasterDependency(string key, object val)
{
    // To enable absolute expiration for 5 minutes, depending on "otherKey" and master key dependency to "MasterKey"
    _cache.Insert(key, val, new CacheEvictionPolicy(
        null,                         // No file dependencies
        new string[] { "otherKey" },  // Cache key dependency
        new string[] { "MasterKey" }, // Master key dependency
        new TimeSpan(0, 5, 0),        // Always a timespan for timeouts
        CacheTimeoutType.Absolute);
}

Configuring caching of files

For the EPiServer platform, it is possible to configure which cache information is added to the response headers at request of static files. This can be used so files can be cached by the client and not served by the web server at each request meaning the total request time for a client can be reduced significantly.

The setting in web.config that controls the cache header directives “Expires” and “Cache-Control” is the configuration section staticFile that is allowed to be set on location level. This means it is possible to set different expiration times depending on the path to the static file. The staticFile configuration is used by both the StaticFileHandler that delivers files from Virtual Path Providers and by the media system in CMS.

Note File delivered by IIS, for example CSS and other resources on the web site, are configuring using standard IIS configuration settings.

Example:

XML
<configuration>

<!--Configures expiration for files in CMS/VPP-->
<staticFile expirationTime="12:0:0" />

<!--Configures expiration in IIS-->
<system.webServer>
   <!--Configures client headers for static files from IIS-->
   <staticContent>
       <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00"></clientCache>
   </staticContent>
   <!--Configures output and kernel caching for ALL images (both CMS and IIS)-->
   <caching>
      <profiles>
        <add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
        <add extension=".png" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
        <add extension=".js" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
        <add extension=".css" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
        <add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
        <add extension=".jpeg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
      </profiles>
   </caching>
</system.webServer>
</configuration>
Do you find this information helpful? Please log in to provide feedback.

Last updated: Jul 09, 2014

Recommended reading