来自 2 个或更多其他缓存项的 CacheDependency。 (ASP.NET MVC3)

发布于 2024-12-08 09:50:37 字数 934 浏览 4 评论 0原文

我对 ASP.NET 中可能存在的缓存依赖项有点困惑,而且我不知道如何使用它们。

我想以某种方式将项目添加到 HttpRuntime.Cache,如果我更改缓存中的其他元素,这些元素应该无效。依赖关系应该由键定义。

我想要一个这样的功能:

public MyObject LoadFromCache(string itemDescriptor, IEnumerable<string> dependencies)
{
    var ret = HttpRuntime.Cache[itemDescriptor] as MyObject;
    if (ret == null)
    {
        ret = LoadFromDataBase(itemDescriptor);

        //this is the part I'm not able to figure out. Adding more than one dependency items.
        var dep = new CacheDependency();
        dependencies.ForEach(o => dep.SomeHowAdd(o));

        HttpRuntime.Cache.Add(
            itemDescriptor, 
            ret, 
            dependencies, 
            System.Web.Caching.Cache.NoAbsoluteExpiration, 
            System.Web.Caching.Cache.NoSlidingExpiration, 
            Caching.CacheItemPriority.Normal, 
            null
        );
    }
    return ret;
}

帮我解决这个问题。

I'm a little puzzled over the possible cachedependencies in asp.net, and I'm not sure how to use them.

I would like to add items to the HttpRuntime.Cache in a way, that the elements should invalidate if I change other elements in the cache. The dependencies should be defined by the key.

I want a function like this:

public MyObject LoadFromCache(string itemDescriptor, IEnumerable<string> dependencies)
{
    var ret = HttpRuntime.Cache[itemDescriptor] as MyObject;
    if (ret == null)
    {
        ret = LoadFromDataBase(itemDescriptor);

        //this is the part I'm not able to figure out. Adding more than one dependency items.
        var dep = new CacheDependency();
        dependencies.ForEach(o => dep.SomeHowAdd(o));

        HttpRuntime.Cache.Add(
            itemDescriptor, 
            ret, 
            dependencies, 
            System.Web.Caching.Cache.NoAbsoluteExpiration, 
            System.Web.Caching.Cache.NoSlidingExpiration, 
            Caching.CacheItemPriority.Normal, 
            null
        );
    }
    return ret;
}

Help me out on this one.

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

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

发布评论

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

评论(1

删除→记忆 2024-12-15 09:50:37

我不知道你可以这样做,但是如果你查看 CacheDependency 构造函数这里,您可以看到第二个参数是一个缓存键数组,因此,如果这些缓存项中的任何一个发生更改,整个依赖项都将更改,并且您的依赖项也将失效。

所以你的代码会是这样的:

String[] cacheKeys = new string[]{"cacheKey1","cacheKey2"};
var dep = New CacheDependency("", cacheKeys);

HttpRuntime.Cache.Add(itemDescriptor, ret, dep ...);

I didn't know you could do this, but if you look at the CacheDependency constructor here, you can see that the second parameter is an array of cache keys, so that if any of those cached items change, the whole dependency will be changed and your dependent item will be invalidated as well.

So your code would be something like:

String[] cacheKeys = new string[]{"cacheKey1","cacheKey2"};
var dep = New CacheDependency("", cacheKeys);

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