Asp.net application data cache

Source: Internet
Author: User

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 ();}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.