Install the nuget package AssociativeCache.4.0.0.nupkg
.
Initialise a single instance of the cache manager using with default parameters.
var cacheManager = new InMemoryCache<string, string>();
cacheManager.Add("key", "value");
var cacheValue = cacheManager.TryGetValue("key");
Current implementations include:
- First In First Out - FifoEvictionPolicy (default)
- Most Recently Used - MruEvictionPolicy
- Least Recently Used - LruEvictionPolicy
var evictionPolicy = new MruEvictionPolicy<TKey,TValue>();
var cacheManager = new InMemoryCache<string, string>(evictionPolicy);
Create a new class that derives from the interface ICacheEvictionPolicy<TKey,TValue>
.
public class CustomEvictionPolicy<TKey,TValue> : ICacheEvictionPolicy<TKey,TValue>
{
public TKey EvictItem()
{
//return key of item to evict
}
public void OnItemAdded(TKey key, TValue value)
{
//custom logic
}
public void OnItemAccessed(TKey key, TValue value)
{
//custom logic
}
public void OnItemRemoved(TKey key)
{
//custom logic
}
}