& Nbsp; many sites use databases as the containers for storing site data. The database contains the producer information, directory structure, article, or message book. some data may be completely static, which will be of great benefit from a cache system. Such a system caches the SQL query results to a file of the system for storage, thus blocking connection to the database, constructing the query and obtaining the returned results, and improving the response time. Some system databases are not stored on WEB servers. Therefore, many websites need to use databases as the containers for storing site data. The database contains the producer information, directory structure, article, or message book. some data may be completely static, which will be of great benefit from a cache system.
Such a system caches the SQL query results to a file of the system for storage, thus blocking connection to the database, constructing the query and obtaining the returned results, and improving the response time.
Some system databases are not stored on WEB servers. Therefore, a remote connection (TCP or similar) is required, or a large amount of data is obtained from the database, in this way, you have to endure more time, which is determined by the system response time and resource utilization.
Prerequisites
This tutorial uses MySQL as the database. You need to install MySQL (www.mysql.com is valid for download) and activate php mysql extension (activated by default ).
To query databases, you need to have basic knowledge about SQL (Structured Query Language.
Cache SQL query results
Why cache query results?
The cache query results greatly improve the script execution time and resource requirements.
Caching SQL query results also allows you to process data later. If you use the file cache to store the output results of all scripts (HTML output), this may not work.
When you execute an SQL query, the typical processing process is:
L Connect to database
L Prepare SQL query
L Send query to database
L Get returned results
L Close database connection
The above method occupies resources and, on the contrary, affects the script performance. Coordination can only be achieved by obtaining a large amount of returned data and the location of the database server. Although continuous connection can improve the load when connecting to the database, it consumes a lot of memory resources. if a large amount of data is obtained, the storage duration will be very short.
Create an SQL query:
SQL (Structured Query Language) queries are used as interfaces to operate databases and their content. SQL can be used to define and edit the table structure, insert data to the table, and update or delete information in the table.
SQL is a language used for data communication. In most PHP databases, extensions (MySQL, ODBC, Oracle, and so on) pass SQL queries to the database to manage the entire process.
In this tutorial, only the select language is used to obtain data in the database. The data will be cached and then used as the data source.
Decide when to update the cache:
Depending on the program's needs, the cache can take multiple forms. The most common three methods are:
L Time-triggered cache (expired timestamp)
L Content change triggers cache (when data changes are detected, the cache is updated accordingly)
L Manually trigger the cache (manually inform the system of information expiration and force a new cache)
Your cache requirements may be one or more of the above principles. This tutorial will discuss how to trigger the time. However, in a comprehensive Cache mechanism, the combination of the three methods will be used.
Cache results:
Basically, the cache uses two PHP functions: serialize () and unserialize () (Note: These two functions represent serialization and deserialization respectively ).
The serialize () function is used to store PHP values. it ensures that the types and structures of these values are not lost.
In fact, PHP
12 3 Next page