CacheItem
This class represents an item in the ICache object. It implements the IExpirable interface.
Properties
| Name | Description |
|---|---|
| Key | The cache key. |
| Value | The cached value (generic type). |
| CacheItemExpiration | The 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
cacheKeyis 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
throwExceptionargument tofalseto suppress this.