Link Search Menu Expand Document

CacheItem


This class represents an item in the ICache object. It implements the IExpirable interface.

Properties

NameDescription
KeyThe cache key.
ValueThe cached value (generic type).
CacheItemExpirationThe cache expiration in minutes.

Creating New Instance

var expirationInMinutes = 180;
var item = new CacheItem("Key", "Value", expirationInMinutes);

Passing to the Cache

The ICache object is typically embedded in a repository. The following example manually adds an item to the cache of a PersonRepository.

var people = GetPeople();
var expirationInMinutes = 180;
var item = new CacheItem<IEnumerable<Person>>("CacheKey:ActivePeople", people, expirationInMinutes);

using (var repository = new PersonRepository())
{
    repository.Cache.Add(item);
}

The cache is set automatically when a cacheKey is passed to the Query or BatchQuery operations.

Retrieving from the Cache

Access the ICache object directly and call Get() with the key.

var item = repository.Cache.Get<IEnumerable<Person>>("CacheKey:ActivePeople");

If no cache entry exists for the given key, an exception is thrown by default. Set the throwException argument to false to suppress this.