Home > Backend Development > PHP Tutorial > thinkphp write read cache example

thinkphp write read cache example

小云云
Release: 2023-03-21 18:32:01
Original
2228 people have browsed it

thinkphp has an S() method that can quickly write to the cache. This article mainly shares with you examples of thinkphp writing and reading cache, hoping to help everyone.

The code is below. It is very simple. You can copy it and use it. I will try my best to be as detailed as possible and use colors to distinguish it clearly.

Let’s talk about the cache reference method first:

The general idea should be 1. First determine whether the cache exists or not 2. If not, create it


The cache location is usually in the temp folder of the cache folder. Newbies can search if they can’t find it. There are only a few.

If you don’t know how to search, I will give you a screenshot:



Look at the cache files inside


// address只是个标识你可以随便写,注意红体字一样就好
if(!S('address')){         // 判断缓存是否存在  如果缓存不存在就:   
         $address_model=M("common_address");       //  实例化表
         $list=$address_model   //  查询到的二维数组
            ->field('latitude,longitude,address_id')
            ->where($where)
            ->select();
            S('address',$list,array('type'=>'file','expire'=>600));   // 写入缓存,expire'=>600 :  设置有效时间:600秒
 }else{ // 如果缓存 存在
           $address = S('address');// 获取缓存
            dump($address); // 打印出来
 }
 
       // S($cachename,NULL); // 删除缓存; 一般不用删,删了下次创建还会有,缓存过期再次访问时他会自动替换原来的缓存
Copy after login

The above is the detailed content of thinkphp write read cache example. 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