28、Hyperf 3 快速使用 - Hyperf 3 协程 Redis 客户端的基础使用

作者: 温新

分类: 【Hyperf 3 基础系列】

阅读: 578

时间: 2023-04-25 16:03:58

hi,我是温新,一名 PHPer

Hypref 版本:Hyperf 3.0

学习目标:学习 Hyperf 3 协程 Redis 客户端的基础使用

Hyperf 3 提供的缓存默认使用的是 Redis,但是想具体使用 Redis 的一些类型就需要安装组件。本篇文章通过组件来使用 Redis。在使用 Redis 之前确保已经安装 Redis 软件。

第一步:安装组件

composer require hyperf/redis

第二步:配置文件

配置文件位于:config/autoload/redis.php,内容如下:

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

return [
    'default' => [
        'host' => env('REDIS_HOST', 'localhost'),
        'auth' => env('REDIS_AUTH', null),
        'port' => (int)env('REDIS_PORT', 6379),
        'db'   => (int)env('REDIS_DB', 0),
        '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),
        ],
    ],
];

配置选项如下:

配置项 类型 默认值 备注
host string 'localhost' Redis 地址
auth string 密码
port integer 6379 端口
db integer 0 DB
cluster.enable boolean false 是否集群模式
cluster.name string null 集群名
cluster.seeds array [] 集群连接地址数组 ['host:port']
pool object {} 连接池配置
options object {} Redis 配置选项

发布配置文件:

php bin/hyperf.php vendor:publish hyperf/redis

默认已经有了 redis.php 配置文件。

第三步:使用 Redis

1、使用方式一:通过注解使用

<?php

namespace App\Controller\Test;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\Redis\Redis;

#[Controller]
class RedisClientController
{
    #[Inject]
    protected Redis $redis;

    #[GetMapping('/redis/store')]
    public function store()
    {
        $this->redis->set('name', '王美丽');
        
        return $this->redis->get('name');
    }
}

测试:

$ curl http://192.168.31.90:9501/redis/store
王美丽

2、通过容器接口构造实例对象使用

<?php

namespace App\Controller\Test;

use Hyperf\Contract\ContainerInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\Redis\Redis;

#[Controller]
class RedisClientController
{
    protected $redis;

    public function __construct(protected ContainerInterface $container)
    {
        $this->redis = $this->container->get(Redis::class);
    }

    #[GetMapping('/redis/store')]
    public function store()
    {
        $this->redis->set('name', '郝帅');
        
        return $this->redis->get('name');
    }
}

测试

$ curl http://192.168.31.90:9501/redis/store
郝帅

3、通过获取容器对象实现

<?php

namespace App\Controller\Test;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;

#[Controller]
class RedisClientController
{
    protected $redis;

    public function __construct()
    {
        $container   = ApplicationContext::getContainer();
        $this->redis = $container->get(Redis::class);
    }

    #[GetMapping('/redis/store')]
    public function store()
    {
        $this->redis->set('name', '王大丽');

        return $this->redis->get('name');
    }
}

测试

$ curl http://192.168.31.90:9501/redis/store
王大丽

这里只是简单演示了字符串类型,关于更多类型自己动手测试。

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

请登录后再评论