I am using MemoryCache
to store key/value pairs in my MVC .Net application.
There are 2 main purposes I am using MemoryCache
. 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.
"user_session:1", "user_session:2"
"constants:1", "constants:2"
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.
IE:
Dictionary<string, string> userSessions = MemoryCache.Default["user_sessions"]
object session = userSessions.get("1");
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Go for option #3!
Option #2:
Option #1:
I'm not sure what your actual implementation is but be cautious of using sessions in this way with the MVC framework. User identifiers are better left in cookies. Either way, I can see uses to go this route as well on occasion.
I would avoid using dictionaries in the cache in that way. I don't know what type of memory allocation your looking at but it could get real ugly if the server has high traffic and multiple dictionaries. As mentioned above in a comment, you would also have to worry about concurrency issues with dictionaries in that way as well.
The better approach from the options you provided would be to give each namespace it's own instance of the cache.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With