【问题标题】:Get Absolute Expiration event from ObjectCache从 ObjectCache 获取 Absolute Expiration 事件
【发布时间】:2022-01-22 21:04:48
【问题描述】:

我将 ObjectCache 对象用作MemoryCache.Default (System.Runtime.Caching),并且我的策略包含 AbsoluteExpiration。

例如

ObjectCache cache = MemoryCache.Default
cahce.Add(key, reading, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(20)});

我想在对象被删除之前保存它。

是否可以在项目从缓存中删除之前捕获过期事件?

【问题讨论】:

  • new CacheItemPolicy { ... , RemovedCallback = ev => { //save ev.CacheItem }} ?虽然它发生在物品被移除之后,但对你来说有什么真正的不同吗?为什么?
  • 还有一个UpdateCallback,在项目被移除之前被调用
  • 谢谢,可以举个例子吗?
  • @fatalica 的例子到底是什么?
  • 感谢@GuruStron,我能够按照你的描述使用回调

标签: c# caching .net-framework-4.8


【解决方案1】:

使用Add函数在MemoryCache中添加Entry时,可以添加CacheItemPolicy

这个类有这个字段:

     private DateTimeOffset _absExpiry;
        private TimeSpan _sldExpiry;
        private Collection<ChangeMonitor> _changeMonitors;
        private CacheItemPriority _priority;
        private CacheEntryRemovedCallback _removedCallback;
        private CacheEntryUpdateCallback _updateCallback;

你需要的是_removedCallback

源代码:herehere

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    相关资源
    最近更新 更多