Caching caching is a common data that generates a relatively large cost, saves it. General data is saved in memory because reading data from memory is fast than other places such as from the database.
ASP.NET supports caching in two ways: Store any data via the Cache API, and outputs the cache to the page that is often accessed through the page. Let's take an example.
A site of an e-commerce, its directory is generally updated a week. The site provides a user interface to allow customers to order products. When a customer browses the directory, the system will query the database through the network, perform various calculations, and finally return the result.
The operation from the server queries these directory data is very frequent. We know that these data will change once a week. So the following operations will bring a loss of performance.
1. Execute the ASP.NET program to generate query statements for the database.
2. Communication via the network and the database server.
3. Database server compiles executing queries (or executes the storage process).
The cache mechanism can reduce many such work and improve the performance and scalability of the application. We can cache results to improve performance in order to static processing of customer requests. At the same time, the system's scalability is also improved due to the reduction of resources that handle each request.
Cache API For ASP developers, saving common data in memory is not a new concept. In ASP, there are two objects to complete it.
Session object
APPLICATION object
Session is used to save the data shared between a single user in multiple requests, although there are some small changes in ASP.NET, but these changes are mainly in the application level, and the session object is still a saver and key. A collection of values. Application object is also saved in ASP.NET, which is also a collection of key and key value pairs. In 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 with key values - Cache. In addition to The storage key and key value are provided, and the cache object also provides additional new features for short-term data: dependent - When a key is inserted into the Cache object, we can set its dependencies. When the dependent object changes, this button will Deleted. Now supports the dependence object with files, other keys and time. Automatic failure - no dependent key value, will be automatically deleted when the frequency is not high, support callback - When a button will be deleted, we will be deleted You can get an event, update the key value or cancel the delete operation in this event. When we use the Cache object, you must pay attention to this: Before we use the key value in the Cache object, you must check if the key value exists every time. Since the key value in the Cache object is deleted due to its dependence or the frequency of use, it will be deleted, so every object in Cache must be checked. 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 rewrite this code with a cache object, so that only the DataSet does not exist in Cache to call loading loadingtataset ().
Public Function getProductData () AS Dataset IF (isnothing (Cache ("ProductData") Then Cache ("ProductData" = loadingDataSet ()
Return Cache ("ProductData") End Function Cache object has many similarities between many places and Application objects, and the biggest difference is that Cache supports dependencies.