views:

958

answers:

3

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!

+2  A: 

memcached is your friend! (but could be overkill if you're not scaling out)

Any idea how large your dictionary would be in application cache? I'd be tempted to recommend that as a good first option.

davewasthere
I'm looking at a few dozen departments and a few hundred categories along with associated information... so size wise it shouldn't be too bad. My only hesitation with using database level caching is that with every call I still have to convert the objects returned by the database into their associated types. There is also some associated data like the url that I was hoping to precompute. Given that I'll frequently need to get long lists of these items I was thinking I should cache further up the line. It is still definitely a good option and I really appreciate the info!
apocalypse9
+8  A: 

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

LorenVS
Thank you very much- That is precisely what I needed to know.
apocalypse9
glad to help :)
LorenVS
Just make sure that, if you're using the Application or Cache, they are per machine. If u have multiple machines (eg. web farm) then you're in trouble .. and u'll need to use a scalable caching solution, such as Microsoft Velocity or MemCacheD (the 'D' is for Daemon).
Pure.Krome
Thanks a lot, though I'd to check the cache serialization issue myself and it appears that you are absolutely right as suggested here: http://www.west-wind.com/Weblog/posts/1214.aspx
Eran Betzalel
+2  A: 

Well, I don't think it's so much work to rewrite code to use a static field instead of application cache if there's need to do so. I'd personally use the cache first. There's no need for premature optimization, have you measured the performance? It may behave just right with the application cache object. Maybe it even works well with db queries? :)

So, my answer is - use the cache and see how it works.

Pawel Krakowiak