Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MemoryCache : IObjectCache
- {
- public T Get<T>(CacheKey key, DateTime absoluteExpiration, Func<T> load)
- {
- var policy = new System.Runtime.Caching.CacheItemPolicy();
- policy.AbsoluteExpiration = absoluteExpiration;
- return Get(key, policy, load);
- }
- public T Get<T>(CacheKey key, TimeSpan slidingExpiration, Func<T> load)
- {
- var policy = new System.Runtime.Caching.CacheItemPolicy();
- policy.SlidingExpiration = slidingExpiration;
- return Get(key, policy, load);
- }
- private T Get<T>(CacheKey key, System.Runtime.Caching.CacheItemPolicy policy, Func<T> load)
- {
- var keyStr = key.ToString();
- var cache = System.Runtime.Caching.MemoryCache.Default;
- T result = (T)cache[keyStr];
- if (result == null)
- {
- result = load();
- cache.Add(new System.Runtime.Caching.CacheItem(keyStr, result), policy);
- }
- return result;
- }
- }
Add Comment
Please, Sign In to add comment