zoukankan      html  css  js  c++  java
  • thinkphp6:访问redis6(thinkphp 6.0.9/php 8.0.14)

    一,在.env中配置redis信息:

    [REDIS0]
    TYPE = redis
    HOST = 127.0.0.1
    PORT = 6379
    PASSWORD =

    说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

             对应的源码可以访问这里获取: https://github.com/liuhongdi/
             或: https://gitee.com/liuhongdi

    说明:作者:刘宏缔 邮箱: 371125307@qq.com

    二,编写代码访问redis

    1,在config/cache添加对redis0的访问:
    cache.php
    <?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,
            ],
        ],
    ];
    2,使用redis
    添加controller
    liuhongdi@lhdpc:/data/php/admapi$ php think make:controller Article
    Controller:app\controller\Article created successfully. 
    增加代码:
    <?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);
        }
    }
     
    3,result代码:
    Result.php
    <?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);
        }
    }

    三,遇到报错:

    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
    返回如图:

    五,查看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
  • 相关阅读:
    删除List集合中的元素你碰到过这样的陷阱吗?
    从spring框架中的事件驱动模型出发,优化实际应用开发代码
    SpringBoot启动原理及相关流程
    基于SpringBoot实现定时任务的设置(常用:定时清理数据库)
    C#开发中常用的加密解密方法
    http://go.microsoft.com/fwlink/?linkid问题
    移动端开发必须知道的小技巧
    工作中遇到的细节问题总结(二)
    redis分布式锁和消息队列
    join和wait
  • 原文地址:https://www.cnblogs.com/architectforest/p/15737997.html
Copyright © 2011-2022 走看看