using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Caching; using System.Web; namespace Central.Control.WebApi.Cache { /// /// /// public class CacheHelper: ICacheHelper { private MemoryCache _cache = MemoryCache.Default; /// /// 此方法缓存不会过期,请谨慎使用 /// /// /// public void SetByNotExpired(string key, T content) { var policy = new CacheItemPolicy { AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration }; _cache.Set(key, content, policy); } /// /// /// /// /// /// public void Set(string key, T content, int seconds = 300) { var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddSeconds(seconds) }; policy.AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration; // System.Runtime.Caching.ObjectCache.InfiniteAbsoluteExpiration _cache.Set(key, content, policy); } /// /// /// /// /// /// public T Get(string key) where T : class { if (_cache.Contains(key)) { return _cache[key] as T; } return null; } /// /// /// /// public void Remove(string key) { _cache.Remove(key); } } }