Thinkphp Cache Technology details, thinkphp cache details

Source: Internet
Author: User

Thinkphp Cache Technology details, thinkphp cache details

This article analyzes the caching technology of thinkphp in detail. 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:
Copy codeThe 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:
Copy codeCode: 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:
Copy codeThe 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:
Copy codeThe 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.

I hope this article will help you design PHP programs based on the ThinkPHP framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.