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

Cache objects

Describes object caching in Optimizely Content Management System (CMS), 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 for object caching because it is de-coupled from any specific cache implementation. CacheEvictionPolicy is immutable so that you can reuse 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
        CacheTimeoutType.Sliding);
    }

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 entries. Doing so means you do not need to add code to conditionally add these entries before inserting 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, the data is not inserted. If there is no entry with MasterKey, then it adds a dummy value with the given key, thus letting you insert the data.

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);
    }