PHP code example of easyswoole-api / redis-pool

1. Go to this page and download the library: Download easyswoole-api/redis-pool library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

easyswoole-api / redis-pool example snippets


//redis连接池注册(config默认为127.0.0.1,端口6379)
\EasySwoole\RedisPool\RedisPool::getInstance()->register(new \EasySwoole\Redis\Config\RedisConfig(),'redis');

//redis集群连接池注册
\EasySwoole\RedisPool\RedisPool::getInstance()->register(new \EasySwoole\Redis\Config\RedisClusterConfig([
        ['172.16.253.156', 9001],
        ['172.16.253.156', 9002],
        ['172.16.253.156', 9003],
        ['172.16.253.156', 9004],
    ]
),'redisCluster');

$redisPoolConfig = \EasySwoole\RedisPool\RedisPool::getInstance()->register(new \EasySwoole\Redis\Config\RedisConfig());
//配置连接池连接数
$redisPoolConfig->setMinObjectNum(5);
$redisPoolConfig->setMaxObjectNum(20);

$redisClusterPoolConfig = \EasySwoole\RedisPool\RedisPool::getInstance()->register(new \EasySwoole\Redis\Config\RedisClusterConfig([
        ['172.16.253.156', 9001],
        ['172.16.253.156', 9002],
        ['172.16.253.156', 9003],
        ['172.16.253.156', 9004],
    ]
));
//配置连接池连接数
$redisPoolConfig->setMinObjectNum(5);
$redisPoolConfig->setMaxObjectNum(20);

//defer方式获取连接
$redis = \EasySwoole\RedisPool\RedisPool::defer();
$redisCluster = \EasySwoole\RedisPool\RedisPool::defer();
$redis->set('a', 1);
$redisCluster->set('a', 1);

//invoke方式获取连接
\EasySwoole\RedisPool\RedisPool::invoke(function (\EasySwoole\Redis\Redis $redis) {
    var_dump($redis->set('a', 1));
});
\EasySwoole\RedisPool\RedisPool::invoke(function (\EasySwoole\Redis\Redis $redis) {
    var_dump($redis->set('a', 1));
});

//获取连接池对象
$redisPool = \EasySwoole\RedisPool\RedisPool::getInstance()->getPool();
$redisClusterPool = \EasySwoole\RedisPool\RedisPool::getInstance()->getPool();

$redis = $redisPool->getObj();
$redisPool->recycleObj($redis);