thinkphp5简单使用redis缓存
<?php namespace app\index\controller; use think\Controller; use think\Cache\Driver\Redis; class Index extends Controller { //redis public function redis($userId) { //加载扩展 $redis = new \Redis(); //连接扩展 $redis->connect("127.0.0.1","6379"); //判断是否过期 $redis_status = $redis->exists("info"); if($redis_status==false){ //缓存失效,重新存入 //查询数据 $Info = db("user")->where('id', $userId)->find(); //转换成字符串,有利于存储 $redisInfo = serialize($Info); //存入缓存 $redis->set("info",$redisInfo); //设置缓存周期,60秒 $redis->expire("info",60); var_dump("新存入的值"); } //获取缓存 $result = unserialize($redis->get("info")); //当前时间 var_dump(date("Y-m-d H:i:s",time())); var_dump($result); } }