using Microsoft.Extensions.Caching.Memory; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DataCapture_MA.Util { /// /// 缓存相关的操作类 /// Copyright (C) TBEA.WMS /// public class DataCache { private static IMemoryCache _cache; public DataCache(IMemoryCache cache) { _cache = cache; } /// /// 获取当前应用程序指定CacheKey的Cache值 /// /// /// public static object GetCache(string CacheKey) { //System.Web.Caching.Cache objCache = HttpRuntime.Cache; //return objCache[CacheKey]; object value = null; _cache?.TryGetValue(CacheKey, out value); return value; } /// /// 设置当前应用程序指定CacheKey的Cache值 /// /// /// public static void SetCache(string CacheKey, object objObject) { //System.Web.Caching.Cache objCache = HttpRuntime.Cache; //objCache.Insert(CacheKey, objObject); _cache?.Set(CacheKey, objObject, DateTimeOffset.Now.AddYears(10)); } /// /// 设置当前应用程序指定CacheKey的Cache值 /// /// /// public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration) { //System.Web.Caching.Cache objCache = HttpRuntime.Cache; //objCache.Insert(CacheKey, objObject,null,absoluteExpiration,slidingExpiration); _cache?.Set(CacheKey, objObject, slidingExpiration); } } }