The System. Web. Caching namespace provides classes for Caching common data on servers. This namespace includes a Cache class, which is a dictionary in which you can store any data objects, such as hash tables and datasets. It also provides invalid functions for these objects and provides you with methods to add and remove these objects. You can also add objects dependent on other files or Cache items, and execute a callback to notify the application when the objects are removed from the Cache object.
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; public partial class Default4: System. web. UI. page {// <summary> /// obtain the Cache object Value of the specified CacheKey in the current application. /// </summary> /// <param name = "CacheKey"> Index key value </param> /// <returns> returns the cache object </returns> public static object GetCache (string CacheKey) {System. web. caching. cache objCache = HttpRuntime. cache; return objCache [CacheKey];} /// <summary> /// set the Cache object Value of the specified CacheKey in the current application. /// </summary> /// <param name = "CacheKey"> index key value </param> /// <param name = "objObject"> cache object </param> public static void SetCache (string CacheKey, object objObject) {System. web. caching. cache objCache = HttpRuntime. cache; objCache. insert (CacheKey, objObject) ;}/// <summary> /// set the Cache object Value of the CacheKey specified by the current application /// </Summary> /// <param name = "CacheKey"> index key value </param> /// <param name = "objObject"> cache object </param>/ // <param name = "absoluteExpiration"> absolute expiration time </param> // <param name = "slidingExpiration"> the last time you access the inserted object and when the object expires time Interval </param> public static void SetCache (string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration) {System. web. caching. cache objCache = HttpRuntime. cache; objCache. Insert (CacheKey, objObject, null, absoluteExpiration, expiration);} protected void Page_Load (object sender, EventArgs e) {string CacheKey = "cachetest"; object objModel = GetCache (CacheKey ); // obtain if (objModel = null) from the cache // No {objModel = DateTime in the cache. now; // cache the current time if (objModel! = Null) {int CacheTime = 30; // The cache time is 30 seconds. SetCache (CacheKey, objModel, DateTime. now. addSeconds (CacheTime), TimeSpan. zero); // write cache} Label1.Text = objModel. toString ();}}