I am using
to store key/value pairs in my MVC .Net application.
There are 2 main purposes I am using
. One is to store sessions for user ids, and another is to store constants (this is just an example) The keys could theoretically be the same in both cases, so I want some way to separate these 2.
I am thinking of 2 or 3 ways. Which way is superior? Or is there a better alternative?
- Each key in the cache will be prepended by a namespace.
- Using nested dictionaries as keys.
There will be a key "user_sessions" whose value will be a Dictionary that maps ids to the session object. There will be a key "constants" whose value will be a Dictionary.
- Each "namespace" gets its own MemoryCache instance.
The disadvantage with #2 is that when I want to get the value belonging to a user ID, I need to first get the dictionary, then get the value for a key in that dictionary. Which means I need to store the dictionary in memory.
Dictionary<string, string> userSessions = MemoryCache.Default["user_sessions"]
object session = userSessions.get("1");