Introduction to [asp] Cache

Source: Internet
Author: User

Write in front

Using caching for some frequently accessed data that is rarely changed can improve performance. Caching is a space-for-time technology, the straightforward point is that the first access to read data from the database, and then the data exist in a place, such as memory, hard disk, re-access time, no longer read from the database, but from memory or hard disk to read data, can improve access Speed.

Cache

Cache is a space for the use of time technology, the popular point is to put the resulting data in memory or hard disk, during this time the server does not read the database, or the real data source, but read your existing in-memory Data.

Role: caching can effectively relieve the pressure on the Database.

An example

Let's look at a simple example and then have a visual understanding of the Cache.

  public Partial classCacheIndex:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs E) {            if(!IsPostBack) {DateTime DT=datetime.now; //Set if the cache does not have a value of currenttime, otherwise the value in the output cache                if(cache["currenttime"] ==NULL)                {                    //cache the current timecache["currenttime"] =dt; //Output ValueResponse.Write ("time of First read:"+dt. ToString ("YYYY-MM-DD HH:mm:ss")); }                Else                {                    //otherwise, The value in the output cacheDT = (DateTime) cache["currenttime"]; //Output ValueResponse.Write ("time to read from Cache:"+dt. ToString ("YYYY-MM-DD HH:mm:ss")); }                           }        }    }

The first time the page is accessed, the current time is read, then it is written to the cache, and the refreshed page you will find, after reading the value from the cached Data. If you want to empty the cache, you can set it to Null.

This way of setting the cache is the most intuitive and easiest way.

So can we set an expiration time for it? Of course it is possible to look at an example like This:

   public Partial classCacheIndex:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs E) {            if(!IsPostBack) {DateTime DT=datetime.now; //Set if the cache does not have a value of currenttime, otherwise the value in the output cache                if(cache["currenttime"] ==NULL)                {                    //cache the current time                    /*@ First Parameter: cache key @ Second Parameter: cached value @ Third Parameter: cache dependency, can be null, and no dependency is set for it.                    @ Fourth Parameter: cache time, 30 seconds @ Fifth Parameter: cache Expiration Time type, here is the absolute expiration time, 30 seconds after each refresh page cache Expires. */Cache.Insert ("currenttime"DtNULL, DateTime.Now.AddSeconds ( -), System.Web.Caching.Cache.NoSlidingExpiration); //Output ValueResponse.Write ("time of First read:"+ DT. ToString ("YYYY-MM-DD HH:mm:ss")); }                Else                {                    //otherwise, The value in the output cacheDT = (DateTime) cache["currenttime"]; //Output ValueResponse.Write ("time to read from Cache:"+ DT. ToString ("YYYY-MM-DD HH:mm:ss")); }            }        }    }

The absoluteexpiration and slidingexpiration parameters cannot be set at the same time. If you want the cache entry to expire at a specific time, you can set the absoluteexpiration parameter to a specific time and set the slidingexpiration (sliding expiration) parameter to Noslidingexpiration.

If you want the cache entry to expire after a period of time after the last access to the item, set the slidingexpiration parameter to the expiration interval and set the absoluteexpiration (absolute expiration Time) parameter to Noabsoluteexpiration.

Summarize

Here is a brief introduction to the concept of caching, as well as an example of caching. This requires a good sense of both the expiration time in the cache, the absolute expiration time, and the sliding expiration Time.

Introduction to [asp] 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.