Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# MemoryCache for 2 different types of keys?

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?

  1. Each key in the cache will be prepended by a namespace.

    "user_session:1", "user_session:2"
    "constants:1", "constants:2"

  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.

  3. 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");
like image 849
Henley Avatar asked May 01 '13 18:05

Henley


People also ask

What C is used for?

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 ...

Is C language easy?

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 in C language?

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.

What is the full name of C?

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.


2 Answers

Go for option #3!

  • For the programmer it is easier to access.
  • It is the fastest solutions (see comments on the other solutions)
  • It is the most memory efficient solution (see comments on the other solutions)

Option #2:

  • You said it yourself.
  • If the cache decides to remove a key, a whole dictionary is removed, resulting in more reloads of the values.

Option #1:

  • You do not have to concatinate string (performance and memeory)
  • Longer key names produce longer compare times.
  • Adding items will be slower because it contains twice as much keys.
like image 109
Martin Mulder Avatar answered Nov 04 '22 05:11

Martin Mulder


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.

like image 32
Mike Johnson Avatar answered Nov 04 '22 05:11

Mike Johnson