SoFunction
Updated on 2025-03-08

How to use Caching

Cache is supported in two ways: storing arbitrary data through the Cache API, and cache frequently visited pages through the page output. Let's look at an example.

An e-commerce site, its directory is usually updated once a week. The site provides a set of user interfaces for 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 results.

Querying these directory data from the server is very frequent. We know that these data only change once a week. Therefore, the following operations will bring performance losses.

1. The executed program generates query statements to the database.
2. Communication with the database server through the network.
3. The database server compiles and executes query (or executes storage procedures).

The caching mechanism can reduce many of these efforts and improve application performance and scalability. We can cache results so that we can handle customer requests statically to improve performance. At the same time, the system's scalability is also improved because the resources used to process each request are reduced.

For ASP developers, storing commonly used data in memory is not a completely new concept. In ASP, there are two objects that complete it.

Session Object

Application Object

Session is used to save data shared by a single user between multiple requests. Although there are some minor changes in it, these changes are mainly at the application level. For Session objects, it is still a collection of key and key-value pairs. The Application object is also saved, and is also a collection of key and key-value pairs. In ASP and we can both use the following code to manipulate Application objects

Copy the codeThe code is as follows:

Application("SomeInterestingData") = "Example data"
(Application("SomeInterestingData")

We can access the Session object using the same method.

A new key and key value object is brought to Cache. In addition to storing key and key value pairs, Cache objects also provide some new functions to store short-term data: Dependency - When a key is inserted into a Cache object, we can set its dependencies. This key will be deleted when the dependent object changes. The dependency objects supported now include files, other keys and times. Automatic failure—No dependency key values ​​will be automatically deleted when they are not used at a high frequency. Support callbacks—When a key is deleted, we can get an event in which to update the key value or cancel the delete operation.
When we use Cache objects, we must pay attention to this point: before using the key values ​​in the Cache object, we must check whether the key values ​​exist every time. Since the key values ​​in the Cache object are deleted due to their dependence or low frequency of use, each time the object in the Cache is used, it is necessary to check whether it exists. For example, we can use the following code to return DataSet.

Copy the codeThe code is as follows:

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
(datasetProducts, "products")
Return products
End Function
It is easy for us to overwrite this code with a Cache object so that LoadDataSet() is called 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.