using System; using System.Collections.Generic; using System.Runtime.Caching; namespace Infrastructure.Cache { public class RuntimeMemoryCache : ICache { private readonly MemoryCache memoryCache = MemoryCache.Default; /// /// 加入缓存项(绝对过期时间) /// /// 缓存项标识 /// 缓存项 /// 缓存失效时间 public void Add(string key, object value, TimeSpan timeSpan) { if (!string.IsNullOrEmpty(key) && (value != null)) { CacheItemPolicy cip = new CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.Add(timeSpan) }; this.memoryCache.Add(key, value, cip, null); } } /// /// 加入依赖物理文件的缓存项 /// /// 缓存项标识 /// 缓存项 /// 依赖的文件全路径 public void AddWithFileDependency(string key, object value, string fullFileNameOfFileDependency) { if (!string.IsNullOrEmpty(key) && (value != null)) { CacheItemPolicy policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMonths(1) }; policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List { fullFileNameOfFileDependency })); this.memoryCache.Add(key, value, policy, null); } } public object Get(string cacheKey) { return this.memoryCache[cacheKey]; } public T Get(string cacheKey) where T : class { object obj = this.Get(cacheKey); if (obj != null) { return (obj as T); } return default(T); } public void Remove(string cacheKey) { this.memoryCache.Remove(cacheKey, null); } /// /// 如果不存在缓存项则添加,否则更新(相对过期) /// /// 缓存项标识 /// 缓存项 /// 缓存失效时间 public void Set(string key, object value, TimeSpan timeSpan) { CacheItemPolicy cip = new CacheItemPolicy() { SlidingExpiration = timeSpan, }; this.memoryCache.Set(key, value, cip, null); } /// /// 设置绝对过期时间 /// /// 缓存项标识 /// 缓存项 /// 缓存失效时间 public void SetAbsoluteExpiration(string key, object value, TimeSpan timeSpan) { CacheItemPolicy cip = new CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.Add(timeSpan), }; this.memoryCache.Set(key, value, cip, null); } } }