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

所以..在请求中

  1. 如果出现提示,它存在于缓存中,删除根对象,所有依赖性都将被删除太
  2. 检查缓存中的根对象,如果不存在,则添加它
  3. 将其他对象添加到缓存中,并在根对象上依赖于依赖于根对象
  4. 当我这样做时,我得到一个错误“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}));
            }
        }
    }
    
    .

有帮助吗?

解决方案

上面的代码是工作,键在每次时创建新的缓存。 在非伪代码中,我尝试重复使用相同的对象,这导致错误描述。

@adeel&jaroslav感谢您的回复,无论如何

其他提示

单个事件时多功能发生的问题是当前可用的缓存不处理本地或有效的东西。作为 tagcache ,我邀请您尝试一下并查看它是否可能是您方案中的更好选择。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top