Store Dictionary object in ApplicationState Vs putting it in Web.config
I need to store a Dictionary in my asp .net application. This dictionary is basically a straight mapping of keys (strings) to values (strings). During the life of the Application the dictionary will not change and will contain about 10 elements.
I will be iterating over possibly thousands of rows returned from a stored procedure and obtaining corresponding values from this dictionary.
What would be the best approach to tackle this? Here are the 2 ideas I have on implementing it:
- Instantiate and initialize the dictionary during Application_Start and storing it in the ApplicationState object.
Or ...
- Do what they propose here: How do I store a dictionary object in my web.config file?
I like option 2 since adding a new value in the future is just a matter of adding an entry in Web.Config without any code changes whatsoever but I'm concerned about performance since I will have t开发者_如何学Pythono obtain a value for every single row returned from the stored procedure.
I wonder if these lookups in web.config are somehow optimized/cached?
If you're using ConfigurationManager
to access AppSettings
values, these are cached after the 1st time they're accessed. So the first time you access ConfigurationManager.AppSettings["MyValue"]
, it's read from disk, after that it's read from cache.
Why not mix it up a bit. On Application_Start load your web.config values into a dictionary. Then you can still configure the list without code changes, and you code can easily reference the dictionary
EDIT: Of course, the disadvantage here is that the list will only refresh when you restart you website (i.e. cause Application_Start to fire again) where as a straight config file will allow more instant results when you modify the web.config file. Perhaps that is a reason to go for the web.config option
精彩评论