thinkphp5使用redis
php环境使用的lnmp安装包
1.lnmp安装redis(如图)
进入lnmp解压后的目录,执行:./addons.sh install redis
2.thinkphp配置多种缓存
在config.php的cache数组下添加(如图)数组
- <?php
-
- 'redis' => [
- // 驱动方式
- 'type' => 'redis',
- // 服务器地址
- 'host' => '127.0.0.1',
- ],
3.这时候就index方法可以访问了,但是hello方法不能访问,
访问hello会说lPush,lgetrange未定义
- <?php
- namespace app\index\controller;
- use think\Cache;
- class Index
- {
- private $redis;
- public function __construct()
- {
- $this->redis = Cache::store('redis');
- }
-
- public function index()
- {
- $arr = ['proid'=>10,'proname'=>'xiaomi1'];
- $res = serialize($arr);
- $this->redis->set("order",$res);
- var_dump($this->redis->get("order"));
- }
- public function hello()
- {
- $arr = ['proid'=>10,'proname'=>'xiaomi1'];
- $res = serialize($arr);
- $this->redis->lPush("order",$res);
- var_dump($this->redis->lgetrange("aaa",0,1000));
- }
- }
- ?>
4.定义redis其他方法的访问(如图)
打开文件\thinkphp\library\think\cache\driver\Redis.php在后面追加
- <?php
- /**
- * 在list左边添加数据
- * @access
- * @param $key
- * @param $value
- * @return int
- * @author zsb
- */
- public function lPush($key,$value)
- {
- return $this->handler->lPush($key,$value);
- }
-
- /**
- * 查看键为$key的列表
- * @access
- * @param 数据键 $key
- * @param int $start
- * @param int $end
- * @return list
- * @author zsb
- */
- public function lgetrange($key,$start,$end)
- {
- return $this->handler->lgetrange($key,$start,$end);
- }
解压密码: detechn或detechn.com
免责声明
本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。