easyswoole/memcache-pool

coroutine memcache pool

1.0.0 2021-05-07 08:10 UTC

This package is auto-updated.

Last update: 2022-06-07 10:40:22 UTC


README

安装

composer require easyswoole/memcache-pool

注册

EasySwooleEvent.phpinitialize事件:

$config = new \EasySwoole\Memcache\Config();
$config->setHost('127.0.0.1');
$config->setPort(11211);

\EasySwoole\MemcachePool\MemcachePool::getInstance()->register($config,'default');

池的名字默认为default

获取池

/** @var \EasySwoole\MemcachePool\Pool $pool */
$pool = \EasySwoole\MemcachePool\MemcachePool::getInstance()->getPool('default');

需要自己回收池内对象.

pool具体用法请看连接池组件.

建议使用以下方案进行缓存defer、invoke;

Defer

/** @var \EasySwoole\Memcache\Memcache $client */
$client = \EasySwoole\MemcachePool\MemcachePool::defer('default');
$client->set('testKey','1');
$client->get('testKey');

连接池不存在,将会返回null.

协程结束后将会自动归还此链接到连接池当中.

Invoke

$ret = \EasySwoole\MemcachePool\MemcachePool::invoke(function (\EasySwoole\Memcache\Memcache $memcache){
    return $memcache->get('testKey');
},'default');

连接池不存在,将会返回null. 成功会返回回调函数的结果.

回调函数执行完,会自动归还此链接到连接池当中.