iVikashJha
iVikashJha

Reputation: 169

Is this recommended to use private static Dictionary<string, string> for caching?

I am using a private and static, Dictionary to cache the menu name.

private static Dictionary<string, string> menuItemCache = new Dictionary<string, string>();

and the cache is updated by my [POST] action method for any new menu item added. And the dictionary is holding menu names all the time, and are being consumed by other [GET] action methods.

My question is about using static dictionary instead of any other caching technique, if it is recommended to do so.

Upvotes: 1

Views: 972

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156978

You should be very careful with static variables. Static variables are shared across all sessions. If you intend to keep a cache per-user, you should use the Session object instead.

On itself, there is nothing wrong in keeping a cache stored in a dictionary. The dictionary has fast lookups, so when using a dictionary over a list you will probably benefit from that performance-wise.

Upvotes: 4

Related Questions