简体   繁体   中英

Best way to cache store a dictionary in c#

I am writing out a navigation structure of categories. Each category has an ID, that is located in a 3rd party search tool. At either app start, or at various intervals, I need to get the caetegories and their id's, and store them in a dictionary (or some other way) so that I can access them for link rewriting from id to name.

Store the Dictionary in a XML file using a format that looks something like this and read it when you need to do your re-writing

<nodes>
<node id="1">Some Nav Data</node>
</nodes>

You then have 2 options , write a separate process that will update the file at specific intervals or your current process can update the file after a set interval.

If you feel that loading and parsing XML is overkill think about serializing a class to disk that contains a simple hash table or dictionary.

How many settins are you talking about? Why do you need a cache rather than just using an in-memory dictionary with a background thread to reload it periodically?

If you need a cache, you could look at the caching application block in EntLib. You can set an expiry interval and then handle an even on expiry to repopulate the cache. But frankly that sounds overblown for your requirements.

Caching is a super big topic! You could use a local cache if you are in a web project. You could use HttpContext.Current.Cache if you are in a windows environment (though you don't really need cache in a windows app). If you are spanning more than one web server then you might look to the power of MemCached or MemCached Win32. If you don't mind using new stuff you could also take a look at Velocity (which is MS's answer to MemCache's superiority for the past several years in the caching arena).

Here are some SO posts that might interest you:

System.Web.Caching vs. Enterprise Library Caching Block

Running a Asp.net website with MS SQL server - When should i worry about scalability?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM