最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET?Core中的Caching組件簡介

 更新時間:2022年04月13日 11:46:06   作者:Ruby_Lu  
這篇文章介紹了ASP.NET?Core中的Caching組件,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

在.NET Core中提供了Caching的組件。目前Caching組件提供了三種存儲方式:

  • Memory
  • Redis
  • SQLSever

1.Memeor Caching

新建一個ASP.NET Core Web應(yīng)用程序項(xiàng)目,然后安裝 Microsoft.Extensions.Caching.Memory。

修改ConfigureServices方法

services.AddMemoryCache();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

在HomeController使用:

        private IMemoryCache memoryCache;
        public HomeController( IMemoryCache _memoryCache)
        {
            memoryCache = _memoryCache;
        }

        public IActionResult Index()
        {
            string cacheKey = "key";
            string result;
            if (!memoryCache.TryGetValue(cacheKey, out result))
            {
                result = $"LineZero{DateTime.Now}";
                memoryCache.Set(cacheKey, result);
                //設(shè)置相對過期時間
                memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
                    .SetSlidingExpiration(TimeSpan.FromSeconds(10)));
                //設(shè)置絕對過期時間
                memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
                    .SetAbsoluteExpiration(TimeSpan.FromSeconds(10)));
                //刪除緩存
                memoryCache.Remove(cacheKey);
                //設(shè)置緩存優(yōu)先級(程序壓力大時,會根據(jù)優(yōu)先級自動回收)
                memoryCache.Set(cacheKey,result,new MemoryCacheEntryOptions() 
                    .SetPriority(CacheItemPriority.NeverRemove));
                //過期時緩存回調(diào)
                memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
                    .SetAbsoluteExpiration(TimeSpan.FromSeconds(60))
                    .RegisterPostEvictionCallback((key, value, reason, substate)
                    =>
                    {
                        nlog.Warn($"鍵{key}值{value}改變,因?yàn)閧reason}");
                    }));
                //Token過期時,緩存回調(diào)
                var cts = new CancellationTokenSource();
                memoryCache.Set(cacheKey, result, new MemoryCacheEntryOptions()
                    .AddExpirationToken(new CancellationChangeToken(cts.Token))
                    .RegisterPostEvictionCallback((key, value, reason, substate)
                    =>
                    {
                        nlog.Warn($"鍵{key}值{value}改變,因?yàn)閧reason}");
                    }));
            }
            ViewBag.Cache = result;
            return View();
        }

2.Distributed Cache Tag Helper

在ASP.NET Core MVC 中有一個 Distributed Cache Tag Helper,它是依賴于MemoryCache組件的。

可以直接在試圖上增加 distributed-cache 標(biāo)簽

@{
    ViewData["Title"] = "Home Page";
}
<distributed-cache name="mycache" expires-after="TimeSpan.FromSeconds(10)">
    <p>緩存項(xiàng)10秒過期(expires-after絕對過期時間)</p>
</distributed-cache>
<distributed-cache name="mycachenew" expires-sliding="TimeSpan.FromSeconds(10)">
    <p>相對十秒(expires-sliding相對過期時間)</p>
    @DateTime.Now
</distributed-cache>
<div>@ViewBag.Cache</div>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

周至县| 图木舒克市| 内黄县| 广州市| 杭锦旗| 通州区| 新余市| 壶关县| 南漳县| 东山县| 共和县| 宜宾县| 台湾省| 榕江县| 井冈山市| 漯河市| 铁力市| 临安市| 平昌县| 高尔夫| 修武县| 盐津县| 专栏| 安福县| 大姚县| 尉犁县| 门源| 利辛县| 恩施市| 九龙坡区| 永康市| 阿克| 寿宁县| 房山区| 广南县| 庆云县| 疏勒县| 隆德县| 桂林市| 通州市| 越西县|