开发者

CacheDependancy for multiple Cached items

开发者 https://www.devze.com 2023-01-07 19:00 出处:网络
I would like to have an item in the ASP.NET CacheObject, which if it were changed a number of dependant items would be removed

I would like to have an item in the ASP.NET CacheObject, which if it were changed a number of dependant items would be removed

So.. In a request

  1. If prompted and it exists in the cache remove the root object, all dependence will be removed too
  2. Check for root object in the cache, if it doesn't exist, add it
  3. Add other objects to the cache with a dependency on the root object

When I do this I get an error "An attempt was made to reference a CacheDependency object from more than one Cache entry"

I see that you can do an AggregateCacheDependency to apply many dependencies to one cached item, but it seems you cannot do it the other way around.

Has any one found a way to do this?

here is some code, its not what I am actually storing but it represents the same task

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.In开发者_StackOverflow中文版sert(
                Request.Url.AbsolutePath, string.Empty, 
                new CacheDependency(null, new []{ROOT_KEY}));
        }
    }
}


The above code does work, the key was creating a new CacheDependency each time. In the non-pseudo code I was attempting to reuse the same object, this was causing the error described.

@Adeel & Jaroslav thanks for the replies anyway


The problem of multiple invalidations upon a single event is something the currently available caches don't deal with natively or efficiently. As the author of TagCache, I invite you to try it out and see whether it may be a better choice in your scenario.

0

精彩评论

暂无评论...
验证码 换一张
取 消