tags:

views:

254

answers:

1

I would like to know if there is a way to use .NET's CacheDependency infrastructure, but cache to a database table or file, rather than the in-memory cache. I think the Enterprise Library may be able to do this but I would prefer something simpler if possible.

I want the cached data to be put in an SQL table or into a file on disk, rather than in-memory. The CacheDependency could be an SqlCacheDependency, or a CacheDependency on a file or an AggregateCacheDependency.

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.

+1  A: 

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 SqlCacheDependency is 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 with

  • The 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 SqlCacheDependency on the database table, and return the XML data

  • If 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 SqlCacheDependency on the table, and return the XML data

This gives you a self-managing two-tiered cache.

Sam
I already tried suggesting that in the previous same question...!
Mitch Wheat
Unless I am misunderstanding you completely, I want the 'cache' to be a cache in the database, rather than in memory. The dependency could be anything.
cbp
@Mitch it's a conspiracy to deny you your 15 points. We're all in on it.
Rex M
Thanks, looks like I'll investigate more into the Enterprise Library. Was just hoping to avoid it if possible.
cbp