Cache vs Application variable
-
Can someone please me out? I got a translatable website where all the translations are currently stored in a DB. I have loaded them into a Dictionaries ect so I need to do is just access them. What are the implications or advantages of storing it in the cache and application variable respectively? I can't seem to see when is it appropriate to use either? Thanks in advance
-
Can someone please me out? I got a translatable website where all the translations are currently stored in a DB. I have loaded them into a Dictionaries ect so I need to do is just access them. What are the implications or advantages of storing it in the cache and application variable respectively? I can't seem to see when is it appropriate to use either? Thanks in advance
The Applicaton state (i.e. Application["myVariable"]) and Application Cache (System.Web.HttpContext.Current.Cache.Add(...) in the System.Web.Caching namespace) are similar in purpose. That being that they cache non user specific data in memory on a web server. The app cache however, offers the following advantages: 1. You can set an expiration policy (sliding, absolute) that dictates when your item will be removed from the memory space. This can be useful with sliding expiration to not hold large items in the cache after they haven't been accessed in say 10 minutes. The absolute is useful if you have mostly static data you want to cache but say it gets updated in its source periodically. You can set the absolute expiration to flush it out of your web app. Coupling that idea with #2: 2. You can set an expiration delegate that will get called when the item is removed from the cache. This can be useful to refresh a stale cache in the case of absolute expiration. Say you have a set of items you offer for sale. You might choose to cache some part of that set. If you realize that your set only changes say in the middle of the night, you might set the absolute expiration accordingly and string an expiration delegate onto it. Then when the item is removed from the cache, you can run some code via the delegate to build a new, updated set and cache that. So, all in all, app cache lets you be less of a memory hog by controlling when your item is removed. It also allows some interesting ways to keep the cache in sync in certain situations.