1

我想在 ASP.NET CacheObject 中有一个项目,如果它被更改,许多相关项目将被删除

所以..在一个请求中

  1. 如果提示并且它存在于缓存中删除根对象,所有依赖也将被删除
  2. 检查缓存中的根对象,如果它不存在,则添加它
  3. 将依赖于根对象的其他对象添加到缓存中

当我这样做时,我得到一个错误“ An attempt was made to reference a CacheDependency object from more than one Cache entry

我看到您可以执行 AggregateCacheDependency 将许多依赖项应用于一个缓存项,但似乎您不能以相反的方式执行此操作。

有没有人找到一种方法来做到这一点?

这是一些代码,它不是我实际存储的,但它代表相同的任务

public class HomeController : Controller
{
    private const string ROOT_KEY = "ROOT";

    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {   
        base.Initialize(requestContext);

        if(Request.QueryString["clearcache"]!=null){
            // removed the root, hopefully removing all dependents
            HttpContext.Cache.Remove(ROOT_KEY);
        }

        if (HttpContext.Cache[ROOT_KEY] == null)
        {
            // create the root entry
            HttpContext.Cache[ROOT_KEY] = string.Empty;
        }

        if(HttpContext.Cache[Request.Url.AbsolutePath]==null){

            // add the url if not already added
            HttpContext.Cache.Insert(
                Request.Url.AbsolutePath, string.Empty, 
                new CacheDependency(null, new []{ROOT_KEY}));
        }
    }
}
4

2 回答 2

3

上面的代码确实有效,关键是每次都创建一个新的 CacheDependency。在我试图重用同一个对象的非伪代码中,这导致了所描述的错误。

@Adeel & Jaroslav 无论如何感谢您的回复

于 2010-07-14T11:42:28.090 回答
0

单个事件多次失效的问题是当前可用的缓存无法本地或有效处理的问题。作为TagCache的作者,我邀请您尝试一下,看看它是否是您场景中更好的选择。

于 2011-09-24T03:12:03.573 回答