Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net - Caching vs Static Variable for storing a Dictionary

Tags:

I am building a web-store with many departments and categories. They are stored in our database and accessed often.

We are using URL rewriting so almost every request within the store generates a lookup. We also need to iterate over the data frequently to generate menus for the main store and the department pages.

This information will not change often so I'm thinking that I should load the database into a dictionary to speed up the information retrieval.

I know the standard practice is to load data into the application cache, however i assume that there is some level of serialization that occurs during caching, and for a large data-structure I'm thinking the overhead would be significant.

My impulse on this is to put the dictionary in a static variable in one of the related classes. I would however like to get some input input on this. Am I right in thinking that this method would be faster? Is it horrible practice? Is there a better way that I'm missing?

I can't seem to find much information on this and I'd really appreciate any information that you can share. Thanks!

like image 406
Kelly Robins Avatar asked Aug 20 '09 19:08

Kelly Robins


People also ask

Are static variables cached?

Static data will cache exactly the same as any other data.

Where does asp net store cache memory?

Cache is stored in web server memory.

What is asp net caching?

Caching in ASP.Net Output Caching : Output cache stores a copy of the finally rendered HTML pages or part of pages sent to the client. When the next client requests for this page, instead of regenerating the page, a cached copy of the page is sent, thus saving time.

What is the use of cache in C#?

It is a type of memory that is relatively small but can be accessed very quickly. It essentially stores information that is likely to be used again. For example, web browsers typically use a cache to make web pages load faster by storing a copy of the webpage files locally, such as on your local computer.


1 Answers

The Application and Cache collections do not serialize the objects you pass into them, they store the actual reference. Retrieving an object from Cache will not be an expensive operation, no matter how large the object is. Always stick with the Cache objects unless you have a very good reason not to, its just good practice.

The only other thing worth mentioning is to make sure you think about multithreaded access to this collection. You're going to end up with some serious issues very quickly if you don't lock properly

like image 86
LorenVS Avatar answered Sep 22 '22 11:09

LorenVS