Several ways to globally cache in ASP.

Source: Internet
Author: User

 Public classStaticcachetest {Private Staticidictionary<string,Object>_dic; Private Static ObjectLocker =New Object(); Private Staticidictionary<string,Object>Cacheddic {Get            {                if(_dic = =NULL)                {                    Lock(locker) {if(_dic = =NULL) {_dic=Newdictionary<string,Object>(); }                    }                }                return_dic; }        }         Public Static ObjectGetObject (stringkey) {            returnCacheddic[key]; }         Public Static voidSetObject (stringKeyObjectobj) {            Lock(Locker) {Cacheddic[key]=obj; }        }    }     Public Partial class_default:system.web.ui.page {Private Const stringKEY ="currenttime"; protected voidPage_Load (Objectsender, EventArgs e) {String Curtime=System.DateTime.Now.ToString (); if(Httpcontext.current.application[key] = =NULL) {HttpContext.Current.Application.Lock (); Httpcontext.current.application[key]=Curtime;            HttpContext.Current.Application.UnLock (); }            if(Httpcontext.current.cache[key] = =NULL) {HttpContext.Current.Cache.Insert (KEY, curtime); }            if(Staticcachetest.getobject (KEY) = =NULL)//is essentially the way Httpruntime.cache is implemented.{staticcachetest.setobject (KEY, curtime); }            if(Httpruntime.cache[key] = =NULL) {HttpRuntime.Cache.Insert (KEY, curtime); //Httpruntime.cache is the same object as HttpContext.Current.Cache, it is recommended to use Httpruntime.cache} TextBox1.Text=Httpcontext.current.application[key].            ToString (); TextBox2.Text=Httpcontext.current.cache[key].            ToString (); TextBox3.Text=Staticcachetest.getobject (KEY).        ToString (); }    }
    1. HttpContext.Current.Application: The entire application can be shared, of course, when the storage should be locked.
    2. Httpruntime.cache and HttpContext.Current.Cache: The two are in fact the same object, the difference lies in the implementation of HttpContext and HttpRuntime. HttpContext must be used in the context of ASP. HttpRuntime can be used in any context, for example, httpruntime can be used as a cache in a console program.
    3. Static variables: Essentially the same as the Httpruntime.cache implementation principle (the cache is also a static variable).

The 1th and 3rd are only suitable for storing small amounts of data (less than 1M), otherwise there will be a loss in performance. Httpruntime.cache is enhanced in terms of efficiency and functionality (such as cache dependencies, expiration policies, etc.), so it should be used as much as possible in real-world applications.

Several ways to globally cache in ASP.

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.