I would like to know if there is a way to have the .NET cache stored in a database table or file, rather than the in-memory cache, but also use the CacheDependency infrastructure. I think the Enterprise Library may be able to do this but I would prefer something simpler if possible.
To explain, I want the cached data to be put in an SQL table or into a file on disk, rather than in-memory. I then want to specify a CacheDependency, which could be an SqlCacheDependency, or a CacheDependency on a file or an AggregateCacheDependency.
The reason is, I do intensive calculations based on xml documents (that change once a day or so). The results of these calculations I cache in memory. However if the website is reset, the cache is lost. It would be nice to have a backup cache in the database.
Use SqlCacheDependency. You’ll need to manually manage the target data yourself, but you can have a dependency in the cache which is automatically invalidated when the target table changes.
EDIT:
Yes, you can use the Caching Block in Enterprise Library to have a cache provider that stores items in a database using the Data Access Block, which is an alternative to the ASP.NET. That’s likely to be the best solution, despite the higher learning curve.
I think
SqlCacheDependencyis simpler because you use the ASP.NET cache to store a local memory version first for faster processing, falling back to the database layer when the local layer does not have the item available. Here’s an example of what I mean:Your web application calls
GetXmlDocument(string key)to get something to work withThe method checks the ASP.NET cache first for the item – if the item exists, great, you return that
If the item doesn’t exist, then you check the database for the existence of the item – if the item exists, you re-populate the local cache item with a
SqlCacheDependencyon the database table, and return the XML dataIf the item doesn’t exist in the database either, you retrieve the XML data from wherever the primary source is, re-populate the database, re-populate the local cache item with
SqlCacheDependencyon the table, and return the XML dataThis gives you a self-managing two-tiered cache.