ASP. NET cache Technology (Httpruntime.cache)

Source: Internet
Author: User

A. Cache:

    • 5 Levels of Cache
      1. Level 1 is a network-level cache, slow presence of browsers, CDN and proxy servers (for example: Each help page is cached, the code to access a page is very simple)
      2. Level 2 is done by the. NET Framework Httpruntime.cache in memory for each server.
      3. Level 3 Redis, a distributed memory key-value store that shares cache entries on multiple servers supporting the same site.
      4. Level 4 SQL Server Cache, entire database, all data is put into memory.
      5. Class 5 SSD. Usually only after the SQL Server warms up.

two. The Httpruntime.cache cache is mainly described below :

Significance:

The data is placed in the cache and can be obtained directly from the cache for a specified period of time, avoiding stress on the database, etc.

Set and read:

Settings:

//        //Summary://inserts an object with a dependency and expiration policy into the System.Web.Caching.Cache. //        //Parameters://Key://The cache key used to reference the object. //        //Value://the object to insert in the cache. //        //dependencies://The file dependency or cache key dependency of the inserted object. When any dependency changes, the object is not valid and is removed from the cache.        If there are no dependencies, this parameter contains null. //        //absoluteexpiration://The time at which the inserted object will expire and be removed from the cache. To avoid possible local time issues, such as changing from a standard time to daylight savings, use the System.DateTime.UtcNow//instead of System.DateTime.Now as this parameter value.        If absolute expiration is used, the slidingexpiration parameter must be System.Web.Caching.Cache.NoSlidingExpiration. //        //slidingexpiration://the time interval between when the inserted object was last accessed and when the object expires.        If the value is equivalent to 20 minutes, the object expires and is removed from the cache after the last 20 minutes of access. //If you use adjustable expiration, the absoluteexpiration parameter must be System.Web.Caching.Cache.NoAbsoluteExpiration. //        //Exception://System.ArgumentNullException://the key or value parameter is null. //        //System.ArgumentOutOfRangeException://set the slidingexpiration parameter to an equivalent value that is less than TimeSpan.Zero or greater than one year. //        //System.ArgumentException://set the absoluteexpiration and slidingexpiration parameters for the items you want to add to the Cache.          Public voidInsert (stringKeyObjectValue, cachedependency dependencies, DateTime absoluteexpiration, TimeSpan slidingexpiration);

Read:

httpruntime.cache["Name"]

Three. Case study:

Code:

///The/cache is globally shared         Public voidProcessRequest (HttpContext context) {context. Response.ContentType="Text/plain"; DataTable DT= (DataTable) httpruntime.cache["Persons"];//first, take the value from the cache! Returns a value of type Object            if(dt = =NULL)//Access database read value without value in cache{DataTable table= Sqlhelper.executequery ("Select *from person"); HttpRuntime.Cache.Insert ("Persons", table,NULL, DateTime.Now.AddMinutes (1), TimeSpan.Zero);//The value to be queried is stored in the cacheContext. Response.Write (table. Rows.Count);//test the output for the                return; } context. Response.Write (dt.        Rows.Count); }

Execution process:

First time visit:

Second visit:

ASP. NET cache Technology (Httpruntime.cache)

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.