我需要一个这样的函数:
AddToList(txtName, timeExpire);
Run Code Online (Sandbox Code Playgroud)
它看起来像是自描述的,该项目将自动过期并从列表中删除.
我无法想象如何做到这一点.任何人都可以给我一个线索吗?
我在Microsoft.Extensions.Caching.Memory.MemoryCache中设置了具有滑动过期的缓存项.我想在每次缓存项到期时触发回调,但在我查询缓存中过期的缓存项之前,不会触发回调.
这是代码:
using System;
using Microsoft.Extensions.Caching.Memory;
namespace Memcache
{
public class Program
{
private static MemoryCache _cache;
private static int _cacheExpSecs;
public static void Main(string[] args)
{
_cache = new MemoryCache(new MemoryCacheOptions());
_cacheExpSecs = 2;
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromSeconds(_cacheExpSecs))
.RegisterPostEvictionCallback(callback: EvictionCallback);
_cache.Set(1, "One", cacheEntryOptions);
_cache.Set(2, "Two", cacheEntryOptions);
var autoEvent = new System.Threading.AutoResetEvent(false);
System.Threading.Timer timer = new System.Threading.Timer(checkCache, autoEvent, 1000, 6000);
Console.Read();
}
private static void checkCache(Object o)
{
if(_cache.Get(1)!=null)
{
Console.WriteLine(string.Format(@"checkCache: Cache with key {0} will be removed …Run Code Online (Sandbox Code Playgroud)