thinkphp caching technology detailed explanation, thinkphp caching detailed explanation_PHP tutorial

WBOY
Release: 2016-07-13 10:12:07
Original
797 people have browsed it

Detailed explanation of thinkphp caching technology, detailed explanation of thinkphp caching

This article analyzes thinkphp’s caching technology in detail. Share it with everyone 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 can be greatly reduced. If we take the process of reading data It is divided 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, and sets the cache Finally, the access layer no longer reads 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 this page is directly read from the database and then compiled and generated every time it is accessed, within an hour it will be It is generated repeatedly 1 million times, and if this page is cached periodically for 10 minutes, that is, the cached data will only be generated once every 10 minutes, and will only be generated 6 times in an hour. Compare the two methods, The effect is obvious. The pressure ratio of the server load between the two comparisons is more than 100,000 times different. The caching technology will make the website load more manageable during peak periods.

Thinkphp has many caching methods, such as File, Apachenote, Apc, Eaccelerator, Memcache, Shmop, Sqlite, Db, Redis and Xcache. Now let me talk about File caching.

Thinkphp cache file configuration

Home is the front-end project I created. I found the cached configuration file in HomeConfconfig.php and the configuration is as follows:

Copy code The code is as follows:
return array(                
'DB_TYPE'=>'mysql', 
'DB_HOST'=>'127.0.0.1', '
'DB_NAME'=>'w3note', 
'DB_USER'=>'root', 
'DB_PWD'=>'123456', 
'DB_PORT'=>'3306', 
'DB_PREFIX'=>'w3_', 
'DATA_CACHE_TYPE'=>'file',//Set the cache mode to file
'DATA_CACHE_TIME'=>'600',//caching period 600 seconds
); 
?>

Use of Thinkphp caching function

In thinkphp, I like to use the shortcut cache function S() for caching. Its usage is as follows:

Copy code The code is as follows:
S('data',$Data);//Use data identifier to cache $Data data
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 of the front-end project controller:
Copy code The code is as follows:
// This class is automatically generated by the system and is for testing purposes only
class IndexAction extends Action{ 
public function index(){
//If there is a cache, read the cached data //If there is no cache, read the data from the database and put it into the cache.
        $lists=S('lists');                              If(emptyempty($lists)){                                                                                                                                                                                                                                              $news=M('news');                                                      $lists=$news->select();                                           S('lists',$lists,600);
echo 'This is the data directly read from the database';                                                                                                        dump($list);                                           ?>

Visit http://127.0.0.1/Home/index.php/Index/index output, which is the data directly read from the database:



Copy code

The code is as follows:
array(10) {
[0] => array(12) {
["id"] => string(1) "1"
["catid"] => string(2) "13"
["title"] => string(4) "thinkphp's caching technology"
["content"] => string(8) "thinkphp's caching technology"
["tags"] => string(4) "cache"
["thumb"] => string(0) ""
["description"] => string(7) "thinkphp's caching technology"
["inputtime"] => string(10) "1348370202"
["posid"] => string(1) "1"
["ord"] => string(1) "2"
["hits"] => string(1) "1"
["status"] => string(1) "1"
}

Note that when running for the first time, the information shown above will be printed. After refreshing the page, "This is the data directly read from the database" is missing, indicating that the cached data generated previously is read.

I hope this article will be helpful to everyone’s PHP programming based on the ThinkPHP framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/924543.htmlTechArticleDetailed explanation of thinkphp caching technology, detailed explanation of thinkphp caching technology. This article analyzes thinkphp caching technology in detail. Share it with everyone for your reference. The specific analysis is as follows: If the website without cache is...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template