Skip to content

Instantly share code, notes, and snippets.

@naepalm
Created April 25, 2016 16:24
Show Gist options
  • Save naepalm/d735a9a929b9a28d429d0d0ed213d7b5 to your computer and use it in GitHub Desktop.
Save naepalm/d735a9a929b9a28d429d0d0ed213d7b5 to your computer and use it in GitHub Desktop.
public class CacheService : ICacheService
{
private readonly MemoryCache _memoryCache = new MemoryCache("Cache.Key");
public void Add<T>(T cacheObject, string key)
{
var cacheItemPolicy = new CacheItemPolicy
{
AbsoluteExpiration = DateTimeOffset.Now.AddHours(24)
};
_memoryCache.Add(new CacheItem(key, cacheObject), cacheItemPolicy);
}
public void Remove(string key)
{
_memoryCache.Remove(key);
}
public T Get<T>(string key)
{
var cacheItem = _memoryCache.GetCacheItem(key);
return (T)cacheItem.Value;
}
public bool IsCached(string key)
{
var isCached = _memoryCache.Contains(key);
return isCached;
}
}
public interface ICacheService
{
void Add<T>(T cacheObject, string key);
void Remove(string key);
T Get<T>(string key);
bool IsCached(string key);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment