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

  • 相关阅读:
    gobject对象不宜作为动态加载的插件
    用内存管理器的钩子函数跟踪内存泄漏
    DBUS与多线程
    broncho小组放假半天为中国奥运加油
    多进程DirectFB用X11显示的死锁问题
    佛诞节快乐
    R语言中substr函数,字符串截取函数
    R语言中while循环
    R语言中求分位数
    R语言中利用sapply函数提取列表中元素
  • 原文地址:https://www.cnblogs.com/WNpursue/p/13404038.html
Copyright © 2011-2022 走看看