Home > Article > PHP Framework > 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( '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',//设置缓存方式为file 'DATA_CACHE_TIME'=>'600',//缓存周期600秒 ); ?>
Usage of Thinkphp cache function
In thinkphp, use the shortcut cache function S() for caching. Its usage is as follows:
S('data',$Data);//使用data标识缓存$Data数据 S('data',$Data,600);// 缓存$Data数据600秒 $Data = S('data');// 获取缓存数据 S('data',NULL);// 删除缓存数据
Example Demonstration
<?php // 本类由系统自动生成,仅供测试用途 class IndexAction extends Action{ public function index(){ //如果有缓存,则读取缓存数据 //如果没有缓存,则读取数据库当中的数据放入缓存 $lists=S('lists'); if(emptyempty($lists)){ $news=M('news'); $lists=$news->select(); S('lists',$lists,600); echo '这是直接读取数据库的数据'; } 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('data',$Data);
Quickly cache Data data, save it to the specified directory
F('data',$Data,TEMP_PATH);
Get cached data
$Data = F('data');
Delete cached data
F('data',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('User/data',$Data);
3.1.2 version Start F method supports the batch deletion function using wildcard characters, using the following:
F('User/*',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:
'DATA_CACHE_TIME' => 60, // 数据缓存有效期 0表示永久缓存 'DATA_CACHE_TYPE' => 'File', // 数据缓存类型,支持:File|Db|Apc|Memcache|Shmop|Sqlite|Xcache|Apachenote|Eaccelerator 'DB_SQL_BUILD_CACHE' => true, 'DB_SQL_BUILD_LENGTH' => 20, // SQL缓存的队列长度 'DATA_CACHE_PATH' => 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,'xcache')->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('cache_name',60)->select();
You can directly obtain the query cache externally through the S method. Content,
$value = S('cache_name');
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:
'DB_SQL_BUILD_CACHE' => 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:
'DB_SQL_BUILD_QUEUE' => 'xcache',
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:
'DB_SQL_BUILD_LENGTH' => 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:
'HTML_CACHE_ON' => true, // 开启静态缓存 'HTML_CACHE_TIME' => 60, // 全局静态缓存有效期(秒) 'HTML_FILE_SUFFIX' => '.shtml', // 设置静态缓存文件后缀 'HTML_CACHE_RULES' => array( // 定义静态缓存规则 // 定义格式1 数组方式 '静态地址' => array('静态规则', '有效期', '附加规则'), // 定义格式2 字符串方式 '静态地址' => '静态规则', )
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操作的静态规则为:
'read'=>array('{id}',60)
其中,{id}
表示取$_GET['id']
为静态缓存文件名,第二个参数表示缓存60秒。
第二种是定义全局的控制器静态规则,例如定义所有的User控制器的静态规则为:
'user:'=>array('User/{:action}_{id}','600')
其中,{:action}
表示当前的操作名称
第三种是定义某个控制器的操作的静态规则,例如,我们需要定义Blog控制器的read操作进行静态缓存
'blog:read'=>array('{id}',0)
第四种方式是定义全局的静态缓存规则,这个属于特殊情况下的使用,任何模块的操作都适用,例如:
'*'=>array('{$_SERVER.REQUEST_URI|md5}'),
表示根据当前的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.支持混合定义
例如我们可以定义一个静态规则为:
'{id},{name|md5}'
在{}之外的字符作为字符串对待,如果包含有"/",会自动创建目录。
例如,定义下面的静态规则:
{:module}/{:action}_{id}
则会在静态目录下面创建模块名称的子目录,然后写入操作名_id.shtml 文件。
静态缓存有效期
单位为秒。如果不定义,则会获取配置参数HTML_CACHE_TIME的设置值,如果定义为0则表示永久缓存。
附加规则
通常用于对静态规则进行函数运算,例如:
'read'=>array('Think{id},{name}','60', 'md5')
翻译后的静态规则是
md5('Think'.$_GET['id']. ', '.$_GET['name']);
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!