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();
}
}
}