easyswoole框架redis的配置和简单应用

第一:dev.php 中配置redis

    /*################ REDIS CONFIG ##################*/
    'REDIS' => [
        'host'          => '127.0.0.1',
        'port'          => '6379',
        'auth'          => '',
        'serialize' => \EasySwoole\Redis\Config\RedisConfig::SERIALIZE_NONE
    ],

第二步:EasySwooleEvent.php中

    public static function mainServerCreate(EventRegister $register)
    {
        // TODO: Implement mainServerCreate() method.
        //redis 连接
        $config = new \EasySwoole\Pool\Config();
        $redisConfig1 = new \EasySwoole\Redis\Config\RedisConfig(GlobalConfig::getInstance()->getConf('REDIS'));
        \EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\RedisPool($config,$redisConfig1),'redis');
    }

第三步:创建文件=》App/Pool/RedisPool.php


/**
 * Created by PhpStorm.
 * User: Tioncico
 * Date: 2019/10/15 0015
 * Time: 14:46
 */

namespace App\Pool;

use EasySwoole\Pool\Config;
use EasySwoole\Pool\AbstractPool;
use EasySwoole\Redis\Config\RedisConfig;
use EasySwoole\Redis\Redis;

class RedisPool extends AbstractPool
{
    protected $redisConfig;

    /**
     * 重写构造函数,为了传入redis配置
     * RedisPool constructor.
     * @param Config      $conf
     * @param RedisConfig $redisConfig
     * @throws \EasySwoole\Pool\Exception\Exception
     */
    public function __construct(Config $conf,RedisConfig $redisConfig)
    {
        parent::__construct($conf);
        $this->redisConfig = $redisConfig;
    }

    protected function createObject()
    {
        //根据传入的redis配置进行new 一个redis
        $redis = new Redis($this->redisConfig);
        return $redis;
    }
}

第四步:应用

    public function redisOne()
    {
        //开启
        $redis=\EsySwoole\Pool\Manager::getInstance()->get('redis')->getObj();
        if(!$res=$redis->get("name")){
            $redis->set("name","zq");
            $redis->expire("name",10);
        }
        $res=$redis->get("name");
        //回收
        \EsySwoole\Pool\Manager::getInstance()->get('redis')->recycleObj($redis);
        return $this->apiSuccess($res);
    }

你可能感兴趣的:(easyseoole,php)