代码之家  ›  专栏  ›  技术社区  ›  Anthony Johnston

多个缓存项的CacheDependency

  •  1
  • Anthony Johnston  · 技术社区  · 14 年前

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

    所以。。在请求中

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

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

    有人找到办法了吗?

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

    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}));
            }
        }
    }
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   Anthony Johnston    14 年前

    上面的代码确实有效, 关键是每次都要创建一个新的CacheDependency .

    @阿德尔公司;雅罗斯拉夫谢谢你的回复

        2
  •  0
  •   Shiva    13 年前

    TagCache ,我邀请您尝试一下,看看在您的场景中它是否是一个更好的选择。

    推荐文章