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, assume that the data reading process is divided into three layers. The first is the access layer.
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:
-
- 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:
- 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:
-
- // 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:
- 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.