The Cache class allows your application to use
data caching. The Cache class works by exposing a
name/value collection where you can store objects that are expensive
to create or frequently used. Its use is analogous to the
System.Web.HttpApplicationState class, although
you use the System.Web.HttpApplicationState to
store information that must be retained for long periods of time and
the Cache class for volatile objects that may
quickly expire and can be dropped automatically when memory declines.
Much like the System.Web.HttpApplicationState
class, the Cache class is globally accessible to
all clients on a single web server, persists for the lifetime of the
ASP.NET application, and has only one instance per application.
Unlike the System.Web.HttpApplicationState class,
the Cache class is intrinsically thread-safe.
You can add items to the cache as you would with other state
collections like System.Web.HttpApplicationState
(for example, Cache["myobject"] = dsData;).
However, using the overloaded Insert( ) method
allows much more control. For example, you can set an absolute
expiration policy in which an item will be removed after a specified
time (Cache.Insert("Data",
dsData, null,
DateTime.Now.AddMinutes(10),
NoSlidingExpiration;) or a sliding expiration
policy in which an item will be removed after a specified interval of
disuse (Cache.Insert("Data",
dsData, Nothing,
NoAbsoluteExpiration,
TimeSpan.FromMinutes(10);). Note that the
NoAbsoluteExpiration and
NoSlidingExpiration fields from the
Cache class allow you to disable the type of
caching you don't want to use. Attempts to retrieve
a removed cached item will return Nothing (or
null).
You can also use a version of the Insert( ) method
with additional parameters for setting cache dependencies (by
supplying an instance of a CacheDependency class),
priorities (by using the CacheItemPriority
enumeration), and a callback (by supplying a
CacheItemRemovedCallback delegate).
The Cache class is available through the built-in
Cache object and shouldn't be
confused with the System.Web.HttpCachePolicy
class, which is used to configure page caching and is available
through the Response.Cache reference.
public sealed class Cache : IEnumerable {
// Public Constructors
public Cache( );
// Public Static Fields
public static readonly DateTime NoAbsoluteExpiration; // =12/31/9999 11:59:59 PM
public static readonly TimeSpan NoSlidingExpiration; // =00:00:00
// Public Instance Properties
public int Count{get; }
public object this[string key]{set; get; }
// Public Instance Methods
public object Add(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration,
TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);
public object Get(string key);
public IDictionaryEnumerator GetEnumerator( );
public void Insert(string key, object value);
public void Insert(string key, object value, CacheDependency dependencies);
public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration,
TimeSpan slidingExpiration);
public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan
slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback);
public object Remove(string key);
}