memcache協程客戶端
memcache協程客戶端,由swoole 協程client實現
組件要求
- easyswoole/spl: ^1.1
安裝方法
composer require easyswoole/memcache
倉庫地址
客戶端調用
$config = new \EasySwoole\Memcache\Config([
'host' => '127.0.0.1',
'port' => 11211
]);
$client = new EasySwoole\Memcache\Memcache($config);
使用示例:
$config = new \EasySwoole\Memcache\Config([
'host' => '127.0.0.1',
'port' => 11211
]);
$client = new EasySwoole\Memcache\Memcache($config);
$client->set('a',1);
$client->get('a');
使用方法:
touch摸一下(刷新有效期)
touch($key, $expiration, $timeout = null)
increment自增KEY
increment($key, $offset = 1, $initialValue = 0, $expiration = 0, $timeout = null)
decrement自減KEY
decrement($key, $offset = 1, $initialValue = 0, $expiration = 0, $timeout = null)
set設置KEY(覆蓋)
set($key, $value, $expiration = 0, $timeout = null)
add增加KEY(非覆蓋)
add($key, $value, $expiration = 0, $timeout = null)
replace替換一個KEY
replace($key, $value, $expiration = 0, $timeout = null)
append追加數據到末尾
append($key, $value, $timeout = null)
prepend追加數據到開頭
prepend($key, $value, $timeout = null)
get獲取KEY
get($key, $timeout = null)
delete刪除一個key
delete($key, $timeout = null)
stats獲取服務器狀態(tài)
stats($type = null, $timeout = null)
version獲取服務器版本
version(int $timeout = null)
flush 清空緩存
flush(int $expiration = null, int $timeout = null)
setMulti 存儲多個元素
public function setMulti(array $items, $expiration = null, $timeout = null)
getMulti 檢索多個元素
public function getMulti(array $keys, bool $isCas = false, $timeout = null)
cas 檢查并設置
public function cas(float $casToken, string $key, $value, int $expiration = null, $timeout = null)
進階使用
Memcache連接池示例
安裝 easyswoole/pool 組件
composer require easyswoole/pool
具體pool相關詳細用法可查看 連接池
新增MemcachePool管理器
新增文件/App/Pool/MemcachePool.php
<?php
/**
* Created by PhpStorm.
* User: Tioncico
* Date: 2019/10/15 0015
* Time: 14:46
*/
namespace App\Pool;
use EasySwoole\Memcache\Memcache;
use EasySwoole\Pool\Config;
use EasySwoole\Pool\AbstractPool;
use EasySwoole\Memcache\Config as MemcacheConfig;
class MemcachePool extends AbstractPool
{
protected $memcacheConfig;
/**
* 重寫構造函數,為了傳入memcache配置
* RedisPool constructor.
* @param Config $conf
* @param MemcacheConfig $memcacheConfig
* @throws \EasySwoole\Pool\Exception\Exception
*/
public function __construct(Config $conf,MemcacheConfig $memcacheConfig)
{
parent::__construct($conf);
$this->memcacheConfig = $memcacheConfig;
}
protected function createObject():Memcache
{
//根據傳入的memcache配置進行new 一個memcache客戶端
$memcache = new Memcache($this->memcacheConfig);
return $memcache;
}
}
注冊到Manager中(在initialize
事件中注冊):
$config = new \EasySwoole\Pool\Config();
$memcacheConfig1 = new \EasySwoole\Memcache\Config(Config::getInstance()->getConf('MEMCACHE1'));
\EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\MemcachePool($config,$memcacheConfig1),'memcache1');
$memcacheConfig2 = new \EasySwoole\Memcache\Config(Config::getInstance()->getConf('MEMCACHE2'));
\EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\MemcachePool($config,$memcacheConfig2),'memcache2');
調用(可在控制器中全局調用):
go(function (){
$memcachePool1 = Manager::getInstance()->get('memcache1');
$memcachePool2 = Manager::getInstance()->get('memcache2');
$memcache1 = $memcachePool1->getObj();
$memcache2 = $memcachePool2->getObj();
var_dump($memcache1->set('name', '仙士可1'));
$this->response()->write($memcache1->get('name'));
var_dump($memcache2->set('name', '仙士可2'));
$this->response()->write($memcache2->get('name'));
//回收對象
$memcachePool1->recycleObj($memcache1);
$memcachePool2->recycleObj($memcache2);
});