Home  >  Article  >  PHP Framework  >  How to use the s method in thinkphp

How to use the s method in thinkphp

藏色散人
藏色散人Original
2021-12-22 10:56:322245browse

The s method in thinkphp supports validity period, also known as dynamic caching method. Its usage method is such as "S('data',$Data);S('data',$Data,3600);S(' data',NULL)...".

How to use the s method in thinkphp

#The operating environment of this article: Windows 7 system, thinkphp v5.1 version, Dell G3 computer.

How to use the s method in thinkphp?

ThinkPHP cache method S() overview

thinkPHP’s F method can only be used to cache simple data types and does not support validity periods and cache objects. The S() cache method supports validity period, also known as dynamic cache method. The usage example is as follows:

The code is as follows:

// 使用data标识缓存$Data数据
S('data',$Data);  //前面的是缓存标示,后面的是缓存的数据
代码如下:
// 缓存$Data数据3600秒
S('data',$Data,3600);
代码如下:
// 删除缓存数据
S('data',NULL);  //第一个参数时缓存的标识名
代码如下:
$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); //删除缓存

Recommended learning: "The latest 10 thinkphp videos Tutorial

The above is the detailed content of How to use the s method in thinkphp. 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