zoukankan      html  css  js  c++  java
  • .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

  • 相关阅读:
    swf上传地址
    Nape 获取碰撞点加特效
    vbs打包exe工具
    Air打包exe
    JDK12的安装搭建
    Dubble 入门
    FastDFS 集群
    PAT Advanced 1077 Kuchiguse (20 分)
    高可用4层lvs——keepalived
    PAT Advanced 1035 Password (20 分)
  • 原文地址:https://www.cnblogs.com/WNpursue/p/13404038.html
Copyright © 2011-2022 走看看