This article mainly introduces thinkphp cache technology, analyzes in detail the characteristics of the cache technology and the practical application skills of caching in ThinkPHP, which has some reference value, for more information about thinkphp caching technology, see this article. Share it with you for your reference. The specific analysis is as follows:
If a website without caching has millions or tens of millions of visits, it will put a lot of pressure on the database or server. Through caching, the load on the server and database will be greatly reduced, if we divide the Data Reading Process into three layers, the first is the access layer, the first is the cache layer, and the third is the database access layer. If there is no cache layer, the access layer reads data directly from the database access layer. After the cache is set, the access layer no longer reads data directly from the database access layer, but reads data from the cache layer.
Let's make a simple comparison. Assume that a page can be accessed 1 million times in an hour. If the page is accessed every time, the database is read directly before compilation, the page will be repeatedly generated 1 million times in an hour. If the page is periodically cached for 10 minutes, that is, the cached data will be generated every 10 minutes, within an hour, the server load will only be generated six times. The two methods have a one-to-one ratio and the effect is obvious. The pressure ratio of the two methods is more than ten thousand times different from that of the server load, the cache technology will make the website load more efficient during peak hours.
Thinkphp has many caching methods, such as File, Apachenote, Apc, Eaccelerator, Memcache, Shmop, Sqlite, Db, Redis, and Xcache. Now let's talk about File caching.
Thinkphp cache file configuration
Home is a front-end project created by me. Find the cache configuration file in Home \ Conf \ config. php and configure it as follows:
The Code is as follows:
<? Php
Return array (
'Db _ type' => 'mysql ',
'Db _ host' => '2017. 0.0.1 ',
'Db _ name' => 'w3note ',
'Db _ user' => 'root ',
'Db _ pwd' => '123 ',
'Db _ port' => '123 ',
'Db _ prefix' => 'w3 _',
'Data _ CACHE_TYPE '=> 'file', // sets the cache mode to file.
'Data _ CACHE_TIME '=> '000000', // The cache cycle is 600 seconds.
);
?>
Use of Thinkphp cache Functions
In thinkphp, I like to use the quick cache function S () for caching. Its usage is as follows:
The Code is as follows:
S ('data', $ data); // use the Data identifier to cache $ data Data
S ('data', $ data, 600); // cache $ Data for 600 seconds
$ Data = S ('data'); // obtain cached data
S ('data', NULL); // deletes cached data
The complete code of the front-end project controller is as follows:
The Code is as follows:
<? Php
// This class is automatically generated by the system for testing purposes only
Class IndexAction extends Action {
Public function index (){
// If cache exists, the cached data is read.
// If no cache is available, the data read from the database is put into the cache.
$ Lists = S ('lists ');
If (emptyempty ($ lists )){
$ News = M ('News ');
$ Lists = $ news-> select ();
S ('lists', $ lists, 600 );
Echo 'this is the data that is directly read from the database ';
}
Dump ($ list );
?>
Access the http: // 127.0.0.1/Home/index. php/Index/index output, which is to directly read the database data:
The Code is as follows:
Array (10 ){
[0] => array (12 ){
["Id"] => string (1) "1"
["Catid"] => string (2) "13"
["Title"] => string (4) "thinkphp Cache Technology"
["Content"] => string (8) "thinkphp Cache Technology"
["Tags"] => string (4) "cache"
["Thumb"] => string (0 )""
["Description"] => string (7) "thinkphp Cache Technology"
["Inputtime"] => string (10) "1348370202"
["Posid"] => string (1) "1"
["Ord"] => string (1) "2"
["Hits"] => string (1) "1"
["Status"] => string (1) "1"
}
Note: during the first running, the information shown above will be printed. After refreshing the page, the system will omit "this is the data for Directly Reading the Database", indicating that the previously generated cache data is read.