Thread: Caching
View Single Post
  #5 (permalink)  
Old May 30th, 2010, 09:05 PM
chroniclemaster1 chroniclemaster1 is offline
Friend of Wrox
Points: 1,749, Level: 16
Points: 1,749, Level: 16 Points: 1,749, Level: 16 Points: 1,749, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2007
Location: San Diego, CA, USA.
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

Quote:
Originally Posted by Imar View Post
No, that's not true. It might be there, it might not.
Stuff you store in the cache isn't necessarily there forever. You can put restrictions on its lifetime or ASP.NET may decide to delete it. You should always check if it's there.

Yes, in this case it stays there until the application is restarted. Indeed, static fields are tied to the application domain so anything you store in a static field is available during the lifetime of the app domain.

The nice thing about the cache is that it can do memory management for you. E.g. stuff that consumes a lot of money and / or that is infrequently used can be thrown out of the cache, something you cannot accomplish with static fields.
Thank you for the sample cache code. :)

And for the first time I really see a value in static classes. So the static fields store information for the entire app, and the static functions are a good option if you have some kind of frequently used cross application functions that you need to run. It now makes sense why I've seen guys choose to build links from a static function. It's one piece of code that all pages are going to be running multiple times.

So static fields and the Cache[] object are simply two different caching strategies. Moreover it looks like the Cache[] is a better choice for caching things you only want for a limited amount of time (because a static field won't recycle them), and for things you don't need all the time. On the other hand, a static field may be a better choice if you need to access the information a lot over a long period of time. Further, minimizing the amount of information stored in the cache is important, but it's much more critical for a static field because it's going to be around until the application recycles. That's an advantage of the Cache[], because it will recycle things for you, if it determines the space is better used for something else.
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.