多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 一,在.env中配置redis信息: ~~~ [REDIS0] TYPE = redis HOST = 127.0.0.1 PORT = 6379 PASSWORD = ~~~ 说明:刘宏缔的架构森林是一个专注架构的博客,地址:[https://www.cnblogs.com/architectforest](https://www.cnblogs.com/architectforest)          对应的源码可以访问这里获取: [https://github.com/liuhongdi/ ](https://github.com/liuhongdi/)         或: [https://gitee.com/liuhongdi](https://gitee.com/liuhongdi) 说明:作者:刘宏缔 邮箱: 371125307@qq.com ## 二,编写代码访问redis 1,在config/cache添加对redis0的访问: cache.php [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ~~~ <?php // +---------------------------------------------------------------------- // | 缓存设置 // +---------------------------------------------------------------------- return [ // 默认缓存驱动 'default' => env('cache.driver', 'file'), // 缓存连接方式配置 'stores' => [ 'file' => [ // 驱动方式 'type' => 'File', // 缓存保存目录 'path' => '', // 缓存前缀 'prefix' => '', // 缓存有效期 0表示永久缓存 'expire' => 0, // 缓存标签前缀 'tag_prefix' => 'tag:', // 序列化机制 例如 ['serialize', 'unserialize'] 'serialize' => [], ], // 更多的缓存连接 // 更多的缓存连接 'redis0' => [ 'type' => env('redis0.type', 'redis'), 'host' => env('redis0.host', '127.0.0.1'), 'port' => env('redis0.port', '6379'), 'password' => env('redis0.password', ''), 'select' => '0', // 全局缓存有效期(0为永久有效) 'expire' => 0, // 缓存前缀 'prefix' => '', 'timeout' => 0, ], ], ]; ~~~ [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") 2,使用redis 添加controller ~~~ liuhongdi@lhdpc:/data/php/admapi$ php think make:controller Article Controller:app\controller\Article created successfully.  ~~~ 增加代码: [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ~~~ <?php declare (strict_types = 1); namespace app\controller; use app\result\Result; use think\Request; use think\facade\Cache; class Article { /** * 在redis写入并读取数据 * * @return \think\Response */ public function index() { Cache::store('redis0')->set('name0','value1234',3600); $value0 = Cache::store('redis0')->get("name0"); $data = ["name0"=>$value0]; return Result::Success($data); } } ~~~ [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") 3,result代码: Result.php [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ~~~ <?php namespace app\result; use think\response\Json; class Result { //success static public function Success($data):Json { $rs = [ 'code'=>0, 'msg'=>"success", 'data'=>$data, ]; return json($rs); } //error static public function Error($code,$msg):Json { $rs = [ 'code'=>$code, 'msg'=>$msg, 'data'=>"", ]; return json($rs); } } ~~~ [![复制代码](https://common.cnblogs.com/images/copycode.gif)](javascript:void(0); "复制代码") ## 三,遇到报错: ~~~ throw new \BadFunctionCallException('not support: redis'); ~~~ thinkphp抛出异常: not support: redis 说明没有安装php访问redis的驱动模块, 参考这一篇安装即可: ~~~ https://www.cnblogs.com/architectforest/p/15737959.html  ~~~ ## 四,测试效果 访问: ~~~ http://192.168.219.6:8000/article/index ~~~ 返回如图: ![](https://img2020.cnblogs.com/blog/1938691/202112/1938691-20211227213710414-340229451.png) ## 五,查看thinkphp和php的版本: ~~~ root@lhdpc:~# php --version PHP 8.0.14 (cli) (built: Dec 23 2021 11:52:42) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.14, Copyright (c) Zend Technologies with Zend OPcache v8.0.14, Copyright (c), by Zend Technologies ~~~ ~~~ root@lhdpc:~# cd /data/php/admapi/ root@lhdpc:/data/php/admapi# php think version v6.0.9 ~~~