How to manage cache in asp.net
As I explain the cache management mechanism, let me first clarify the next idea: data management under IE. Everyone will use different methods to solve how to manage data in IE. Some will mention the use of state management, some mentioned cache management, here I prefer cache management, because I prefer the word "cache". But state management and cache management are different in concept and meaning, let's discuss the differences between the two in different aspects.
Although cache management does not exist in Windows programs, it has been widely used in web environments. Since HTTP became a no protocol, it is very difficult to distinguish two different requests on the web, how to distinguish so many requests becomes very important, if the same request, we can cache the data for all users on the Web Access, reduce data duplication of physical loading.
ASP.net provides several ways to cache data on both the client and server side, but we often get upset about which way to go. ASP.net offers the following three implementations:
1:session;2:application 3:cache objects, we have to be very clear about the advantages between them so that we can take advantage of their advantages to play in the Web program.
Background:
In this article, I will simply cover the different functions in cache management, in the Web application, in order to avoid the high concurrency generated by the data access performance problems, we need to cache the data on the server side, so that subsequent access can directly invoke the cached data, the role of data reuse.
Caching can help us to mention three important aspects of service quality:
Performance: Cache data to achieve data reuse. Avoid duplicate physical data loading.
Scalability: Data caching reduces the load of data from the server side.
Usability: If other systems or databases crash, you can still get data from the cache that is not affected by local hardware.
In a Web program, we can data caching, page caching, and so on, let's look at the data cache on the server side and client differences.
1. Server-Side Caching:
1.1Session State Management:
The session caches data for everyone. That is to say, the cached data cannot be shared by many people at the same time, limited to a single person caching data.
There are three ways to implement state management, respectively:
1.11:inproc:
Its data is stored in the Aspnet_wp.exe process, and the data is lost because of the restart of IIS.
1.12:stateserver:
Unlike InProc, it can be stored on different servers.
1.133:sqlserver:
Its data is stored in the database, and data is not lost because of the restart of IIS.
The biggest difference between the last two methods and the InProc is that we want to make sure that the cached data is serializable, otherwise it can only be used for the first method. To do so, we need to analyze carefully to choose the best way for ourselves.
Here is how to act on the session's code fragment:
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 us with another method of global variable saving, Application object, which is also for all users, its lifecycle and application, and it begins to rebuild when the application is initialized. But its biggest drawback is that there is no relevant data expiration method, at this time will be used to cache management.
1.3 asp.net cache
Cache is my favorite mechanism and that's why I like to say it. It provides a key-value corresponding method, and the cache corresponds to the namespace: System.Web.Caching its lifecycle also relies on the application
Preface, but it is not like session, this is also for all users. Although the cache looks particularly like application, its biggest difference is that it provides data cache failure control methods and data cache dependency management. In other words, we can very easily in the cache in accordance with the pre-set expiration time to allow cache expiration, delete cache, we can also be based on the cache dependency to operate cached, when the dependency of the relationship has changed, at this time the cache will automatically expire. And that's all applicaion can't do.
Now let's look at how the cache expires and the data cache dependencies are supported in the asp.net.
1.31: Cache dependencies:
As the name suggests, it means that the cache will fail when a predetermined dependency change occurs. Two dependencies are provided in asp.net:
1.311: File cache dependencies: Automatically disable cache when a file on disk is changed
Here 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 dependencies: Looks like the file cache is very similar to the difference is that this way of relying on different, when there are multiple cache information related to each other, a cache information changes will cause other cache failure. For example, a user information includes: number, name, address, etc., if the user number changes then the cache fails, in this case, the user's basic information depends on the user number.
Here 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", "5555USD", keydependency);
1.32: Expiration policy: Start from the cache creation, after a period of time automatically expires.
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.4 asp.net page Output cache
Sometimes in the Web site, some pages in a long period of time will not change, such as a recruitment site, it for the description of wages will not often change the text, is generally one months to change, so in these one months, users see the content is the same, All the data caching on the server side is not the perfect solution. This can be cached using page output.
Here is the sample code:
<% @OutputCache duration= "varybyparam=" Empnum "
location= "Server"%>
2: Client cache:
In the previous article, I discussed some data caching methods on the server side, but sometimes we have to cache some data to the client in order to improve performance. This mechanism is used to mitigate server pressure, but there are various security issues with the client caching data, and I'll say a few things about it:
2.1 Cookies:cookies is widely used in the development of Web applications, it can easily access the client and server side, but it has the data size limit, the maximum 4K, all with it is often to save small data. At the same time, the cookie's control over failure is quite perfect.
Here 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 new concept that is typically used in pages or controls to keep data for and service-side traffic. In the ASP, we store the data by using a hidden control (Hidden fields), which is viewstate, except that it is more secure than the hidden control, and all values are hashed. If you view the page source code, you will see the existence of ViewState, general viewstate not to save large data.
Here 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.txtName.Text = this. viewstate["My_name"]. ToString ();
}
2.3 Hidden Controls Hidden fields: It's the simplest, don't say much.
Here is the sample code:
<!--in asp.net-->
<asp:hiddenfield id= "myHiddenField" value= "Shubhabrata"
runat= "Server"/>
<!--in html-->
<input id= "myHiddenField" type= "hidden" value= "Shubhabrata"/>