Groups | Blog | Home
all groups > asp.net caching > january 2005 >

asp.net caching : Caching vs. Static member variables


Steve James
1/15/2005 9:05:50 PM
This may be a stupid question but...

What's the difference between using the ASP.NET Cache object and a class
that stores data in a static object. For example, the class below contains a
hashtable declared statically. This hashtable is accessible by all objects
that are part of the application.

public class MyCache

{

private static Hashtable hashtable = new Hashtable();

public MyCache()

{ }

public static Hashtable GetHashtable()

{

return hashtable;

}

}



The data being accessible by code such as:

string s = MyCache.GetHashtable()[ "CacheValue" ] as string;

s = s == null ? String.Empty : s;

this.TextBox1.Text = s;



Surely this provides the same functionality as the built-in Cache object
and, dare I say it, prefereable if you're wanting to write as environment
agnostic component as possible i.e. the class will behave the same
regardless of whether it is being called by a web/windows/console app.

Any comments and/or protestations would be welcomed.

Steve

Joerg Jooss
1/16/2005 1:35:01 AM
[quoted text, click to view]

There are quite a number of differences here:

-- The Cache is thread-safe (uses ReaderWriterLocks).

-- Cache entries can expire, can have scavenging priorities, can have
dependencies which may force them to be removed from the Cache, and the
Cache notifies you in case an entry is removed.

-- The Cache automatically scavenges entries if their lifetime has
expired or memory is low, thus is somewhat self-tuning.


Cheers,
--
http://www.joergjooss.de
AddThis Social Bookmark Button