zoukankan      html  css  js  c++  java
  • thinkphp6:访问多个redis数据源(thinkphp6.0.5 / php 7.4.9)

    一,配置多个redis库的地址:

    .env

    APP_DEBUG = true
    #APP_DEBUG = false
    
    [APP]
    DEFAULT_TIMEZONE = Asia/Shanghai
    
    [REDIS0]
    TYPE = redis
    HOST = 127.0.0.1
    PORT = 6379
    PASSWORD =
    
    [REDIS1]
    TYPE = redis
    HOST = 127.0.0.1
    PORT = 6380
    PASSWORD =
    
    [LANG]
    default_lang = zh-cn

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

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

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

    二,配置php到redis的连接

     config/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,
            ],
            // 更多的缓存连接
            'redis1'    =>    [
                'type'     => env('redis1.type', 'redis'),
                'host'     => env('redis1.host', '127.0.0.1'),
                'port'     => env('redis1.port', '6380'),
                'password' => env('redis1.password', ''),
                'select'   => '0',
                // 全局缓存有效期(0为永久有效)
                'expire'   => 0,
                // 缓存前缀
                'prefix'   => '',
                'timeout'  => 0,
            ],
        ],
    ];

    三,如何访问多个redis数据源,看例子:

    api/controller/UserController.php

    <?php
    namespace appapicontroller;
    
    use thinkfacadeCache;
    
    
    class UserController
    {
        public function index()
        {
            //访问redis0
            Cache::store('redis0')->set('name0','value1234',3600);
            $value0 = Cache::store('redis0')->get("name0");
            //访问redis1
            Cache::store('redis1')->set('name1','value5678',3600);
            $value1 = Cache::store('redis1')->get("name1");
            //返回
            return '您好![userindex],<br/>redis0 value0:'.$value0."<br/>redis1 value1:".$value1;
        }
    
        public function test() {
            return "this is usercontroller test";
        }
    }

    四,测试效果

    1,访问:

    http://127.0.0.1:81/api/userindex

    返回:

    2,从命令行查看redis中保存的数据:

    root@ku:/data# /usr/local/soft/redis6/bin/redis-cli -p 6380
    127.0.0.1:6380> get name1
    "s:9:"value5678";"
    127.0.0.1:6380> exit
    root@ku:/data# /usr/local/soft/redis6/bin/redis-cli -p 6379
    127.0.0.1:6379> get name0
    "s:9:"value1234";"

    可以看到数据已保存到redis缓存中

    五,查看thinkphp的版本

    liuhongdi@ku:/data/php/mytp$ php think version
    v6.0.5

    六,查看php的版本:

    liuhongdi@ku:/data/logs/phplogs/tlog/202012$ php --version
    PHP 7.4.9 (cli) (built: Oct 26 2020 15:17:14) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
        with Zend OPcache v7.4.9, Copyright (c), by Zend Technologies
  • 相关阅读:
    Convert Date between LocalDateTime
    Java 8 Date Time API Example Tutorial – LocalDate, Instant, LocalDateTime, Parse and Format
    Enable HTTPS in Spring Boot
    设定范围和步长的递增数验证器Validator
    Codeforces Round #392(div 2) 758D (贪心)
    机器学习:决策树算法(简单尝试)
    Codeforces Round #388 (Div. 2) 749E(巧妙的概率dp思想)
    Codeforces Round #364 (Div. 1) 700B(树)
    Codeforces Round #389 (Div. 2) 752F(树的权值重心)
    Codeforces Round #389 (Div. 2) 752E(二分答案)
  • 原文地址:https://www.cnblogs.com/architectforest/p/14212236.html
Copyright © 2011-2022 走看看