views:

280

answers:

2

Hi folks,

I've got two items in my cache

  1. Key: Cat
  2. Key: Animal_Vacinations

Now, Animal_Vacinations has a key-based cache dependency on Cat. So, if anything changes for the cache item Cat, then the cache item Animal_Vacinations get invalidated. PERFECT :)

Ok.. now to the problem.

After i create the 2nd cache item (ie. Animal_Vacinations) I add a 3rd cache object :-

  1. Key: Dog

Problem is, the 2nd object needs to have a dependency on Dog also. At the time of creating the 2nd object, it knows what items it should depend on. So in this example the Animal_Vacination object knows it's should be dependant on ...

  1. Cat
  2. Dog
  3. Bird
  4. Jon Skeet

Problem is, if i try and insert the Animal_Vacination object into the cache with all those 4 dependencies, it fails. No error, just fails. (ie. the Cache["Animal_Vacination"] == null).

The reason for this, is, when i insert the cache object with those 4 dependencies ... *but 1 or more of those dependencies do not exist* ... it fails gracefully.

bummer.

because in my example above, one of those missing three objects is added right after the 2 object is added.

So ... is there anyway to add an object to a cache, with key-based cache dependencies, where 1 or more dependencies are not yet created but MIGHT be created later on?

A: 
Numenor
but how would you do that? how does a dog or bird know to tell the Animal_Vacinations to add a dependency to what it just added?
Pure.Krome
A: 

I'm reluctant to call this an answer, as it makes a number of assumptions that should probably be clarified with additional questions first, but here goes.

Assuming there's a piece of code somewhere responsible for adding something to the cache with the key "Animal_Vacinations", and that code knows what other cached items the "Animal_Vacinations" item is dependent on it, then that code should create each of the necessary cache key dependencies, including adding Null objects to the cache, if necessary, for any dependent items not already found there.

So, for instance, in the example you give, where there is already "Cat" in the cache prior to adding "Animal_Vacinations", then the logic responsible for adding "Animal_Vacinations" to the cache should check the cache for the existence of each dependent item, i.e., "Cat", "Dog", "Bird", "Jon Skeet"; when one isn't found, a placeholder object or boxed value (maybe an empty string) should be added to the cache for that key (in this case, for "Dog", "Bird", and "Jon Skeet"); once all of the dependent items exist in the cache, then create your cache dependencies and add "Animal_Vacinations" to the cache. (Alternatively, call Cache.Add with a placeholder object for each of the required dependent keys, without first checking if they exist with a Get, and use an exception handler to swallow the exception thrown if it already exists.)

Continuing your example, when subsequent to this activity a real something is added to the cache with the "Dog" key, using Insert instead of Add in order to account for the possibility that the key already exists (as it does in this example), then the replacement of the "Dog" cache item, which was simply a Null value, will trigger the invalidation of the "Animal_Vacinations" cache item per its cache dependency.

ewbi
SQUIRT!!!!!!!!!! awesome dude! i'm gonna give this a go. That's such a cool solution! it makes perfect sence. Right now, the Animal_Vacination knows what it has to be dependant on. Has to. As such, i'm checking to see if the cache item exist and if so, add dependency. It never cares about the CONTENT of that dependency, just that it exists. So instead, i'll create the cache item if it doesn't exist. When the real data is loaded, it will override the fake content. the dependency remains. AWESOMESAUCE. i'll post back with info once i've done it.
Pure.Krome
Confirmed. Worked. You beauty!
Pure.Krome