I am pulling in data from a webservice and generally after I pull it in once the data will not change. This data would be referenced in different model objects. I am wondering what the best way to cache this in MVC would be. I was thinking a static Dictionary in the model class that would be using this data, but multiple threads could be reading and writing to it. Any suggestions?
Share
I think your idea is pretty good (it’s what I’ve been doing so far anyway).
The only gotcha is that you need to avoid concurrent access to your dictionary, so just make it private and have the accessor methods use a lock for both reading and writing (replace K and V with your own types):
Other alternatives involve using a ConcurrentDictionary (.NET 4 or greater).
This obviously does not deal with cache aging. But it seems to me like you are controlling this on your own.