29、Hyperf 3 快速使用 - Hyperf 3 协程 Redis 多库的使用

作者: 温新

分类: 【Hyperf 3 基础系列】

阅读: 556

时间: 2023-04-25 16:05:28

hi,我是温新,一名 PHPer

Hypref 版本:Hyperf 3.0

学习目标:学习 Hyperf 3 协程 Redis 多库使用

上篇文章学习了 redis 的使用,对于切换数据库,可以如下方式进行数据库的切换:

# 切换数据库
$this->redis->select(1);

Hyperf 提供了通过配置文件来使用。

同服务器的基础使用

这个案例是基于同一台服务器不同的库使用。

第一步:修改配置文件

<?php
// config/autoload/redis.php
declare(strict_types=1);

return [
    // 此处省略默认 defalut 代码
    
    // 新一个名为 test 的 redis 连接池
    'test' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'auth' => env('REDIS_AUTH', ''),
        'port' => (int)env('REDIS_PORT', 6379),
        // 连接数据库 1
        'db'   => 1,
        'pool' => [
            'min_connections' => 1,
            'max_connections' => 10,
            'connect_timeout' => 10.0,
            'wait_timeout'    => 3.0,
            'heartbeat'       => -1,
            'max_idle_time'   => (float)env('REDIS_MAX_IDLE_TIME', 60),
        ],
    ],
];

第二步:新代理类

新增一个 TestRedis 类并继承 Hyperf\Redis\Redis 类,修改 poolName 的名字为配置文件中的 test,这样就完成了连接池的切换。

<?php
// App\Controller\Server\TestRedis.php
namespace App\Controller\Server;

use Hyperf\Redis\Redis;

class TestRedis extends Redis
{
    // 对应配置文件中的 test
    protected string $poolName = 'test';
}

第三步:使用 redis

1、方法一:通过依赖注入方式使用

<?php
// App\Controller\Test\TestRedisController.php
namespace App\Controller\Test;

use App\Controller\Server\TestRedis;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;

#[Controller]
class TestRedisController
{
    // 依赖注入 redis
    #[GetMapping('/tsredis/store')]
    public function store(TestRedis $redis)
    {
        $redis->set('webName', '自如初');

        return $redis->get('webName');
    }
}

测试

$ curl http://192.168.31.90:9501/tsredis/store
自如初

2、方式二:通过容器对象使用

<?php
// App\Controller\Test\TestRedisController.php
    
use Hyperf\Contract\ContainerInterface;

#[GetMapping('/tsredis/store')]
public function store(ContainerInterface $container)
{
    $redis = $container->get(TestRedis::class);
    $redis->set('webName', '自如初博客');

    return $redis->get('webName');
}

3、方式三:通过工厂类使用

<?php
// App\Controller\Test\TestRedisController.php

#[GetMapping('/tsredis/store')]
public function store(ApplicationContext $applicationContext)
{
    $container = $applicationContext::getContainer();
    $redis     = $container->get(RedisFactory::class)->get('test');

    $redis->set('nickname', '王丽丽');

    return $redis->get('nickname');
}

测试

$ curl http://192.168.31.90:9501/tsredis/store
王丽丽

不同服务器的使用

既然可以操作同一台服务器的不同库,那么也能操作其他机器上的 redis。在操作之前有些准备工作要做。

第一步:相关说明

1、两台机器上的 redis,IP 分别为 192.168.31.90,192.168.31.92

2、在 90 这台机器上操作 92 上的 redis

3、修改 92 这台机器上的 redis.conf,取消保护模式(测试用,生产环境不建议),修改如下:

# 文件: 192.168.31.92  redis.conf
# 取消保护模式
protected-mode no
# 允许连接(生产环境不建议)
bind 0.0.0.0

配置完成后,后续操作都是在 90 这台服务器的 hyperf 项目中。

第二步:修改配置文件

<?php
// config/autoload/redis.php
declare(strict_types=1);

return [
    // 此处省略默认 defalut 代码
    
    // 另一台 redis 服务器
    'redis31' => [
        'host' => env('REDIS_HOST_31', 'localhost'),
        'auth' => env('REDIS_AUTH_31', ''),
        'port' => (int)env('REDIS_PORT_31', 6379),
        // 连接数据库 1
        'db'   => 1,
        'pool' => [
            'min_connections' => 1,
            'max_connections' => 10,
            'connect_timeout' => 10.0,
            'wait_timeout'    => 3.0,
            'heartbeat'       => -1,
            'max_idle_time'   => (float)env('REDIS_MAX_IDLE_TIME', 60),
        ],
    ],
];

第三步:修改 .env 文件

REDIS_HOST_31=192.168.31.92
REDIS_AUTH_31=(null)
REDIS_PORT_31=6379

第四步:添加代理类

<?php
// App\Controller\Server\TestRedis31.php
    
namespace App\Controller\Server;

use Hyperf\Redis\Redis;

class TestRedis31 extends Redis
{
    protected string $poolName = 'redis31';
}

第五步:控制器使用

<?php
// App\Controller\Test\TestRedis31Controller.php
namespace App\Controller\Test;

use App\Controller\Server\TestRedis31;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;

#[Controller]
class TestRedis31Controller
{
    #[GetMapping('/tsredis31/store')]
    public function store(TestRedis31 $redis31)
    {
        $redis31->set('new_name', '真美丽');

        return $redis31->get('new_name');
    }
}

测试

$ http://192.168.31.90:9501/tsredis31/store/store
真美丽

关于其他使用方式都是一样了,参考基础使用中的方法。

我是温新,本篇文章结束。

请登录后再评论