Use Caching in ASP. NET

Source: Internet
Author: User

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: storing arbitrary data through the Cache API and outputting frequently accessed pages through the page. Let's look at an 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 the ASP. NET program to generate a query statement for the database.
2. Communicate with the database server over the network.
3. database servers compile and execute queries or execute storage processes ).

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 dependencies. 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.
When using a Cache object, you must note this: 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.

Private Function LoadDataSet () As DataSet
Dim sqlConnection As SQLConnection
Dim sqlAdapater As SQLDataSetCommand
Dim datasetProducts As New DataSet ()
Dim sqlDSN As String
Dim sqlSelect As String
"Connection String and Select statement
SqlDSN = "server = localhost; uid = sa; pwd =; database = grocertogo"
SqlSelect = "Select * From Products"
"Connect
SqlConnection = new SQLConnection (sqlDSN)
SqlAdapater = new SQLDataSetCommand (sqlSelect, sqlConnection)
"Fill dataset create product table
SqlAdapter1.FillDataSet (datasetProducts, "products ")
Return products
End Function
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 ().
Public Function GetProductData () As DataSet
If (IsNothing (Cache ("ProductData") Then
Cache ("ProductData") = LoadDataSet ()
Return Cache ("ProductData ")
End Function
The Cache object has many similarities with the Application Object in many places, and the biggest difference is that the Cache supports dependency.

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.