Reference Commands

The following section details the SaintModeCache members and behaviour.

Constructors

Create with defaults which has an infinite expires policy when not overridden.

var cache = new SaintModeCache(); 

Create with given expires policy in seconds. Used when not overridden.

var cache = new SaintModeCache(60); 

Create with custom ObjectCache for storing values.

var cache = new SaintModeCache(new MemoryCache("MyCache", null)); 

Create with custom ObjectCache for storing values and a default expires policy in seconds.

var cache = new SaintModeCache(new MemoryCache("MyCache", null), 60); 

GetOrCreate

Get cache value if exists or block for single thread to create cache item. Uses the default expires policy from constructor if set, or default policy defined by constructor.

cache.GetOrCreate("MyKey", (key, cancelToken) => 
        string.Concat("Value for key: ", key)); 

As above but overrides default expires policy with a custom timeout in seconds.

var expiresPolicy = DateTime.UtcNow.AddSeconds(20); 
cache.GetOrCreate("MyKey", (key, cancelToken) => 
        string.Concat("Value for key: ", key), 
        expiresPolicy); 

As above but uses CacheItemPolicy instance.

var expiresPolicy = new CacheItemPolicy {  
    AbsoluteExpiration = DateTime.UtcNow.AddSeconds(20) }; 
cache.GetOrCreate("MyKey", (key, cancelToken) => 
        string.Concat("Value for key: ", key), 
        expiresPolicy); 

Stale

A cache item is stale when it has expired, but has not yet fetched an updated value to replace it. Once stale, a background fetch is only triggered by a call to GetOrCreate.

var isStale = cache.Stale("MyKey", new object()); 

Expired

A cache item has expired when it's cache policy has passed. However the item may still be in cache in a stale state.

var hasExpired = cache.Expired("MyKey"); 

TryGet

Try to get an existing item from the cache if it exists. Saint mode is not available via this interface, therefore it will return false if no item exists.

if (cache.TryGet("MyKey", out result)) { 
    // do something with result 
} 

As above, but includes a value to include whether the data is stale.

if (cache.TryGet("MyKey", out result, out stale)) { 
    // do something with result 
} 

SetOrUpdateWithoutCreate

Set or update an item in the cache directly. This will update an existing item and change it's expires policy to the default defined by the constructor.

cache.SetOrUpdateWithoutCreate("MyKey", new object()); 

Set or update an item in the cache directly and define a new expires policy. This will update an existing item and change it's expires policy to a timeout in seconds.

var expiresPolicy = DateTime.UtcNow.AddSeconds(60); 
cache.SetOrUpdateWithoutCreate("MyKey", new object(), expiresPolicy); 

Set or update an item in the cache directly and define a new expires policy. This will update an existing item and change the cache policy to the one provided.

var expiresPolicy = new CacheItemPolicy {  
    AbsoluteExpiration = DateTime.UtcNow.AddSeconds(20)}; 
cache.SetOrUpdateWithoutCreate("MyKey", new object(), expiresPolicy); 

Set or update an item in the cache directly using a CacheItem instance. The default expires policy defined by the constructor is used.

var cacheItem = new CacheItem("MyKey", new object());
cache.SetOrUpdateWithoutCreate(cacheItem); 

Set or update an item in the cache directly using a CacheItem instance. This will update an existing item and change it's expires policy to a timeout in seconds.

var cacheItem = new CacheItem("MyKey", new object());
var expiresPolicy = new CacheItemPolicy {  
    AbsoluteExpiration = DateTime.UtcNow.AddSeconds(20)}; 
cache.SetOrUpdateWithoutCreate(cacheItem, expiresPolicy); 

GetWithoutCreateOrNull

Get an existing item from the cache if it exists or return null. SaintMode is not available via this interface, but it can be used to access existing values where they exist.

var cacheValue = cache.GetWithoutCreateOrNull("MyKey"); 

Remove

Explicitly remove an item from the cache and return the last value.

var removedValue = cache.Remove("MyKey");