There’s two ways to add data to the Application Cache. Notice that this is different to the Session cache in that it will be common to all visitors of the web page. Session cache only refers to a specific client/visitor.
The easiest way to add data to the Application Cache is to add a reference to “System.Web.Caching” and then
1. add the desired data to the cache directly like so:
HttpRuntime.Cache[string key] = value;
(where “key” is the name by which you will access the value/object)
2. Another more complex way that will allow you to also set expiry/duration is like this:
HttpRuntime.Cache.Insert(string key, object value, CacheDependancy dependancy, DateTime absoluteExpiration, TimeSpan timeToLapseSinceSomeoneLastAccessedTheCacheBeforeMakingItExpire);
where
“key” is again the name by which you will access the value in the cache
“CacheDependancy” defines a file or another key in the cache on which the added key depends on -if the dependancy changes, the key added here immediately expires
“absoluteExpiration” is an absolute DateTime when the key/value will expire
“timeToLapseSinceSomeoneLastAccessedTheCacheBeforeMakingItExpire” is a period of time that will have to lapse since someone last accessed the key in the cache, before the value/object expires