I've got two items in my cache
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 :-
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 ...
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?
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.