C# 4.0 MemoryCache - 当依赖项发生更改时如何逐出依赖项缓存条目

发布于 2024-12-09 23:21:07 字数 1298 浏览 0 评论 0原文

当对它们所依赖的其他条目进行更改时,我试图从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

就是爱搞怪 2024-12-16 23:21:07

OnChangedCacheEntry 中用作参数的委托是:

public delegate void OnChangedCallback(object state);

因此您必须像这样调用它:

monitor.NotifyOnChanged(delegate {
       OnChangedCacheEntry(OnDependencyChanged(dependencyKeys)); });

然后您将可以访问 OnDependencyChanged 中的依赖项键

private void OnDependencyChanged(object state)
{
    IEnumerable<string> dependencyKeys = (IEnumerable<string>) state;
}

The delegate that is used as a parameter in OnChangedCacheEntry is:

public delegate void OnChangedCallback(object state);

So you have to call it like this:

monitor.NotifyOnChanged(delegate {
       OnChangedCacheEntry(OnDependencyChanged(dependencyKeys)); });

And then you will have access to the dependency keys in OnDependencyChanged

private void OnDependencyChanged(object state)
{
    IEnumerable<string> dependencyKeys = (IEnumerable<string>) state;
}
你又不是我 2024-12-16 23:21:07

我知道这是一个旧线程,但这就是我所拥有的并且它似乎有效。

...

if (dependencies != null)
{
    var monitor = MemoryCache.Default.CreateCacheEntryChangeMonitor(dependencies);
    monitor.NotifyOnChanged(delegate { OnChangedCallback(dependencies); });
    policy.ChangeMonitors.Add(monitor);
}

...

private static void OnChangedCallback(object state)
{
    var keys = (IEnumerable<string>) state;
    if (keys != null)
        Logger.InfoFormat("callback - {0}", string.Join("|", keys.ToArray()));
    else
        Logger.InfoFormat("callback - null");
}

OnChangedCallback(dependencies) 中的 dependencies 参数是原始帖子中缺少的内容。

I know it's an old thread, but here's what I have and it seems to work.

...

if (dependencies != null)
{
    var monitor = MemoryCache.Default.CreateCacheEntryChangeMonitor(dependencies);
    monitor.NotifyOnChanged(delegate { OnChangedCallback(dependencies); });
    policy.ChangeMonitors.Add(monitor);
}

...

private static void OnChangedCallback(object state)
{
    var keys = (IEnumerable<string>) state;
    if (keys != null)
        Logger.InfoFormat("callback - {0}", string.Join("|", keys.ToArray()));
    else
        Logger.InfoFormat("callback - null");
}

The dependencies parameter in OnChangedCallback(dependencies) is what's missing from original post.

太阳男子 2024-12-16 23:21:07

假设您想要做的只是逐出相关条目,如OP所述,您无需担心NotifyOnChanged。这就是您所需要的:

public bool AddToCache(string key, object dataItem,
    DateTimeOffset absoluteExpiration, IEnumerable<string> dependencyKeys)
{
    CacheItemPolicy policy = new CacheItemPolicy();
    policy.AbsoluteExpiration = absoluteExpiration;

    ChangeMonitor monitor = MemoryCache.Default.CreateCacheEntryChangeMonitor(dependencyKeys);
    policy.ChangeMonitors.Add(monitor);

    return MemoryCache.Default.Add(key, dataItem, policy);
}

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:

public bool AddToCache(string key, object dataItem,
    DateTimeOffset absoluteExpiration, IEnumerable<string> dependencyKeys)
{
    CacheItemPolicy policy = new CacheItemPolicy();
    policy.AbsoluteExpiration = absoluteExpiration;

    ChangeMonitor monitor = MemoryCache.Default.CreateCacheEntryChangeMonitor(dependencyKeys);
    policy.ChangeMonitors.Add(monitor);

    return MemoryCache.Default.Add(key, dataItem, policy);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文