Reputation: 2935
I have a service that I wanted to implement some sort of caching on, but am unsure as to whether I should use .NET 4's ObjectCache or simply have private fields on the service that, after some time, gets refreshed.
The situation is as follows: The service is used to return large chunks of data. Currently, we hit the database for the data on every request to the service. This data is only refreshed once a day at midnight. I want to cache this data on the service and refresh it every night after the database updates (say around 12:30am).
Now, in the past, we would populate local fields and create a "monitor thread" that would run constantly and after some specified amount of time, re-populate those fields. One downside I saw to that was the service class had some extra code in it for the storage.
If I switch to the ObjectCache, one benefit is the auto-removal when expired. What I was thinking of doing was assigning a callback method to the CacheEntryRemovedCallback delegate and when that was called, just re-cache the data at some point in the future (~30 minutes once the database is refreshed).
My question is, for a WCF service, what's the more desirable option for caching data?
Upvotes: 1
Views: 587
Reputation: 6008
Use System.Runtime.Caching
. It is better than homegrown bicycle.
You can use SqlChangeMonitor
if your data updated only at midnight. That will significantly simplify logic and your cache will be synchronized with data.
http://www.codeproject.com/Articles/167282/NET-4-0-MemoryCache-with-SqlChangeMonitor
Upvotes: 1