hyperf如何操作redis

发布时间:2021-12-28 10:00:11 阅读:2047次

在hyperf中如何使用redis缓存

1、首先修改.env文件配置redis服务器参数

REDIS_HOST=127.0.0.1
REDIS_AUTH=(null)
REDIS_PORT=6379
REDIS_DB=1

2、修改Controller文件

<?php

declare(strict_types=1);
namespace App\Controller;
use Hyperf\Utils\ApplicationContext;

class RedisController extends AbstractController
{
    private $redis;
    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        $container = ApplicationContext::getContainer();
        $this->redis = $container->get(\Redis::class);

        $this->redis->set('name','value');
        $user.=$this->redis->get('name');


        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }
}

3、打开config/routes.php,添加路由

Router::get('/redis', 'App\Controller\RedisController@index');

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:hyperf如何操作redis 出自老鄢博客 | 欢迎分享