We are refactoring our site to use an external cache and the first step we took was using a custom OutputCacheProvider. First, we created a simple provider that just wraps MemoryCache and found problems with the way we are managing dependencies.
We have a custom OutputCacheAttribute that adds an extra key dependency to be able to invalidate a set of pages when certain entities change and to keep this feature I see some options:
-
Removing manually the
CachedVarythat ASP.NET stores in the cache, assuming that the key is"a2" + query". This seems to work but I’m not sure about the reliability. -
Add cache keys that contain an array of the pages that have to be evicted from the cache then the key is removed o use the external cache key dependency feature in case it has it. This should be enough to emulate the key dependency we used but in a more complex way.
-
Forget about this, put a short cache period and let them expire without worrying much about that.
-
Do our own page caching and forget about ASP.NET output cache, not very appealing.
I’m sure there are other ways. Any tips, experiences or recommendations?
I answer my own question with the solution we adopted just for the record.
In our
OutputCacheAttributewe add an empty cache object with a key that depends on the requested URL and some parameters. This will be used to invalidate a page externally.Then, we also add another object with a key that depends on the current request and contains the previous cacheKey.
Finally, a static ValidationCallback is set up. The callback gets the value of the key for the current request, which is the dependency key. Then if it’s not null, gets the value of the dependency, if it’s null, the dependency has been evicted and we set the
validationStatustoHttpValidationStatus.Invalid.Some code to illustrate: