The system's default caching method is the File method Caching, we can define other caching methods in the project configuration file, for example, modify the default caching method to Xcache (of course, your environment needs to support Xcache)
For the problem of a large number of files under the cache directory under File mode caching due to excessive cache data, ThinkPHP also provides a solution, which can enable hash subdirectory caching.
'DATA_CACHE_SUBDIR'=>true
You can also set the level of the hash directory, such as
'DATA_PATH_LEVEL'=>2
You can automatically create multi-level subdirectories for caching based on the hash of the cache identifier.
The S method supports cache validity period. In many cases, we may not need the concept of validity period, or using file mode caching can meet the requirements, so the system also provides a fast caching method F specifically for file mode. method. The F method can only be used to cache simple data types and does not support validity periods and cache objects. Use the following:
//Quickly cache Data data, saved in the DATA_PATH directory by default
F('data',$data);
//Quickly cache Data data and save it to the specified directory
F('data',$data,TEMP_PATH);
F('user/data',$data);
//Delete cached data
F('data',null);
//Get cache data
$data=F('data');
//Dynamic cache, cache files exist in RuntimeTemp
'DATA_CACHE_TYPE'=>'file',
'DATA_CACHE_TIME'=>'3600',
//'DATA_CACHE_SUBDIR'=>true,//Open subdirectory
//'DATA_CACHE_LEVEL'=>3,//Set the level of subdirectories
function view(){ //缓存 //$cache=Cache::getInstance('缓存方式','缓存参数'); //$cache=Cache::getInstance('Xcache',array('expire'=>60)); //$cache->set('名字','值');或者$cache->name='值'; //$value=$cache->get('名字');或者$value=$cache->name; //$cache->rm('名字');或者unset($cache->name); //S('名字','数据','3600','类型')缓存快捷方法 $user=M('haodetong'); $value=S('list'); if(empty($value)){ $list=$user->select(); S('list',$list,3600); echo '这个是直接从数据库中读取的文件'; dump($list); }else{ echo '这个是缓存文件'; dump($value); } }
After refreshing again, as shown below:
The following is a separate introduction to the fast caching of the F method in ThinkPHP
Cache using file mode can meet the requirements, so the system also provides a fast caching method F method specifically for file mode$path="../Public/Runtime/";
$str="asdfasdfasdaaaaaaaaaaaaaaaaaaaaa";
F("str/andy",$str,$path);
The content of the andy.php file is as follows:
return 'asdfasdfasdaaaaaaaaaaaaaaaaaaaaa';
The following piece of code is an overview of ThinkPHP caching method S()
thinkPHP’s F method can only be used to cache simple data types and does not support validity periods and cached objects. The S() cache method supports validity period, also known as dynamic cache method. The usage examples are as follows:The code is as follows:
Copy code The code is as follows:
// Use data identifier to cache $Data data
S('data',$Data); //The first one is the cache mark, the second one is the cached data
S('data',$Data,3600);
Copy code The code is as follows:
// Delete cached data
S('data',NULL); //The first parameter is the cached identification name
$cache=S($cachename);//设置缓存标示 // 判断是否有这个查询缓存 if(!$cache){ //$cache 中是缓存的标示(每个查询都对应一个缓存 即 不同的查询有不同的缓存) $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); //设置缓存的生存时间 } S($cachename,NULL); //删除缓存