Home >Backend Development >PHP Tutorial >ThinkPHP cache fast cache and dynamic cache

ThinkPHP cache fast cache and dynamic cache

不言
不言Original
2018-06-08 17:09:152055browse

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. This article is the editor’s daily compilation of thinkphp caching methods. Friends who are interested in thinkphp caching methods should learn together.

The system’s default caching method is File caching. We can define other caches in the project configuration file. Method, 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 in the cache directory under the File mode cache due to too much cached data, ThinkPHP A solution is also given to enable hash subdirectory caching.

    'DATA_CACHE_SUBDIR'=>true

You can also set the level of the hash directory. For example,

    'DATA_PATH_LEVEL'=>2

can automatically create multi-layer 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 cache specifically for file mode. Cache method F 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:

 //快速缓存Data数据,默认保存在DATA_PATH目录下面    
      F('data',$data);
    //快速缓存Data数据,保存到指定的目录   
      F('data',$data,TEMP_PATH);
      F('user/data',$data);
    //删除缓存数据
      F('data',null);
    //获取缓存数据
      $data=F('data');

Configuration file config.php

//动态缓存,缓存文件存在于\Runtime\Temp
'DATA_CACHE_TYPE'=>'file',
'DATA_CACHE_TIME'=>'3600',
//'DATA_CACHE_SUBDIR'=>true,//开启子目录
//'DATA_CACHE_LEVEL'=>3,//设置子目录的层次

Action file:

 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);
    }
  }

As shown below when visiting for the first time:

As shown after refreshing again:

The following is a separate introduction to the fast caching of the F method in ThinkPHP

The file mode caching can meet the requirements, so the system also provides a fast caching dedicated to the file mode. Cache method F method

$path="../Public/Runtime/";
$str="asdfasdfasdaaaaaaaaaaaaaaaaaaaaaa";
F("str/andy",$str,$path);

In this way, the content of the $str string is placed in the file of ../Public/Runtime/str/andy.php and the file of

andy.php The content is as follows:

<?php
return &#39;asdfasdfasdaaaaaaaaaaaaaaaaaaaaaa&#39;;
?>

The following code is an overview of ThinkPHP caching method S()

thinkPHP’s F method can only be used to cache simple data Type, expiration and cache objects are not supported. The S() cache method supports validity period, also known as the dynamic cache method. The usage example is as follows:

The code is as follows:

// 使用data标识缓存$Data数据
S(&#39;data&#39;,$Data);  //前面的是缓存标示,后面的是缓存的数据

The code is as follows:

// 缓存$Data数据3600秒
S(&#39;data&#39;,$Data,3600);

The code is as follows:

// 删除缓存数据
S(&#39;data&#39;,NULL);  //第一个参数时缓存的标识名

The code is as follows:

$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[&#39;user_id&#39;])->find();
  $cache[$key]["nickname"]=$userlist[&#39;nickname&#39;];
  }
  S($cachename,$cache,3600); //设置缓存的生存时间 
  }
  S($cachename,NULL); //删除缓存

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Analysis on ThinkPHP’s implementation of static caching and dynamic caching

##

The above is the detailed content of ThinkPHP cache fast cache and dynamic cache. 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