Brief analysis of thinkphp cache (F method) and dynamic cache (S method) (daily finishing), analysis of thinkphp
The system is cached by default, and we can define other caching methods in the project configuration file, for example, modify the default cache mode to XCache (of course, your environment needs to support XCache)
For cache directories under file-mode caching, where there is a large number of file problems due to too much cache data, thinkphp also gives a solution to how the Hashits directory cache can be enabled.
' Data_cache_subdir ' =>true
You can also set the hierarchy of hash directories, such as
' Data_path_level ' =>2
You can automatically create multi-tier subdirectories to cache based on the hash of the cache identity.
The S method supports cache expiration, in many cases we may not need the concept of expiration, or the use of file-like caching to meet the requirements, so the system also provides a quick caching method specifically for file methods F method. The F method can only be used to cache simple data types, does not support expiration dates, and cache objects, using the following:
Cache data quickly, saved by default in the Data_path directory
F (' data ', $data);
Cache data quickly and save to the specified directory
F (' Data ', $data, Temp_path);
F (' User/data ', $data);
Delete cached data
F (' data ', NULL);
Get Cached data
$data =f (' data ');
Configuration file config.php
Dynamic cache, cache file exists in \runtime\temp
' Data_cache_type ' = ' file ',
' Data_cache_time ' = ' 3600 ',
' Data_cache_subdir ' =>true,//open sub-directory
' Data_cache_level ' =>3,//sets the hierarchy of subdirectories
Action file:
Function view () { //cache //$cache =cache::getinstance (' cache mode ', ' cache parameters '); $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 ', ' 3600 ', ' type ') cache shortcut method $user =m (' Haodetong '); $value =s (' list '); if (empty ($value)) { $list = $user->select (); S (' list ', $list, 3600); Echo ' This is a file read directly from the database '; Dump ($list); } else{ Echo ' This is the cache file '; Dump ($value); } }
The first visit is as follows:
Once again refreshed as:
Let's give you a separate introduction to the fast cache of the F method in thinkphp
The use of file-mode caching is sufficient, so the system also provides a fast caching method specifically for File mode F method
$path = ". /public/runtime/";
$str = "ASDFASDFASDAAAAAAAAAAAAAAAAAAAAAA";
F ("Str/andy", $str, $path);
This puts the contents of the $STR string. In the/public/runtime/str/andy.php file.
The contents of the andy.php file are as follows:
<?php
Return ' ASDFASDFASDAAAAAAAAAAAAAAAAAAAAAA ';
?>
The following section of code is the Thinkphp caching method s () overview
The thinkphp F method can only be used to cache simple data types and does not support expiration and cache objects. The S () cache method supports the validity period, also known as the dynamic caching method, using the following example:
The code is as follows:
Copy the Code code as follows:
Cache $data data using the data identity
S (' data ', $Data); The front is the cache flag, followed by the cached data
The code is as follows:
Cache $data data for 3,600 seconds
S (' Data ', $Data, 3600);
The code is as follows:
Copy the Code code as follows:
Delete cached data
S (' data ', NULL); The first parameter when the cached identity name
The code is as follows:
$cache =s ($cachename);//Set cache flag//Determine if there is a cache identifier for this query cache if (! $cache) {//$cache are cached (each query corresponds to a cache that has different caches for different queries) $cache = $video->where ($map)->order ($order)->limit ($limit)->select (); foreach ($cache as $key = + $value) { $userlist = $user->where ("id=". $value [' user_id '])->find (); $cache [$key] ["nickname"]= $userlist [' nickname '] ; S ($cachename, $cache, 3600); Set the lifetime of the cache } S ($cachename, NULL);//Delete cache
http://www.bkjia.com/PHPjc/1065155.html www.bkjia.com true http://www.bkjia.com/PHPjc/1065155.html techarticle Brief analysis of thinkphp cache (F method) and dynamic cache (S method) (daily finishing), analysis of thinkphp system The default caching method is to use the file mode cache, we can configure the project ...