thinkphp5使用redis

本文阅读 1 分钟
首页 Thinkphp笔记 正文

php环境使用的lnmp安装包
1.lnmp安装redis(如图)
进入lnmp解压后的目录,执行:./addons.sh install redis

2.thinkphp配置多种缓存
在config.php的cache数组下添加(如图)数组

  1. <?php
  2. 'redis' => [
  3. // 驱动方式
  4. 'type' => 'redis',
  5. // 服务器地址
  6. 'host' => '127.0.0.1',
  7. ],


3.这时候就index方法可以访问了,但是hello方法不能访问,
访问hello会说lPush,lgetrange未定义

  1. <?php
  2. namespace app\index\controller;
  3. use think\Cache;
  4. class Index
  5. {
  6. private $redis;
  7. public function __construct()
  8. {
  9. $this->redis = Cache::store('redis');
  10. }
  11. public function index()
  12. {
  13. $arr = ['proid'=>10,'proname'=>'xiaomi1'];
  14. $res = serialize($arr);
  15. $this->redis->set("order",$res);
  16. var_dump($this->redis->get("order"));
  17. }
  18. public function hello()
  19. {
  20. $arr = ['proid'=>10,'proname'=>'xiaomi1'];
  21. $res = serialize($arr);
  22. $this->redis->lPush("order",$res);
  23. var_dump($this->redis->lgetrange("aaa",0,1000));
  24. }
  25. }
  26. ?>

4.定义redis其他方法的访问(如图)
打开文件\thinkphp\library\think\cache\driver\Redis.php在后面追加

  1. <?php
  2. /**
  3. * 在list左边添加数据
  4. * @access
  5. * @param $key
  6. * @param $value
  7. * @return int
  8. * @author zsb
  9. */
  10. public function lPush($key,$value)
  11. {
  12. return $this->handler->lPush($key,$value);
  13. }
  14. /**
  15. * 查看键为$key的列表
  16. * @access
  17. * @param 数据键 $key
  18. * @param int $start
  19. * @param int $end
  20. * @return list
  21. * @author zsb
  22. */
  23. public function lgetrange($key,$start,$end)
  24. {
  25. return $this->handler->lgetrange($key,$start,$end);
  26. }
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

PHP 向关联数组指定的 Key 之前插入元素
« 上一篇 11-25
thinkphp5.1 关联查询多表查询
下一篇 » 12-01

发表评论