easyswoole3.5 redis使用

安装redis指定版本

composer require easyswoole/redis:^1.3
redis配置,dev.php
<?php

use EasySwoole\Log\LoggerInterface;

return [
    'SERVER_NAME' => "EasySwoole",
    'MAIN_SERVER' => [
        'LISTEN_ADDRESS' => '0.0.0.0',
        'PORT' => 9503,
        'SERVER_TYPE' => EASYSWOOLE_WEB_SERVER, //可选为 EASYSWOOLE_SERVER  EASYSWOOLE_WEB_SERVER EASYSWOOLE_WEB_SOCKET_SERVER
        'SOCK_TYPE' => SWOOLE_TCP,
        'RUN_MODEL' => SWOOLE_PROCESS,
        'SETTING' => [
            'worker_num' => 8,
            'reload_async' => true,
            'max_wait_time' => 3
        ],
        'TASK' => [
            'workerNum' => 4,
            'maxRunningNum' => 128,
            'timeout' => 15
        ]
    ],
    "LOG" => [
        'dir' => null,
        'level' => LoggerInterface::LOG_LEVEL_DEBUG,
        'handler' => null,
        'logConsole' => true,
        'displayConsole' => true,
        'ignoreCategory' => []
    ],
    'TEMP_DIR' => null,
    // 添加 Redis 及对应的连接池配置
    /*################ REDIS CONFIG ##################*/
    'REDIS' => [
        'host' => '127.0.0.1', // Redis 地址
        'port' => '6379', // Redis 端口
        'auth' => '', // Redis 密码
        'timeout' => 3.0, // Redis 操作超时时间
        'reconnectTimes' => 3, // Redis 自动重连次数
        'db' => 0, // Redis 库
    ],
];
EasySwooleEvent.php,加入预加载
   public static function initialize()
    {
        date_default_timezone_set('Asia/Shanghai');

        $rdConfig = new RedisConfig(Config::getInstance()->getConf('REDIS'));
        RedisPool::getInstance()->register($rdConfig);

    }

控制器使用

public function getOne()
	{
		$param = ContextManager::getInstance()->get('param');
        $redis =  RedisPool::defer();

        //redis 操作
        $redisData = $redis->get($this->key.$param['id']);
        if ($redisData) {
            $this->writeJson(Status::CODE_OK, json_decode($redisData, true), "获取数据成功.");
            return false;
        }
		$model = new StudentModel();
        try {
            $info = $model->where('id', $param['id'])->get();
        } catch (Exception|\EasySwoole\ORM\Exception\Exception|\Throwable $e) {
            $this->writeJson(Status::CODE_BAD_REQUEST, [], $e->getMessage());
            return false;
        }
        if (!$info) {
            $this->writeJson(400, [], '该数据不存在');
            return false;
        }

        $redis->set($this->key.$param['id'], json_encode($info, JSON_UNESCAPED_UNICODE), $this->time);
		$this->writeJson(Status::CODE_OK, $info, "获取数据成功.");
	}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值