: This article mainly introduces the dynamic cache (S method) and quick cache (F method) in ThinkPHP. if you are interested in the PHP Tutorial, refer to it. The default caching method is File caching. you can define other caching methods in the project configuration File. for example, you can modify the default caching method to Xcache (of course, your environment needs to support Xcache)
ThinkPHP also provides a solution for caching large volumes of files under the cache directory in the File mode, which enables the hash subdirectory.
'Data _ cache_subdir' => true
You can also set the level of the hash Directory, for example
'Data _ PATH_LEVEL '=> 2
You can automatically create multiple sub-directories to cache based on the hash of the cache ID.
The S method supports the cache validity period. in many cases, we may not need the validity period, or the file cache can meet the requirements, therefore, the system also provides a fast cache method F for files. The F method can only be used to cache simple data types, and does not support validity periods and cache objects. The following is used:
// Cache Data quickly. The Data is saved in the DATA_PATH directory by default.
F ('data', $ data );
// Cache Data quickly and save it to the specified directory
F ('data', $ data, TEMP_PATH );
F ('user/data', $ data );
// Delete cache data
F ('data', null );
// Obtain cache data
$ Data = F ('data ');
Configuration file config. php
// Dynamic cache. the cached file exists in \ Runtime \ Temp
'Data _ CACHE_TYPE '=> 'File ',
'Data _ CACHE_TIME '=> '123 ',
// 'Data _ cache_subdir' => true, // enable subdirectory
// 'Data _ CACHE_LEVEL '=> 3, // sets the sub-directory level
Action file:
Function view (){
// Cache
// $ Cache = Cache: getInstance ('cache method ', 'cache parameter ');
// $ Cache = Cache: getInstance ('xcache', array ('expire '=> 60 ));
// $ Cache-> set ('name', 'value'); or $ cache-> name = 'value ';
// $ Value = $ cache-> get ('name'); or $ value = $ cache-> name;
// $ Cache-> rm ('name'); or unset ($ cache-> name );
// S ('name', 'data', '123', 'type') cache shortcut
$ User = M ('haodetong ');
$ Value = S ('list ');
If (empty ($ value )){
$ List = $ user-> select ();
S ('list', $ list, 3600 );
Echo 'This is a file directly read from the database ';
Dump ($ list );
} Else {
Echo 'This is a cached file ';
Dump ($ value );
}
}
The first access time is as follows:
Refresh again, as shown in the following figure:
The above introduces the dynamic cache (S method) and quick cache (F method) in ThinkPHP, including some content, and hope to help friends who are interested in PHP tutorials.