How to use s method in thinkphp
This article will explain in detail how to use the s method in thinkphp, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.
thinkphp s method is a caching method that supports validity, also known as dynamic caching method, which uses the format "S ('data ',$Data);" to indicate that $Data data is cached using the data identifier.
Operating environment: Windows 7 system, thinkphp v5.1 version, Dell G3 computer.
How does thinkphp s work?
Thinkphp S() cache method detailed explanation
ThinkPHP's F method can only be used to cache simple data types, and does not support validity and cache objects. S() cache method supports validity period, also known as dynamic cache method. Examples are as follows:
The code is as follows:
//cache $Data S ('data ',$Data) using data identifier; //cache identifier before, cached data after
The code is as follows:
//cache $Data for 3600 seconds S ('data ',$Data,3600); code is as follows: //delete cached data S ('data',NULL); //cache ID code for first argument is as follows: $cache=S($cachename);//set cache flag//determine whether there is this query cache if(!$ cache){ //$cache is the cache flag (each query corresponds to a cache, i.e. different queries have different caches) $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 cache lifetime } S($cachename,NULL); //delete cache
Example Details
$cache=S ('ii ');//Set cache flag if(!$ cache){ $newItems = ["1056018","1127006", "1127041", "1221004", "1223005", "1280012", "1116030", "1116031", "1143011", "1143016" , "1143021" , "1156099" , "1252025" , "1275011" , "1275015" , "1116016"]; S ('ii ',$Itemnews,15); //Set cache lifetime }else{ $newItems = $cache; }
About "how to use s method in thinkphp" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.