缓存并重复触发 NotifyOnChanged

发布于 2024-12-03 02:19:31 字数 1447 浏览 0 评论 0原文

我正在尝试使用文件作为触发器来刷新我的缓存项目。当文件更改时,我需要触发一个事件(每次文件更改时)。我目前正在使用 HostFileChangeMonitor 类。这是设置缓存项的位置,使用策略将其链接到文件:

private static void SetPolicy(CacheNames cacheName, CacheItem item)
    {
        string strCacheName = cacheName.ToString();
        CacheItemPolicy policy;
        if (_policies.TryGetValue(strCacheName, out policy))
        {
            _cacheObject.Set(item, policy);
            return;
        }
        policy = new CacheItemPolicy();
        List<string> filePaths = new List<string> {
            string.Format(@"{0}\{1}.txt",Config.AppSettings.CachePath,cacheName.ToString())
        };
        var changeMonitor = new HostFileChangeMonitor(filePaths);
        _cacheObject.Set(item, policy);
        changeMonitor.NotifyOnChanged(new OnChangedCallback(RefreshCache));
        policy.ChangeMonitors.Add(changeMonitor);
    }

但是,NotifyOnChanged 仅触发一次。因此,我当前正在删除该项目,然后在 NotifyOnChanged 调用的 RefreshCache 方法中将其重新添加到缓存中:

private static void RefreshCache(object state)
    {
        //remove from cache
        WcfCache.ClearCache("Refreshed");
        //resubscribe to NotifyOnChanged event
        WcfCache.SetCache("Refreshed", true, CacheNames.CacheFileName);
        //grab all cache data and refresh each in parallel
}

是否有更好的方法来执行此操作?是否有一个我可以触发的事件总是会触发(而不是像 NotifyOnChanged 这样第一次触发)?这看起来相当脆弱。如果 HostFileChangeMonitor 一次没有正确添加,整个应用程序的缓存将永远不会刷新。

I am attempting to use a file as a trigger to refresh my cached items. When the file is changed, I need to fire an event (every time the file changes). I'm currently using the HostFileChangeMonitor class. Here's where a cached item gets set, using a policy to link it to a file:

private static void SetPolicy(CacheNames cacheName, CacheItem item)
    {
        string strCacheName = cacheName.ToString();
        CacheItemPolicy policy;
        if (_policies.TryGetValue(strCacheName, out policy))
        {
            _cacheObject.Set(item, policy);
            return;
        }
        policy = new CacheItemPolicy();
        List<string> filePaths = new List<string> {
            string.Format(@"{0}\{1}.txt",Config.AppSettings.CachePath,cacheName.ToString())
        };
        var changeMonitor = new HostFileChangeMonitor(filePaths);
        _cacheObject.Set(item, policy);
        changeMonitor.NotifyOnChanged(new OnChangedCallback(RefreshCache));
        policy.ChangeMonitors.Add(changeMonitor);
    }

The NotifyOnChanged fires only once, however. Because of that, I am currently removing and then re-adding the item to the cache in the RefreshCache method called by the NotifyOnChanged:

private static void RefreshCache(object state)
    {
        //remove from cache
        WcfCache.ClearCache("Refreshed");
        //resubscribe to NotifyOnChanged event
        WcfCache.SetCache("Refreshed", true, CacheNames.CacheFileName);
        //grab all cache data and refresh each in parallel
}

Is there a better way to do this? Is there an event I can tap into that will ALWAYS fire (instead of just the first time like this NotifyOnChanged)? This seems pretty fragile. If the HostFileChangeMonitor doesn't get added properly one time, the entire app's cache will never refresh.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

固执像三岁 2024-12-10 02:19:31

您是否尝试过 FileSystemWatcher 并处理其 Change 事件?这里是 MSDN 文档 有关该主题的更多详细信息。

Have you tried the FileSystemWatcher and handling its Change event?Here's MSDN's documentation on the subject for more detailed info.

夏天碎花小短裙 2024-12-10 02:19:31

我刚刚发现了这个类,从我得到的情况来看,NotifyOnChanged 并不意味着每次更新监视文件时都会被触发(即使这个名称让您有不同的想法)。
相反,该文件会不断被监视和缓存,您只需在需要时从缓存中获取它即可。

I've just discovered this class and from what I get, the NotifyOnChanged is not meant to be fired every time a watched file is updated (even though the name lets you think otherwise).
Rather the file is constantly being watched and cached and you simply need to get it from the cache whenever you need it.

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