HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev 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. Use the same 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 previously used the CacheManager class, take a dependency on ISynchronizedObjectInstanceCache instead. The new interface provides the same functionality. IObjectInstanceCache supersedes IRuntimeCache by supporting different cache implementations through abstraction of the CacheDependency class.

CacheEvictionPolicy

Use CacheEvictionPolicy to control cache duration, timeout type, and dependencies for cached objects.

The CacheEvictionPolicy class takes a TimeSpan value to indicate how long to keep the cache entry, the type of timeout to use, and optionally a set of keys that refer to other cache keys that the entry depends on. The entry is immediately evicted if a dependent key is not in the cache. CacheEvictionPolicy is immutable, so 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

Use master keys to group a set of cache entries for bulk removal.

Normal cache dependency keys in a CacheEvictionPolicy create a dependency to existing items in the cache. A master dependency groups a set of cache entries. When a master key is provided, the cache ensures that an entry with this key exists, or inserts a dummy object with the provided key and an infinite timeout. This lets the whole group of entries be removed at once. Never use the key for another entry as a master key, because doing so interferes 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);
}