. NET Server cache and Client Cache

Source: Internet
Author: User

Introduction:

When explaining the cache management mechanism, let me first clarify the next concept: Data Management under IE. Everyone will use different methods to solve how to manage data in IE. Some will mention the use of status management, some mention the Cache Management, here I prefer Cache Management, because I prefer the word "cache. However, status management and Cache Management are different in terms of concept and meaning. Let's discuss the differences between the two in various aspects.

Although Cache Management does not exist in Windows programs, it has been widely used in Web environments. Since HTTP becomes protocol-free, it is very difficult to distinguish two different requests on the Web. How to distinguish so many requests becomes very important. If it is the same request, we can cache the data for all users on the web to access and reduce data duplication for physical loading.

Asp.net provides several methods to cache data on the client and server, but we are often worried about which method to use. Asp.net provides the following three implementation methods:

1: session; 2: Application 3: cache objects. We must be very clear about their advantages so that we can make full use of their advantages in web programs.

Background:

In this article, I will briefly introduce different functions in Cache Management. In web programs, we want to avoid performance problems caused by high concurrency data access, it is necessary for us to cache data on the server so that later accesses can directly call the cached data to play a role in data reuse.

Cache helps us to mention three important aspects of service quality:

Performance:Cache data is reused to avoid repeated physical data loading.
Measurable:After the data is cached, data is loaded from the server.
Practicality:If other systems or databases crash, data can still be obtained from the cache without being affected by local hardware.

 

In a web program, we can cache data, cache pages, and so on. Let's take a look at the differences between the data cache on the server and the client.

 

1. Server cache:

1.1session status management:

Session caches data for everyone. That is to say, the cached data cannot be shared by multiple people at the same time, but is limited to cache data for individual users.
There are three methods to implement status management:
1.11: inproc:
Local data is stored in the aspnet_wp.exe process, and data is lost due to IIS restart.
1.12: StateServer:
Different from inproc, it can be stored on different servers.
1.133: sqlserver:

Its data is stored in the database, and the data will not be lost due to IIS restart.

 

The biggest difference between the last two methods and inproc is that we need to ensure that the cached data is serializable, otherwise it can only be used in the first method. therefore, we need to carefully analyze and select the most suitable method.

 

The following is the code snippet of how the session works:

 

Code
String empnum
= Request. querystring ["empnum"];
If (empnum
! = NULL)
{
String details
= NULL;

If (session ["emp_details"]
= NULL)
{
// Get employee details for employee number passed
String details
= Getemployeedetails (convert. toint32 (empnum ));

Session ["emp_details"]
= Details;
}
Else
{
Details
= Session ["emp_details"];
}

// Send it to the browser
Response. Write (details );
}

 

1.2 ASP. NET application
Asp.net provides another method for saving global variables. The application object is also oriented to all users, and its lifecycle is the same as that of applications, after the application is initialized, it starts to be rebuilt. But its biggest drawback is that there is no relevant data expiration method, and Cache Management is required at this time.

1.3 ASP. NET Cache
Cache is my favorite mechanism, which is why I like to talk about it. It providesKey-ValueFor the corresponding method, the namespace of cache is: system. Web. caching. its lifecycle also depends on the application, but it is not like Session, which is also oriented to all users. Although the cache looks especially like an application, the biggest difference is that it provides a data cache failure control method and data cache dependency management. That is to say, in the cache, we can easily set the expiration time for the cache to expire and delete the cache. We can also operate the cache based on the cache dependency, when the dependency relationship is changed, the cache automatically becomes invalid. This is not what Applicaion can do.

Now let's take a look at how Asp.net supports cache expiration and data cache dependencies.

 

1.31: cache dependency:

As the name implies, the cache will become invalid when the preset dependency changes. Asp.net provides two dependency relationships:
1.311: File Cache dependency: Automatically invalidates the cache when a file on the disk changes.
The following is the instance code:
Object errordata;
// Load errordata from errors. xml
Cachedependency filedependency =
New cachedependency (server. mappath ("errors. xml "));

Cache. insert ("error_info", errordata, filedependency );

 

1.312: Key-value cache dependency:It looks very similar to the File Cache. In addition, this dependency method is different. When multiple cache information is associated with each other, a change in the cache information will cause the failure of other caches. For example, a user information includes: number, name, address, etc. If the user ID changes, the cache becomes invalid. In this case, the basic user information depends on the user ID.
The following is the sample code:
String [] relatedkeys = new string [1];
Relatedkeys [0] = "emp_num ";
Cachedependency keydependency = new cachedependency (null, relatedkeys );
Cache ["emp_num"] = 5435;
Cache. insert ("emp_name", "shubhabrata", keydependency );
Cache. insert ("emp_addr", "Bhubaneswar", keydependency );

Cache. insert ("emp_sal", "5555 USD", keydependency );

 

1.32: Expiration Policy:It automatically expires after a period of time from when a cache is created.
Sample Code:
// Absolute expiration
Cache. insert ("emp_name", "shubhabrata", null,
Datetime. Now. adddays (1), cache. noslidingexpiration );

// Sliding expiration
Cache. insert ("emp_name", "shubhabrata", null,
Cache. noabsoluteexpiration, timespan. fromseconds (60 ));

1.4ASP. NET page output Cache

Sometimes some pages on the web site will not change for a long time. For example, for a recruitment website, the description of the salary is generally not changed, it is generally changed once a month, so the content you see is the same in this month. It is not a perfect solution to cache data on the server. The page output cache can be used here.

The following is the sample code:
<% @ Outputcache duration = "60" varybyparam = "empnum"
Location = "server" %>

 

2: Client Cache:

In the above article, I discussed some data caching methods on the server. However, sometimes we need to cache some data to the client to improve performance. This mechanism can be used to relieve the pressure on the server. However, there may be various security issues in the client cache data. I will briefly describe the related content below:

 

2.1 cookies:Cookies are widely used in Web application development. They can easily access each other on the client and server. However, they have a data size limit of up to 4 kb, it is often used to store small data. At the same time, cookies also support perfect control over invalidation.

The following is the sample code:

If (this. Request. Cookies ["my_name"] = NULL)
{
This. response. Cookies. Add (New httpcookie ("my_name ",
"Shubhabrata Mohanty "));
}
Else
{
This. response. Write (this. Request. Cookies ["my_name"]. value );
}

 

2.2 viewstate: Viewstate is a brand new concept. It is generally used for retaining data in pages or controls for transportation to the server. In ASP, we store data by using a hidden control (hidden fields). viewstate is also used, but it is more secure than a hidden control, all values are processed by hash. If you view the page source code, you will see the existence of viewstate. Generally, viewstate does not need to be used to save big data.

The following is the sample code:
Protected void page_load (Object sender, eventargs E)
{
If (this. viewstate ["my_name"] = NULL)
{
This. viewstate ["my_name"] = "shubhabrata Mohanty ";
}

// Txtname is a Textbox Control
This.txt name. Text = This. viewstate ["my_name"]. tostring ();

}

 

2.3 hide the hidden fields Control: It is the simplest.
The following is the sample code:
<! -- In ASP. NET -->
<Asp: hiddenfield id = "myhiddenfield" value = "shubhabrata"
Runat = "server"/>
<! -- In HTML -->

<Input id = "myhiddenfield" type = "hidden" value = "shubhabrata"/>

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.