Home > PHP Framework > ThinkPHP > body text

How to use thinkphp s method

藏色散人
Release: 2021-12-20 09:41:12
Original
2253 people have browsed it

thinkphp s method is a caching method that supports validity period, also known as dynamic caching method. Its usage format is such as "S('data',$Data);", which means using the data identifier to cache $Data data.

How to use thinkphp s method

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

How to use thinkphp s method?

Detailed explanation of the S() cache method in thinkphp

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 the dynamic cache method. The usage example is as follows:

The code is as follows:

// 使用data标识缓存$Data数据
S('data',$Data);  //前面的是缓存标示,后面的是缓存的数据
Copy after login

The code is as follows:

// 缓存$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); //删除缓存
Copy after login

Example details

 $cache=S('ii');//设置缓存标示
 if(!$cache){
     $newItems = ["1056018","1127006", "1127041", "1221004", "1223005", "1280012", "1116030", "1116031", "1143011", "1143016" , "1143021" , "1156099" , "1252025" , "1275011" , "1275015" , "1116016"];
            
     S('ii',$newItems,15); //设置缓存的生存时间
 
  }else{
      $newItems = $cache;
   }
Copy after login

Recommended learning: "PHP Video Tutorial" "The latest 10 thinkphp Video tutorial

The above is the detailed content of How to use thinkphp s method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template