The Caching cache is used to save and reuse some frequently-used data that generates a relatively high cost. Generally, data is stored in the memory, because reading data from the memory is faster than reading data from a database or other places.
ASP. NET supports caching in two ways:
1. Use the cache API to store arbitrary data;
2. pages that are frequently accessed through the page output cache.
Example:
For an e-commerce website, its directories are generally updated once a week. The site provides a user interface for customers to order products. When a customer browses a directory, the system queries the database through the network, performs various computations, and finally returns results.
The operations to query the directory data from the server are frequent. We know that the data changes once a week. Therefore, the following operations will cause performance loss.
1. Execute ASP. NET Program Generate a query statement for the database.
2. Communicate with the database server over the network.
3. The database server compiles and executes queries (or executes the storage process ).
The Caching mechanism can reduce a lot of such work and improve the performance and scalability of applications. We can cache results so that we can process customer requests statically to improve performance. At the same time, because the resources used to process each request are reduced, the system scalability is also improved.
The cache API is not a completely new concept for ASP developers to store frequently used data in the memory. In ASP, there are two objects to complete it.
Session Object
Application Object
Session is used to save the data shared by a single user among multiple requests. net has some small changes, but these changes are mainly at the application level. For session objects, they are still a set of key-value pairs. The application object is saved in ASP. NET, which is also a set of key-value pairs. In ASP and ASP. NET, we can use the following Code To operate the Application Object
Application ("someinterestingdata") = "example data"
Response. Write (Application ("someinterestingdata ")
We can use the same method to access the session object.
ASP. NET brings a new key and key value object-Cache. In addition to the storage key and key value, the cache object also provides some new features for storing short-term data:
Dependency-when a key is inserted into the cache object, we can set its dependency. When the dependent object changes, the key is deleted. Currently, supported dependent objects include files, other keys, and time.
Automatic expiration-no dependent key value. When the usage frequency is not high, it is automatically deleted.
Supports callback-when a key is deleted, we can get an event in which the key value is updated or the deletion operation is canceled.
Note This When Using Cache objects:
Before using the key value in the cache object, you must check whether the key value exists every time.
Because the key value in the cache object is deleted because of its dependency or low usage frequency, you must check whether the object in the cache exists.
For example, we can use the following code to return dataset.
VB. NET 1 Private Function loaddataset () Function Loaddataset () As Dataset
2 Dim Sqlconnection As Sqlconnection
3 Dim Sqladapater As Sqldatasetcommand
4 Dim Datasetproducts As New Dataset ()
5 Dim Sqldsn As String
6 Dim Sqlselect As String
7
8 " Connection string and select statement
9 Sqldsn = " Server = localhost; uid = sa; Pwd =; database = grocertogo "
10 Sqlselect = " Select * from products "
11
12 " Connect
13 Sqlconnection = New Sqlconnection (sqldsn)
14 Sqladapater = New Sqldatasetcommand (sqlselect, sqlconnection)
15
16 " Fill dataset create product table
17 Sqladapter1.filldataset (datasetproducts, " Products " )
18
19 Return Products
20 End Function
21
C # 1 Private Dataset loaddataset ()
2 {
3 Sqlconnection;
4 Sqldatasetcommand sqladapater;
5 Dataset datasetproducts = New Dataset ();
6 String Sqldsn;
7 String Sqlselect;
8 Sqldsn = " Server = localhost; uid = sa; Pwd =; database = grocertogo " ;
9 Sqlselect = " Select * from products " ;
10 Sqlconnection = New Sqlconnection (sqldsn );
11 Sqladapater = New Sqldatasetcommand (sqlselect, sqlconnection );
12 Sqladapter1.filldataset (datasetproducts, " Products " );
13 Return Products;
14 }
We can easily use the cache object to rewrite this code so that loaddataset () is called only when the dataset does not exist in the cache ().
VB. NET 1 Public Function getproductdata () Function Getproductdata () As Dataset
2 If ( Isnothing (Cache ( " Productdata " )) Then
3 Cache ( " Productdata " ) = Loaddataset ()
4
5 Return Cache ( " Productdata " )
6 End Function
C # 1 Public Dataset getproductdata ()
2 {
3 If (Cache [ " Productdata "] = Null )
4 {
5Cache ["Productdata"] =Loaddataset ()
6}
7 Return Cache [ " Productdata "] ;
8 }
The cache object has many similarities with the Application Object in many places, and the biggest difference is that the cache supports dependency.