If the Web site that is not cached is millions or tens, it can put a lot of pressure on the database or the server, and through the cache, greatly reduce the load of the server and the database. If we divide the process of reading data into three layers, the first one is the access layer, the first is the cache layer, and the third is the database access layer. Without a cache layer, the access layer reads the data directly from the database access layer, and after the cache is set, the access layer is no longer read directly from the database access layer, but instead reads the data from the cache layer.
We make a simple comparison, suppose a page, in one hours can be accessed 1 million times, if the page each time it is accessed, the database is read directly after the build, in one hours will be duplicated 1 million times, and if the page is periodically cached for 10 minutes, that is, every Interval 10 minutes cache data will be generated once, one hours will only be generated 6 times, two ways a contrast, the effect is obvious, two comparisons under the pressure of the server load than the difference of more than a hundred thousand of times times, caching technology will make the site load in the peak.
There are many ways to cache thinkphp, such as file, Apachenote, Apc, Eaccelerator, Memcache, Shmop, Sqlite, Db, Redis, and XCache, and now I'll take a look at the file cache.
Configuration of the thinkphp cache file
Home is the foreground item that I set up, in home\conf\config.php find the cached configuration file, configured as follows:
| 06 |
' Db_type ' = ' mysql ', |
| 07 |
' Db_host ' = ' 127.0.0.1 ', |
| 08 |
' Db_name ' = ' w3note ', |
| 09 |
' Db_user ' = ' root ', |
| 10 |
' Db_pwd ' = ' 123456 ', |
| 11 |
' Db_port ' = ' 3306 ', |
| 12 |
' Db_prefix ' = ' w3_ ', |
| 13 |
' Data_cache_type ' = ' file ',//set cache as file |
| 14 |
' Data_cache_time ' = ' 600 ',//cache period 600 seconds |
Use of thinkphp cache functions
In thinkphp, I like to use the shortcut cache function s () for caching; Its usage is as follows:
S (' data ', $Data);//cache $data data using the data identity
S (' Data ', $Data, 600);//cache $data data for 600 seconds
$Data = S (' data ');//Get Cached data
S (' data ', NULL);//Delete cached data
The following is the complete code for the foreground project controller
| 02 |
This class is automatically generated by the system and is intended for testing purposes only |
| 03 |
Class Indexaction extends action{ |
| 04 |
Public Function index () { |
| 06 |
If there is a cache, read the cached data |
| 07 |
If there is no cache, the data in the read database is placed in the cache |
| 08 |
$lists =s (' lists '); |
| 14 |
$lists = $news->select (); |
| 16 |
S (' lists ', $lists, 600); |
| 18 |
Echo ' This is the data directly read from the database '; |
To access the Http://127.0.0.1/Home/index.php/Index/index output:
This is the data that reads the database directly
Array (10) {
[0] = = Array (12) {
["id"] + = string (1) "1"
["catid"] = + string (2) "13"
["title"] and string (4) "Thinkphp Caching Technology"
["Content"] and string (8) "Thinkphp Caching Technology"
["tags"] + + string (4) "Cache"
["thumb"] = + string (0) ""
["description"] = + string (7) "Thinkphp Caching Technology"
["inputtime"] = + string (10) "1348370202"
["posid"] = + string (1) "1"
["ord"] = + string (1) "2"
["hits"] = + string (1) "1"
[Status] = + string (1) "1"
}
...
Description, the first run, will print out the information as shown above, after refreshing the page, "This is directly read the database data", indicating that the read is the previously generated cache data.
Caching Technology for thinkphp