dotNetCore v3-MemoryCache被动过期

using Microsoft.Extensions.Caching.Memory;
using System;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            MemoryCache cache = new MemoryCache(new MemoryCacheOptions()
            {
            });

            var option = new MemoryCacheEntryOptions()
            {
                AbsoluteExpiration = DateTime.Now.AddSeconds(5)
            };
            option.RegisterPostEvictionCallback((key, value, reason, obj) =>
            {
                Console.WriteLine(reason);
            });

            cache.Set("key", "value", option);

            while(true) 
            {
                cache.Get("key"); //懒模式,获得时候才会触发过期回调函数
            }

      
            Console.Read();
        }
    }
}
 

发布了445 篇原创文章 · 获赞 71 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/dxm809/article/details/104184457
今日推荐