Caching Technology for thinkphp

Source: Internet
Author: User

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:

01 <?php
02 /**
03 * Configuration file
04 */
05 Return Array (
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
15
16
17 );
18 ?>

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

01 <?php
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 () {
05
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 ');
09
10 if (empty ($lists)) {
11
12 $news =m (' News ');
13
14 $lists = $news->select ();
15
16 S (' lists ', $lists, 600);
17
18 Echo ' This is the data directly read from the database ';
19
20 }
21st
22 Dump ($list);

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

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.