. Net Cache Mechanism

Source: Internet
Author: User

1. web applications, some called status management, some called Cache Management, and cache mechanism.

2. server-side cache and client-side cache.

3. Client Cache:

3.1 Cookies:The client sends a request to the server every time it sends the request. When the server responds, it also sends it back to the client. Because it limits the number of bytes (4096 bytes), it can only cache relatively small data. It can use an expiration policy to make it expire after a specific period of time.

if (this.Request.Cookies["UserName"] == null) {     this.Response.Cookies.Add(new HttpCookie("UserName",  "aaa")); } else {     this.Response.Write(this.Request.Cookies["UserName"].Value); }

3.2ViewState: Page-related data and controls are stored in ViewState, while ViewState in ASP. NET is the internal implementation of hidden controls. functions are similar.Hidden fields

3.3Page Cache: Browser Cache and Proxy Cache. The two settings are also very convenient. Cache-Control: Set to public, and private is only cached in the browser.

<% @ OutputCache Duration = "1800" VaryByParam = "*" %>

<% @ OutputCache Duration = "3600" VaryByParam = "id" %>

In ASP. NET, each access to a page must be reloaded from the server, rather than directly reading The Last Temporary page from the IE cache, as follows:

protected void Page_Load(object sender, EventArgs e)    {        Response.Buffer = true;        Response.ExpiresAbsolute = DateTime.Now.AddMinutes(-1);        Response.Expires = 0;        Response.CacheControl = "no-cache";    }

Proxy Cache problems mainly occur in three aspects:
1. Security
2. Regional Problems
3. Content update Problems

First, this is the biggest security problem: Because the proxy caches the data of the entire page based on the URL, including the response header, it contains cookie information. The problem is
Here, if user A logs on and the proxy caches the response to the page, then when user B requests the same page, the whole response will be sent to user B. At this time, user B can enter
Let's go.
There is no way to solve this problem, but there is a way to avoid: Set proxy cache for some pages that can be accessed without user logon, especially those pages with static content; do not set a page that involves user verification.

Second, regional issues,
Because the proxy caches the entire page and matches it according to the URL. If we are Chinese, visit a page, such.

What should I do? Although the method is little known, it is still simple.
Set the header information: Accept-Language
When sending a Chinese response, set it to: Accept-Language: zh-cn, then the Proxy Cache caches the content of this version.
If the request is in English, the Accept-Language information of the request sent by the browser of the old Midea is en-US. The agent does not check the content of this version, then the request will be sent to our server.

The final result is that the proxy caches content of different versions of the same page.

Content update: Because the proxy caches all the data on the page, including images, js, and so on, even if the images on our page are updated, the client cannot know, unless the cache expires,

Aa. js? V = 2.1

Bb.css? V = 1.1

4. Server cache:Session, the Application will not be said, there are two main types of commonly used:

4.1:. net Cache: (host Cache) in. Net Framework, application Cache is implemented through the System. Web. Caching. Cache class,

When IIS is frequently recycled, the. net cache will be lost, resulting in frequent access to the database, affecting performance. Therefore, it must be used in conjunction with the MemCache cache.

Regarding the relationship between the net cache and IIS, if the cache occupies a certain amount of memory, the resources used will be recycled by the application pool,

Reference http://www.cnblogs.com/yanyangtian/archive/2012/05/02/2478659.html

4.2: MemCache cache: (kernel cache) first install and run MemCache. The MemCache process is displayed in the task manager. Then use. Currently, it is mainly used to cache database data.

Data stored in memcached is stored in the memory storage space built in memcached. Because the data only exists in the memory, restarting memcached and the operating system will cause all data to disappear.

This cache is distributed, that is, it allows multiple users on different hosts to access the cache system at the same time. This method not only solves the disadvantages that shared memory can only be a single machine, at the same time, it also solves the pressure on database query. The biggest advantage is that it increases the speed of data access. Memcached is used to reduce the database load and improve the access speed in dynamic applications, however, it is usually used to accelerate Web applications and reduce the load on the database. Memcached can also be used in other places, such as distributed databases and distributed computing.

The web. config configuration is as follows:

<! -- MemCache configuration (Cache pool name) -->
<Add key = "MemPoolName" value = "poolName"/>
<! -- MemCache configuration (Server LIST) -->
<Add key = "MemServer" value = "Maid: 11211"/>
<! -- MemCache configuration (whether to enable 1) -->
<Add key = "MemEnabledCache" value = "1"/>

 

 

 

 

 

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.