Created
April 25, 2016 16:24
-
-
Save naepalm/d735a9a929b9a28d429d0d0ed213d7b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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