views:

25

answers:

1

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.Insert(
                Request.Url.AbsolutePath, string.Empty, 
                new CacheDependency(null, new []{ROOT_KEY}));
        }
    }
}
A: 

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

Anthony Johnston