using Microsoft.Extensions.Caching.Memory; namespace JNPF.Extras.CollectiveOAuth.Cache; /// /// http运行时缓存. /// public class HttpRuntimeCache { /// /// 缓存. /// public static IMemoryCache memoryCache = new MemoryCache(new MemoryCacheOptions() { }); /// /// 获取数据缓存. /// /// 键. public static object Get(string CacheKey) { memoryCache.TryGetValue(CacheKey, out object result); return result; } /// /// 设置数据缓存 /// 变化时间过期(平滑过期)。表示缓存连续2个小时没有访问就过期(TimeSpan.FromSeconds(7200)). /// /// 键. /// 值. /// 过期时间,默认7200秒. /// 是否相对过期,默认是;否,则固定时间过期. public static void Set(string CacheKey, object objObject, long Second = 7200, bool Sliding = true) => memoryCache.Set(CacheKey, objObject, Sliding ? new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(Second)) : new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(Second))); /// /// 移除指定数据缓存. /// /// 键. public static void Remove(string CacheKey) => memoryCache.Remove(CacheKey); /// /// 移除全部缓存. /// public static void RemoveAll() => memoryCache.Dispose(); }