using System; using Microsoft.Extensions.Caching.Memory; namespace Common.Helper.Cache { /// /// 缓存框架 /// public class CacheHelper { private readonly IMemoryCache _memoryCache; /// /// 缓存框架 /// /// public CacheHelper(IMemoryCache memoryCache) { _memoryCache = memoryCache; } /// /// 存缓存 /// /// 关键词 /// 值 public void Set(string key, string value) { var cacheOptions = new MemoryCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromMinutes(5)); _memoryCache.Set(key, value,cacheOptions); } /// /// 存缓存 带时效 /// /// 关键词 /// 值 /// 时效 public void SetAging(string key, string value,int aging) { var cacheOptions = new MemoryCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromMinutes(aging)); _memoryCache.Set(key, value, cacheOptions); } /// /// 获取缓存值 /// /// /// public string Get(string key) { return _memoryCache.TryGetValue(key, out string value) ? value:""; } } }