HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In

Object caching

This topic describes object caching in Optimizely, and how to define and configure cache information.

How it works

The IObjectInstanceCache and ISynchronizedObjectInstanceCache interfaces expose the built-in cache system with custom dependency and eviction policies. Optimizely CMS and Commerce use these interfaces to cache both content and system data, and you can use the interfaces to cache custom data. The ISynchronizedObjectInstanceCache interface synchronizes cache removal among servers in a load-balanced environment using the Event management system.



If you were using the CacheManager class in CMS, you should take a dependency on ISynchronizedObjectInstanceCache instead, because the new interface provides the same functionality. The IObjectInstanceCache supersedes IRuntimeCache (that was previously used only by CMS) because it supports different cache implementations by abstracting the CacheDependency class and other things.


Use the CacheEvictionPolicy class for object caching, because it is de-coupled from any specific cache implementation. CacheEvictionPolicy is immutable so you can re-use existing instances of the class. (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.) The following example shows how to use CacheEvictionPolicy.

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

Master keys

When you need to define a common dependency for cached data, create a master dependency (or master key) with CacheEvictionPolicy, which automatically adds a list of key names to the cache as regular cache entries. By doing so, you do not need to add code to conditionally add these entries before you insert specific data into the cache.

In the following example, the difference between the cache key dependency and the master key dependency is that if there is no otherKey in the cache then the data is not inserted. If there is no entry with MasterKey, then it adds a dummy value with the given key, thus allowing the data to be inserted successfully.

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 TimeSpan(0, 5, 0),        // Always a timespan for timeouts
          new string[] { "otherKey" },  // Cache key dependency
          new string[] { "MasterKey" }  // Master key dependency

What’s Next
Did this page help you?