// *********************************************************************** // Assembly : OpenAuth.WebApi // Author : yubaolee // Created : 07-11-2016 // // Last Modified By : yubaolee // Last Modified On : 07-11-2016 // Contact : // File: CacheObjService.cs // *********************************************************************** using System; namespace Helper.Cache { /// /// 带超时结构的缓存 /// public class ObjCacheProvider : CacheProvider { public ObjCacheProvider() { SetCacheInstance(new HttpApplicationContext()); } public bool Create(string key, T val) { var cacheobj = new CacheObj { key = key, InvalidTime = DateTime.Now.AddMinutes(5), CreateTime = DateTime.Now, Obj = val }; //设置缓存 return CacheContext.Set(key, cacheobj); } /// /// 根据失效时间获取缓存 /// 李玉宝于2016-11-08 16:54:04 /// /// The key. public T GetCache(string key) { var cache = CacheContext.Get>(key); if (cache == null) return default(T); if (cache.InvalidTime > DateTime.Now) { return cache.Obj; } //移除无效Session缓存 Remove(key); return default(T); } public void Remove(string key) { CacheContext.Remove(key); } } }