Use cache to Improve website Performance (Caching to Improve the Performance of Your Webs)

Source: Internet
Author: User
Tags website performance

You can increase the access speed of your site, so you need to use cache. Generally, we can use cache to process frequently searched results or infrequently changed data results.
You will learn:
<! -- [If! SupportLists] --> tables <! -- [Endif] --> How to Use cache to speed up website response.
Functions described in this chapter:
<! -- [If! SupportLists] --> tables <! -- [Endif] --> WebCache helper
 
Cache to improve website response
When people request pages on your site, the Web server will do some work to execute the request. For some pages with server-side code execution, the Web server may execute the task for a long time. For example, retrieving data from a database. These tasks do not take a long time to obtain a small number of requests. However, if there is a large amount of traffic on the Internet, when all users make independent requests, this will cause the Web server to increase a lot of work and slow the execution speed. This will ultimately affect the performance of the website.
 
In this case, one of the ways to improve the performance of your website is to cache data. If your website responds to the same number of requests, and the data does not need to be changed for the individual, it is not time sensitive. Therefore, we can cache the data. In each request, the data is not re-computed on the Web server, but stored in the cache for the first access. In the next request, only obtain the data in the cache.
 
In general, your cached information does not change frequently. When you put the information in the cache, it will be stored in the memory on the Web server. You can specify how long it should be cached, from seconds to days. When the cache period expires, this information is automatically deleted from the cache.
 
Note that the items in the cache have not expired, but may be deleted for other reasons. For example, a Web server may temporarily run low-level memory. One of its memory recovery methods is to remove the cache item. As you can see, even if you put the cached information, you must check to ensure that it still exists during your use.
 
Imagine that your website has a page showing the current temperature and weather forecast. To obtain this type of information, you may send a request to an external service. This information does not need to be significantly changed (for example, within two hours) because it takes time and bandwidth for external request services, it is a good cache project.
 
ASP. NET includes WebCache helper, which makes it easy for you to add the cache function to a website and add the data to the cache. In this process, you will create a page to cache the current time. Of course, this is not a real example, because the current time does not change frequently and complex computing is not required. However, this is a good way to describe that the cache is working.
<! -- [If! SupportLists] --> 1. <! -- [Endif] --> Add a new website named WebCache. cshtml.
<! -- [If! SupportLists] --> 2. <! -- [Endif] --> Add the following code and tag to the page:
@{
Var cacheItemKey = "Time ";
Var cacheHit = true;
Var time = WebCache. Get (cacheItemKey );

If (time = null ){
CacheHit = false;
}

If (cacheHit = false ){
Time = @ DateTime. Now;
WebCache. Set (cacheItemKey, time, 1, false );
}
}
<! DOCTYPE html>
<Html>
<Head>
<Title> WebCache Helper example </title>
</Head>
<Body>
<Div>
@ If (cacheHit ){
@: Found the time data in the cache.
} Else {
@: Did not find the time data in the cache.
}
</Div>
<Div>
The time on the page is saved as @ time.
</Div>
</Body>
</Html>
 
In the cacheItemKey code example, a cache item is defined using time. When you cache data, you need to define a name for the cache. The cache exists independently throughout the website.
 
The code first reads the value in the time cache. If the returned value is not empty, the Code obtains the time cache from the cache project and saves it to the variable time.
 
However, if the cache entry does not exist (that is, it is empty), the Code sets the value of time to the current time, adds it to the cache, and sets a one-minute expiration time. If the page request is not performed within one minute, the items in the cache will be discarded. (The default expiration time for cache items is 20 minutes ).
 
This Code indicates that you should always cache data when using it. Before getting a new cache, always check whether the WebCache. Get method returns a null value. Keep in mind that cache entries may have expired or may have been deleted for other reasons, so it is never guaranteed that any given project will always exist in the cache.
 
<! -- [If! SupportLists] --> 3. <! -- [Endif] --> RUN WebCache. cshtml in the browser. (Make sure that the page is in the selected workspace and then run it .) When you request the page for the first time, the time data is not in the cache, and the Code adds the time value to the cache.

 
<! -- [If! SupportLists] --> 4. <! -- [Endif] --> refresh WebCache. cshtml in the browser. This time, the time data is obtained from the cache. Please note that the time has not changed since you last browsed the web page.

 
<! -- [If! SupportLists] --> 5. <! -- [Endif] --> wait for one minute and then refresh the page. It indicates that the time data is not found in the cache, and the update time is added to the cache item.

 
From the technical column of Zhang Jian

Related Article

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.