Home  >  Article  >  PHP Framework  >  What cache types does thinkphp have?

What cache types does thinkphp have?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-22 13:43:472438browse

What cache types does thinkphp have?

ThinkPHP provides convenient caching methods, including data caching, static caching and query caching. It supports file mode, APC, Db, Memcache, Shmop, Dynamic data cache types including Sqlite, Redis, Eaccelerator and Xcache, as well as customizable static cache rules, provide shortcut methods for access operations.

Data cache

Thinkphp cache file configuration

Home is the front-end project I created, and the cache is found in Home\Conf\config.php Configuration file, the configuration is as follows:

<?php   
   return array(       
   &#39;DB_TYPE&#39;=>&#39;mysql&#39;,       
   &#39;DB_HOST&#39;=>&#39;127.0.0.1&#39;,       
  &#39;DB_NAME&#39;=>&#39;w3note&#39;,       
   &#39;DB_USER&#39;=>&#39;root&#39;,       
  &#39;DB_PWD&#39;=>&#39;123456&#39;,       
  &#39;DB_PORT&#39;=>&#39;3306&#39;,       
  &#39;DB_PREFIX&#39;=>&#39;w3_&#39;,       
  &#39;DATA_CACHE_TYPE&#39;=>&#39;file&#39;,//设置缓存方式为file       
  &#39;DATA_CACHE_TIME&#39;=>&#39;600&#39;,//缓存周期600秒       
 );       
 ?>

Usage of Thinkphp cache function

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

S(&#39;data&#39;,$Data);//使用data标识缓存$Data数据  
S(&#39;data&#39;,$Data,600);// 缓存$Data数据600秒  
 $Data = S(&#39;data&#39;);// 获取缓存数据  
S(&#39;data&#39;,NULL);// 删除缓存数据

Example Demonstration

<?php       
 // 本类由系统自动生成,仅供测试用途       
  class IndexAction extends Action{       
    public function index(){       
        //如果有缓存,则读取缓存数据       
        //如果没有缓存,则读取数据库当中的数据放入缓存       
        $lists=S(&#39;lists&#39;);                     
        if(emptyempty($lists)){                           
          $news=M(&#39;news&#39;);   
          $lists=$news->select();   
          S(&#39;lists&#39;,$lists,600);   
          echo &#39;这是直接读取数据库的数据&#39;;       
           }   
        dump($list);  
 ?>

Visit http://127.0.0.1/Home/index.php/Index/index,

First visit:

This is to read the database directly Data

array(10) {  
   [0] => array(12) {  
     ["id"] => string(1) "1"  
     ["catid"] => string(2) "13"  
     ["title"] => string(4) "thinkphp的缓存技术"  
     ["content"] => string(8) "thinkphp的缓存技术"  
     ["tags"] => string(4) "缓存"  
     ["thumb"] => string(0) ""  
     ["description"] => string(7) "thinkphp的缓存技术"  
     ["inputtime"] => string(10) "1348370202"  
     ["posid"] => string(1) "1"  
     ["ord"] => string(1) "2"  
     ["hits"] => string(1) "1"  
     ["status"] => string(1) "1"  
 }

Second visit:

array(10) {  
   [0] => array(12) {  
     ["id"] => string(1) "1"  
     ["catid"] => string(2) "13"  
     ["title"] => string(4) "thinkphp的缓存技术"  
     ["content"] => string(8) "thinkphp的缓存技术"  
     ["tags"] => string(4) "缓存"  
     ["thumb"] => string(0) ""  
     ["description"] => string(7) "thinkphp的缓存技术"  
     ["inputtime"] => string(10) "1348370202"  
     ["posid"] => string(1) "1"  
     ["ord"] => string(1) "2"  
     ["hits"] => string(1) "1"  
     ["status"] => string(1) "1"  
 }

Note: When running for the first time, the information shown above will be printed. After refreshing the page, there is no "This is a direct read" "Get data from the database", indicating that the previously generated cached data is read.

Related recommendations: "ThinkPHP Tutorial"

Quick Cache

If you just want to cache some files. For simple data and no concept of validity period, the system also provides a fast caching method F for faster operations.

Quickly cache Data data, save it under the DATA_PATH directory by default

F(&#39;data&#39;,$Data);

Quickly cache Data data, save it to the specified directory

F(&#39;data&#39;,$Data,TEMP_PATH);

Get cached data

$Data = F(&#39;data&#39;);

Delete cached data

F(&#39;data&#39;,NULL);

F method supports automatic creation of cache subdirectories and caches data data under the DATA_PATH directory. If the User subdirectory does not exist, it will be automatically created:

F(&#39;User/data&#39;,$Data);

3.1.2 version Start F method supports the batch deletion function using wildcard characters, using the following:

F(&#39;User/*&#39;,NULL);

means deleting the data cache under the DATA_PATH.'User/' directory.

The system's built-in data field information cache uses a fast caching mechanism.

Query Cache

For data queries that do not require high timeliness, we can use the query cache function to improve performance, and there is no need to use caching methods for caching and retrieval ourselves. .

APP/config.php configuration:

&#39;DATA_CACHE_TIME&#39;   => 60, // 数据缓存有效期 0表示永久缓存
&#39;DATA_CACHE_TYPE&#39;   => &#39;File&#39;,
 // 数据缓存类型,支持:File|Db|Apc|Memcache|Shmop|Sqlite|Xcache|Apachenote|Eaccelerator
&#39;DB_SQL_BUILD_CACHE&#39; => true,
&#39;DB_SQL_BUILD_LENGTH&#39; => 20, // SQL缓存的队列长度
&#39;DATA_CACHE_PATH&#39; => TEMP_PATH,

The query cache function supports all databases, and supports all caching methods and validity periods.

When using the query cache, you only need to call the cache method of the Model class, for example:

$Model->cache(true)->select();

If cache(true) is used, the current query SQL will be processed while querying. Generate query cache. By default, the cache method uses the cache method set by the DATA_CACHE_TYPE parameter (the system default value is File, which means file cache is used). The cache validity period is the time set by the DATA_CACHE_TIME parameter. The cache method and validity period of the query cache can also be specified separately:

$Model->cache(true,60,&#39;xcache&#39;)->select();

indicates that the current query cache cache method is xcache, and the cache validity period is 60 seconds.

For the same query, if the cache method is not used, no cache will be obtained or generated, even if the Cache method has been called before.

The query cache is only for internal calls. If you want the query cache to be open to other programs, you can specify the key of the query cache. For example:

$Model->cache(&#39;cache_name&#39;,60)->select();

You can directly obtain the query cache externally through the S method. Content,

$value = S(&#39;cache_name&#39;);

In addition to the select method, the query cache also supports the find and getField methods, as well as their derived methods (including statistical query and dynamic query methods). For specific applications, you can choose the caching method and cache validity period according to your needs.

SQL parsing cache

In addition to query caching, ThinkPHP also supports SQL parsing caching. Because of ThinkPHP’s ORM mechanism, all SQL is dynamically generated, and then Executed by database driver.

So if your application has a large number of SQL query requirements, you can turn on the SQL parsing cache to reduce SQL parsing and improve performance. To enable SQL parsing caching, you only need to set:

&#39;DB_SQL_BUILD_CACHE&#39; => true,

to enable the SQL creation cache for database queries. The default caching method is file mode. It also supports xcache and apc caching. You only need to set:

&#39;DB_SQL_BUILD_QUEUE&#39; => &#39;xcache&#39;,

We know that the amount of SQL query for a project may be very huge, so it is necessary to set the cache queue length. For example, if we hope that the SQL parsing cache does not exceed 20 records, we can set:

&#39;DB_SQL_BUILD_LENGTH&#39; => 20, // SQL缓存的队列长度

Note: Only query methods support SQL parsing cache

Static cache

To use the static cache function, you need to enable the HTML_CACHE_ON parameter and use the HTML_CACHE_RULES configuration parameter to set static Cache rules file.

Although static caching rules can also be defined in the application configuration file, it is recommended to define static caching rules for different modules in the module configuration file.

Static rule definition

The definition of static rules is as follows:

&#39;HTML_CACHE_ON&#39; => true, // 开启静态缓存
&#39;HTML_CACHE_TIME&#39; => 60, // 全局静态缓存有效期(秒)
&#39;HTML_FILE_SUFFIX&#39; => &#39;.shtml&#39;, // 设置静态缓存文件后缀
&#39;HTML_CACHE_RULES&#39; => array( // 定义静态缓存规则
 // 定义格式1 数组方式
 &#39;静态地址&#39; => array(&#39;静态规则&#39;, &#39;有效期&#39;, &#39;附加规则&#39;), 
 // 定义格式2 字符串方式
 &#39;静态地址&#39; => &#39;静态规则&#39;, 
)

Definition format 1 uses an array to facilitate the setting of different static rules individually. The validity period, definition format 2 uses string mode to subscribe to static rules, and uses the global static cache validity period set by HTML_CACHE_TIME.

静态缓存文件的根目录在HTML_PATH定义的路径下面,并且只有定义了静态规则的操作才会进行静态缓存。 并且静态缓存支持不同的存储类型。 静态缓存仅在GET请求下面有效。

静态地址

静态地址包括下面几种定义格式:

第一种是定义全局的操作静态规则,例如定义所有的read操作的静态规则为:

&#39;read&#39;=>array(&#39;{id}&#39;,60)

其中,{id}

表示取$_GET['id']

为静态缓存文件名,第二个参数表示缓存60秒。

第二种是定义全局的控制器静态规则,例如定义所有的User控制器的静态规则为:

&#39;user:&#39;=>array(&#39;User/{:action}_{id}&#39;,&#39;600&#39;)

其中,{:action}

表示当前的操作名称

第三种是定义某个控制器的操作的静态规则,例如,我们需要定义Blog控制器的read操作进行静态缓存

&#39;blog:read&#39;=>array(&#39;{id}&#39;,0)

第四种方式是定义全局的静态缓存规则,这个属于特殊情况下的使用,任何模块的操作都适用,例如:

&#39;*&#39;=>array(&#39;{$_SERVER.REQUEST_URI|md5}&#39;),

表示根据当前的URL进行缓存。

静态规则

静态规则是用于定义要生成的静态文件的名称,静态规则的定义要确保不会冲突,写法可以包括以下情况:

1.使用系统变量

包括 _GET、_REQUEST、_SERVER、_SESSION、_COOKIE

格式:

{$_×××|function}

例如:

{$_GET.name} 
{$_SERVER.REQUEST_URI|md5}

2.使用框架特定的变量

{:module} 、{:controller} 和{:action}

分别表示当前模块名、控制器名和操作名。

例如:

{:module}/{:controller}_{:action}

3.使用_GET变量

{var|function}也就是说 {id}其实等效于 {$_GET.id}

4.直接使用函数

{|function} 例如:{|time}

5.支持混合定义

例如我们可以定义一个静态规则为:

&#39;{id},{name|md5}&#39;

在{}之外的字符作为字符串对待,如果包含有"/",会自动创建目录。

例如,定义下面的静态规则:

{:module}/{:action}_{id}

则会在静态目录下面创建模块名称的子目录,然后写入操作名_id.shtml 文件。

静态缓存有效期

单位为秒。如果不定义,则会获取配置参数HTML_CACHE_TIME的设置值,如果定义为0则表示永久缓存。

附加规则

通常用于对静态规则进行函数运算,例如:

&#39;read&#39;=>array(&#39;Think{id},{name}&#39;,&#39;60&#39;, &#39;md5&#39;)

翻译后的静态规则是

md5(&#39;Think&#39;.$_GET[&#39;id&#39;]. &#39;, &#39;.$_GET[&#39;name&#39;]);

The above is the detailed content of What cache types does thinkphp have?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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