How to: Add data to the Application Cache programmatically

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

MGR: the Intelogist

About MGR: the Intelogist

SharePoint Server developer, turned Sitefinity developer, turned Angular developer, turned SharePoint Online consultant, turned Unily consultant, turned O365 consultant... Never a dull moment!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.