HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog 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 to control how an object is managed by the cache. The CacheEvictionPolicy takes a TimeSpan value indicating how long the cache entry should be kept, the type of timeout that should be used and optionally a set of keys that refers to other cache keys that this entry should be dependent on. If a dependent key is not present in the cache, the new entry will be immidiately evicted. Note that the CacheEvictionPolicy is immutable so you can re-use existing instances of the class.
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
  _objectInstanceCache.Insert(key, val, CacheEvictionPolicy.Empty);

  // To enable sliding expiration for 10s, depending on "otherKey"
  _objectInstanceCache.Insert(key, val, 
    new CacheEvictionPolicy(
      cacheKeys: new [] { "otherKey" }

Master keys

While the normal cache dependency keys in a CacheEvictionPolicy creates a dependency to existing items in the cache, a master dependency is used to group a set of cache entries. When a master key is provided, the cache will ensure that an entry with this key exist in the cache, or insert a dummy object with the provided key into the cache with an infinite timeout. This allows the whole group of entries to be removed at once. Make sure that you never use the key for another entry as a master key as this may interfere with the functionality of that item.

The following example shows how to insert an object with a master key and how to clear all items with a master key dependency.

public void AddToCacheWithMasterDependency(string key, object val, string masterKey)
  // To enable absolute expiration for 5 minutes, 
  // depending on "otherKey" and master key dependency
  _objectInstanceCache.Insert(key, val, 
    new CacheEvictionPolicy(
      cacheKeys: new [] { "otherKey" },
      masterKeys: new [] { masterKey }

public void RemoveFromCacheWithMasterDependency(string masterKey)

What’s Next
Did this page help you?