Asp.net Core CacheHelper 通用緩存幫助類

 

注:本文轉載html

public class CacheHelper
    {
        public static IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions());

        /// <summary>
        /// 緩存絕對過時時間
        /// </summary>
        ///<param name="key">Cache鍵值</param>
        ///<param name="value">給Cache[key]賦的值</param>
        ///<param name="minute">minute分鐘後絕對過時</param>
        public static void CacheInsertAddMinutes(string key, object value, int minute)
        {
            if (value == null) return;
            _memoryCache.Set(key, value, new MemoryCacheEntryOptions()
                    .SetAbsoluteExpiration(TimeSpan.FromMinutes(minute)));
        }

        /// <summary>
        /// 緩存相對過時,最後一次訪問後minute分鐘後過時
        /// </summary>
        ///<param name="key">Cache鍵值</param>
        ///<param name="value">給Cache[key]賦的值</param>
        ///<param name="minute">滑動過時分鐘</param>
        public static void CacheInsertFromMinutes(string key, object value, int minute)
        {
            if (value == null) return;
            _memoryCache.Set(key, value, new MemoryCacheEntryOptions()
                    .SetSlidingExpiration(TimeSpan.FromMinutes(minute)));
        }

        /// <summary>
        ///以key鍵值,把value賦給Cache[key].若是不主動清空,會一直保存在內存中.
        /// </summary>
        ///<param name="key">Cache鍵值</param>
        ///<param name="value">給Cache[key]賦的值</param>
        public static void CacheInsert(string key, object value)
        {
            _memoryCache.Set(key, value);
        }

        /// <summary>
        ///清除Cache[key]的值
        /// </summary>
        ///<param name="key"></param>
        public static void CacheNull(string key)
        {
            _memoryCache.Remove(key);
        }

        /// <summary>
        ///根據key值,返回Cache[key]的值
        /// </summary>
        ///<param name="key"></param>
        public static object CacheValue(string key)
        {
            return _memoryCache.Get(key);
        }

        #region 緩存文檔
        //public IActionResult Index()
        //{
        //    string cacheKey = "key";
        //    string result;
        //    if (!_memoryCache.TryGetValue(cacheKey, out result))
        //    {
        //        result = $"LineZero{DateTime.Now}";
        //        _memoryCache.Set(cacheKey, result);
        //        //設置相對過時時間2分鐘
        //        _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
        //            .SetSlidingExpiration(TimeSpan.FromMinutes(2)));
        //        //設置絕對過時時間2分鐘
        //        _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
        //            .SetAbsoluteExpiration(TimeSpan.FromMinutes(2)));
        //        //移除緩存
        //        _memoryCache.Remove(cacheKey);
        //        //緩存優先級 (程序壓力大時,會根據優先級自動回收)
        //        _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
        //            .SetPriority(CacheItemPriority.NeverRemove));
        //        //緩存回調 10秒過時會回調
        //        _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
        //            .SetAbsoluteExpiration(TimeSpan.FromSeconds(10))
        //            .RegisterPostEvictionCallback((key, value, reason, substate) =>
        //            {
        //                Console.WriteLine($"鍵{key}值{value}改變,由於{reason}");
        //            }));
        //        //緩存回調 根據Token過時
        //        var cts = new CancellationTokenSource();
        //        _memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
        //            .AddExpirationToken(new CancellationChangeToken(cts.Token))
        //            .RegisterPostEvictionCallback((key, value, reason, substate) =>
        //            {
        //                Console.WriteLine($"鍵{key}值{value}改變,由於{reason}");
        //            }));
        //        cts.Cancel();
        //    }
        //    ViewBag.Cache = result;
        //    return View();
        //}
        #endregion
    }

 

更多:跨域

.NetCore中EFCore的使用整理(三)-關聯表操做緩存

windos server2012安裝.net core 2.2問題spa

Ajax跨域請求附帶Cookie/Ajax跨域請求附帶身份憑證.net

相關文章
相關標籤/搜索