• .Net Core 缓存方式(一)内存缓存


    .Net Core 缓存方式(一)内存缓存

    使用 IMemoryCache

    官方文档

    • 官方文档
    • 使用包 dotnet add package System.Runtime.Caching --version 4.7.0
    • 使用方式
    public class HomeController : Controller
    {
        private IMemoryCache _cache;
    
        public HomeController(IMemoryCache memoryCache)
        {
            _cache = memoryCache;
        }
    
    public IActionResult CacheTryGetValueSet()
    {
        DateTime cacheEntry;
    
        // Look for cache key.
        if (!_cache.TryGetValue(CacheKeys.Entry, out cacheEntry))
        {
            // Key not in cache, so get data.
            cacheEntry = DateTime.Now;
    
            // Set cache options.
            var cacheEntryOptions = new MemoryCacheEntryOptions()
                // Keep in cache for this time, reset time if accessed.
                .SetSlidingExpiration(TimeSpan.FromSeconds(3));
    
            // Save data in cache.
            _cache.Set(CacheKeys.Entry, cacheEntry, cacheEntryOptions);
        }
    
        return View("Cache", cacheEntry);
    }
    
    
    public IActionResult CacheGetOrCreate()
    {
        var cacheEntry = _cache.GetOrCreate(CacheKeys.Entry, entry =>
        {
            entry.SlidingExpiration = TimeSpan.FromSeconds(3);
            return DateTime.Now;
        });
    
        return View("Cache", cacheEntry);
    }
    
    public async Task<IActionResult> CacheGetOrCreateAsynchronous()
    {
        var cacheEntry = await
            _cache.GetOrCreateAsync(CacheKeys.Entry, entry =>
            {
                entry.SlidingExpiration = TimeSpan.FromSeconds(3);
                return Task.FromResult(DateTime.Now);
            });
    
        return View("Cache", cacheEntry);
    }
    

    GetOrCreate 实现原理

            public static TItem GetOrCreate<TItem>(this IMemoryCache cache, object key, Func<ICacheEntry, TItem> factory)
            {
                if (!cache.TryGetValue(key, out object result))
                {
                    ICacheEntry entry = cache.CreateEntry(key);
                    result = factory(entry);
                    entry.SetValue(result);
                    // need to manually call dispose instead of having a using
                    // in case the factory passed in throws, in which case we
                    // do not want to add the entry to the cache
                    entry.Dispose();
                }
    
                return (TItem)result;
            }
    
            public static async Task<TItem> GetOrCreateAsync<TItem>(this IMemoryCache cache, object key, Func<ICacheEntry, Task<TItem>> factory)
            {
                if (!cache.TryGetValue(key, out object result))
                {
                    ICacheEntry entry = cache.CreateEntry(key);
                    result = await factory(entry).ConfigureAwait(false);
                    entry.SetValue(result);
                    // need to manually call dispose instead of having a using
                    // in case the factory passed in throws, in which case we
                    // do not want to add the entry to the cache
                    entry.Dispose();
                }
    
                return (TItem)result;
            }
    

    https://github.com/dotnet/runtime/blob/master/src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCache.cs

  • 相关阅读:
    js 中添加空格
    js radio 赋值取值
    对于不返回任何键列信息的 selectcommand 不支持 updatecommand 的动态 sql 生成
    Adapter
    MySQL学习笔记:while循环
    GIS数据资源下载
    观察者模式与发布订阅模式
    Cesium局部区域精细瓦片数据下载技巧
    Cesium加载地形数据只显示半个地球
    git教程和命令集合
  • 原文地址:https://www.cnblogs.com/WNpursue/p/13404038.html
Copyright © 2020-2023  润新知