easyswoole / memcache-pool
coroutine memcache pool
1.0.0
2021-05-07 08:10 UTC
Requires
- easyswoole/component: ^2.2
- easyswoole/memcache: ^1.1
- easyswoole/pool: ^1.0
Requires (Dev)
- easyswoole/phpunit: ^1.0
This package is auto-updated.
Last update: 2024-11-07 16:43:36 UTC
README
安装
composer require easyswoole/memcache-pool
注册
EasySwooleEvent.php
的initialize
事件:
$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
.
成功会返回回调函数的结果.
回调函数执行完,会自动归还此链接到连接池当中.