asp.net caching cache, is to save some of the common data generated by the cost of a large, reuse. General data is kept in memory because it is faster to read data from memory than from other places such as databases.
Asp. NET supports caching in two ways: by storing arbitrary data through the cache API, pages are often accessed through page output caching. Let's take a look at an example.
An E-commerce site, its directory is generally updated once a week. The site provides a set of user interfaces for customers to order products. When a customer browses the catalog, the system will query the database through the network, perform various calculations, and finally return the result.
Querying these directory data from the server is a frequent operation. We know that the data only changes once a week. So several of the following operations will result in performance losses.
1. Execute the ASP.NET program to generate query statements for the database.
2. Communicate with the database server through the network.
3. The database server compiles execution queries (or executes stored procedures).
Caching mechanisms can reduce many of these tasks and improve the performance and scalability of applications. We can cache the results so that the client requests can be processed statically to improve performance. Also, the scalability of the system is increased by reducing the resources used to process each request.
The Cache API is not a completely new concept for ASP developers to keep the commonly used data in memory. In ASP, there are two objects to complete it.
Session Object
Application objects
The session is used to hold data that a single user shares across multiple requests, although there are some minor changes in the ASP.net, but these changes are primarily at the application level and are still a collection of key and key-value pairs for the session object. The Application object is also preserved in ASP.net, which is also a collection of key and key-value pairs. In both ASP and asp.net, we can use the following code to manipulate application objects
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 to the key value of the object-cache. In addition to storing key and key values externally, the Cache object also provides some new features for storing short-term data:
Dependencies-When a key is inserted into the cache object, we can set its dependencies. This key will be deleted when the dependent object changes. The dependent objects currently supported are files, other keys, and time.
Automatic invalidation-no dependent key value is automatically deleted when usage is low.
Support Callback-When a key is deleted, we can get an event that updates the key value or cancels the deletion in this event.
When we use the cache object, we must be aware of this:
Before you can use the key values in the cache object, you must check that the key value exists every time.
Because the key value in the cache object is deleted because of its dependency or low frequency, every time the object in the cache is used, it must be checked for existence.
For example, we can use the following code to return a 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=;d Atabase=grocertogo"
Sqlselect = "SELECT * FROM Products"
"Connect
SqlConnection = New SqlConnection (SQLDSN)
Sqladapater = new Sqldatasetcommand (Sqlselect, SqlConnection)
The Fill DataSet Create product table
Sqladapter1.filldataset (datasetproducts, "products")
Return Products
End Function
It is easy to rewrite this code with the cache object so that the Loaddataset () is invoked only if 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
Cache objects have many similarities with application objects in many places, and the biggest difference is that cache supports dependencies.
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.