HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Cache objects

Describes object caching in Optimizely, and how to define and configure cache information.

The IObjectInstanceCache and ISynchronizedObjectInstanceCache interfaces expose the built-in cache system with custom dependency and eviction policies. Optimizely Content Management System (CMS) and Optimizely Customized Commerce use these interfaces to cache 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.

📘

Note

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.

CacheEvictionPolicy

Use the CacheEvictionPolicy class to control how the cache manages an object. The CacheEvictionPolicy takes a TimeSpan value to indicate 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. The entry is immediately evicted if a dependent key is not in the cache. 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(
      TimeSpan.FromSeconds(10),
      CacheTimeoutType.Sliding,
      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 exists in the cache, or insert a dummy object with the provided key into the cache with an infinite timeout. This lets the whole group of entries be removed at once. Ensure you never use the key for another entry as a master key, which may interfere with that item's functionality.

The following example shows how to insert an object with a master key and 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(
      TimeSpan.FromMinutes(5),
      CacheTimeoutType.Absolute,
      cacheKeys: new [] {
        "otherKey"
      },
      masterKeys: new [] {
        masterKey
      }
    )
  );
}

public void RemoveFromCacheWithMasterDependency(string masterKey) {
  _objectInstanceCache.Remove(masterKey);
}