Caching ASP.NETCore2.0中的CacheItemRemovedCallback

Caching ASP.NETCore2.0中的CacheItemRemovedCallback,caching,asp.net-core-mvc,Caching,Asp.net Core Mvc,我在asp.net内核中第一次通过google搜索,寻找与CacheItemRemovedCallback等效的方法,但到目前为止,搜索结果为空。我使用缓存失效来充当一些web应用程序中的伪服务/计时器,我正在寻找更多的核心 你们这些英明的互联网之神能给我任何指引,我都很感激 更新:我创建了一个OWIN类,该类在启动时似乎被正确调用,但缓存失效从未被调用。我是恩森小姐 public class OwinCacheTimer { private readonly RequestDelega

我在asp.net内核中第一次通过google搜索,寻找与CacheItemRemovedCallback等效的方法,但到目前为止,搜索结果为空。我使用缓存失效来充当一些web应用程序中的伪服务/计时器,我正在寻找更多的核心

你们这些英明的互联网之神能给我任何指引,我都很感激

更新:我创建了一个OWIN类,该类在启动时似乎被正确调用,但缓存失效从未被调用。我是恩森小姐

public class OwinCacheTimer
{
    private readonly RequestDelegate next;

    private IMemoryCache cache;

    public OwinCacheTimer(RequestDelegate next, IMemoryCache memoryCache)
    {
        this.cache = memoryCache;
        this.next = next;
        SetCache(this);
    }

    public  void SetCache(object o)
    {
        /* do work */

        MemoryCacheEntryOptions options = new MemoryCacheEntryOptions();
        options.AbsoluteExpiration = DateTime.Now.AddSeconds(21);
        options.SlidingExpiration = TimeSpan.FromSeconds(20);
        options.RegisterPostEvictionCallback(ExpiredCallback, o);  // <- this doesn't fire
        IMemoryCache x = ((OwinCacheTimer)o).cache;

        /* setting whateve, just so something expires */
        x.Set<string>("timestamp", DateTime.Now.ToString(), options);
    }


    public void ExpiredCallback(object key, object value, EvictionReason reason, object state)
    {
         SetCache(state);

    }

    public async Task Invoke(HttpContext context)
    {
        this.BeginInvoke(context);
        await this.next.Invoke(context);
        this.EndInvoke(context);
    }

    private void BeginInvoke(HttpContext context)
    {
        // Do custom work before controller execution
    }

    private void EndInvoke(HttpContext context)
    {
        // Do custom work after controller execution
    }
}
公共类OwinCacheTimer
{
私有只读请求委托下一步;
私有IMemoryCache缓存;
公共OwinCacheTimer(RequestDelegate next,IMemoryCache memoryCache)
{
this.cache=memoryCache;
this.next=next;
SetCache(this);
}
公共void SetCache(对象o)
{
/*工作*/
MemoryCacheEntryOptions=新的MemoryCacheEntryOptions();
options.AbsoluteExpiration=DateTime.Now.AddSeconds(21);
options.SlidingExpiration=TimeSpan.FromSeconds(20);

options.RegisterPostVictionCallback(ExpiredCallback,o);//看起来我的做法完全错了。您现在可以安排任务,而不必执行上述类型的黑客操作,但如果可能的话,我仍然希望知道如何用上述方法来完成。看来安排任务的正确方法是这样的,仅供参考-


好的,这篇文章似乎讨论了一个类似的概念,但看起来缓存绑定到一个控制器上,这可能会改变,因为它可以将缓存失效作为一种永久性的服务/计时器。我将玩一下,看看。我也可能离这里很远,上面的缓存示例是针对一个控制器的,我尝试将其破解到n OWIN类,可能这不可行。有关options.registerPostVictotionCallback(ExpiredCallback,o)的任何信息;不触发?我需要在缓存中存储的每个对象的缓存过期时间上调用一些函数,例如保存缓存中存储的数据,有什么想法吗?。这在System.Web.Caching Library中是可能的。老实说,我对此进行了推注,但从未返回。当时我找到的唯一文档是上面的链接。我认为我在System.Web.Caching上玩得很开心,但在下注之前却一无所获。