C# 4.0 MemoryCache - 当依赖项发生更改时如何逐出依赖项缓存条目
当对它们所依赖的其他条目进行更改时,我试图从 MemoryCache 中逐出条目。这是通过为依赖键上的依赖项创建缓存条目更改监视器来设置的:
public bool AddToCache(string key, object dataItem,
DateTimeOffset absoluteExpiration, IEnumerable<string> dependencyKeys)
{
bool result = false;
if (!string.IsNullOrWhiteSpace(key) && dataItem != null)
{
CacheItemPolicy policy = new CacheItemPolicy {
AbsoluteExpiration = absoluteExpiration
};
if (masterKeys != null && masterKeys.Any())
{
policy.ChangeMonitors.Add(
this.provider.Cache.
CreateCacheEntryChangeMonitor(dependencyKeys));
foreach (ChangeMonitor monitor in policy.ChangeMonitors)
{
monitor.NotifyOnChanged(this.OnDependencyChanged);
}
}
result = this.provider.Cache.Add(key, dataItem, policy);
}
return result;
}
OnChangedCallBack
方法是这样的:
private void OnDependencyChanged(object state)
{
// what do I do here as "state" is always null?
}
项目按预期添加到缓存中,并且 OnDependencyChanged
当对受监视的键进行更改时,将按预期调用 code> 方法,但是传递给它的“状态”实例始终为 null,这意味着我对依赖项已更改的缓存键一无所知,因此无法执行计划中的驱逐。
我在这里错过了什么吗?我是否以错误的方式处理这件事?
I am attempting to evict entries from a MemoryCache when changes are made to other entries on which they are dependent. This is being set up by creating cache entry change monitors for the dependencies on the dependent keys:
public bool AddToCache(string key, object dataItem,
DateTimeOffset absoluteExpiration, IEnumerable<string> dependencyKeys)
{
bool result = false;
if (!string.IsNullOrWhiteSpace(key) && dataItem != null)
{
CacheItemPolicy policy = new CacheItemPolicy {
AbsoluteExpiration = absoluteExpiration
};
if (masterKeys != null && masterKeys.Any())
{
policy.ChangeMonitors.Add(
this.provider.Cache.
CreateCacheEntryChangeMonitor(dependencyKeys));
foreach (ChangeMonitor monitor in policy.ChangeMonitors)
{
monitor.NotifyOnChanged(this.OnDependencyChanged);
}
}
result = this.provider.Cache.Add(key, dataItem, policy);
}
return result;
}
The OnChangedCallBack
method is this:
private void OnDependencyChanged(object state)
{
// what do I do here as "state" is always null?
}
The items are added to the cache as intended, and the OnDependencyChanged
method is called as expected when a change is made to a monitored key, however the "state" instance that is passed to it is always null which means that I know nothing about the cache key whose dependency has changed and can therefore not perform the planned eviction.
Have I missed something here, am I going about this all the wrong way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
OnChangedCacheEntry
中用作参数的委托是:因此您必须像这样调用它:
然后您将可以访问 OnDependencyChanged 中的依赖项键
The delegate that is used as a parameter in
OnChangedCacheEntry
is:So you have to call it like this:
And then you will have access to the dependency keys in OnDependencyChanged
我知道这是一个旧线程,但这就是我所拥有的并且它似乎有效。
OnChangedCallback(dependencies) 中的 dependencies 参数是原始帖子中缺少的内容。
I know it's an old thread, but here's what I have and it seems to work.
The dependencies parameter in OnChangedCallback(dependencies) is what's missing from original post.
假设您想要做的只是逐出相关条目,如OP所述,您无需担心NotifyOnChanged。这就是您所需要的:
Assuming all you want to do is evict dependent entries, as OP stated, you don't need to worry about NotifyOnChanged. This is all you need: