memcache协程客户端

memcache协程客户端,由swoole 协程client实现

组件要求

  • easyswoole/spl: ^1.1

安装方法

composer require easyswoole/memcache

仓库地址

easyswoole/memcache

客户端调用

  1. $config = new \EasySwoole\Memcache\Config([
  2. 'host' => '127.0.0.1',
  3. 'port' => 11211
  4. ]);
  5. $client = new EasySwoole\Memcache\Memcache($config);

使用示例:

  1. $config = new \EasySwoole\Memcache\Config([
  2. 'host' => '127.0.0.1',
  3. 'port' => 11211
  4. ]);
  5. $client = new EasySwoole\Memcache\Memcache($config);
  6. $client->set('a',1);
  7. $client->get('a');

使用方法:

touch摸一下(刷新有效期)

  1. touch($key, $expiration, $timeout = null)

increment自增KEY

  1. increment($key, $offset = 1, $initialValue = 0, $expiration = 0, $timeout = null)

decrement自减KEY

  1. decrement($key, $offset = 1, $initialValue = 0, $expiration = 0, $timeout = null)

set设置KEY(覆盖)

  1. set($key, $value, $expiration = 0, $timeout = null)

add增加KEY(非覆盖)

  1. add($key, $value, $expiration = 0, $timeout = null)

replace替换一个KEY

  1. replace($key, $value, $expiration = 0, $timeout = null)

append追加数据到末尾

  1. append($key, $value, $timeout = null)

prepend追加数据到开头

  1. prepend($key, $value, $timeout = null)

get获取KEY

  1. get($key, $timeout = null)

delete删除一个key

  1. delete($key, $timeout = null)

stats获取服务器状态

  1. stats($type = null, $timeout = null)

version获取服务器版本

  1. version(int $timeout = null)

flush 清空缓存

  1. flush(int $expiration = null, int $timeout = null)

setMulti 存储多个元素

  1. public function setMulti(array $items, $expiration = null, $timeout = null)

getMulti 检索多个元素

  1. public function getMulti(array $keys, bool $isCas = false, $timeout = null)

cas 检查并设置

  1. 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

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Tioncico
  5. * Date: 2019/10/15 0015
  6. * Time: 14:46
  7. */
  8. namespace App\Pool;
  9. use EasySwoole\Memcache\Memcache;
  10. use EasySwoole\Pool\Config;
  11. use EasySwoole\Pool\AbstractPool;
  12. use EasySwoole\Memcache\Config as MemcacheConfig;
  13. class MemcachePool extends AbstractPool
  14. {
  15. protected $memcacheConfig;
  16. /**
  17. * 重写构造函数,为了传入memcache配置
  18. * RedisPool constructor.
  19. * @param Config $conf
  20. * @param MemcacheConfig $memcacheConfig
  21. * @throws \EasySwoole\Pool\Exception\Exception
  22. */
  23. public function __construct(Config $conf,MemcacheConfig $memcacheConfig)
  24. {
  25. parent::__construct($conf);
  26. $this->memcacheConfig = $memcacheConfig;
  27. }
  28. protected function createObject():Memcache
  29. {
  30. //根据传入的memcache配置进行new 一个memcache客户端
  31. $memcache = new Memcache($this->memcacheConfig);
  32. return $memcache;
  33. }
  34. }

注册到Manager中(在initialize事件中注册):

  1. $config = new \EasySwoole\Pool\Config();
  2. $memcacheConfig1 = new \EasySwoole\Memcache\Config(Config::getInstance()->getConf('MEMCACHE1'));
  3. \EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\MemcachePool($config,$memcacheConfig1),'memcache1');
  4. $memcacheConfig2 = new \EasySwoole\Memcache\Config(Config::getInstance()->getConf('MEMCACHE2'));
  5. \EasySwoole\Pool\Manager::getInstance()->register(new \App\Pool\MemcachePool($config,$memcacheConfig2),'memcache2');

调用(可在控制器中全局调用):

  1. go(function (){
  2. $memcachePool1 = Manager::getInstance()->get('memcache1');
  3. $memcachePool2 = Manager::getInstance()->get('memcache2');
  4. $memcache1 = $memcachePool1->getObj();
  5. $memcache2 = $memcachePool2->getObj();
  6. var_dump($memcache1->set('name', '仙士可1'));
  7. $this->response()->write($memcache1->get('name'));
  8. var_dump($memcache2->set('name', '仙士可2'));
  9. $this->response()->write($memcache2->get('name'));
  10. //回收对象
  11. $memcachePool1->recycleObj($memcache1);
  12. $memcachePool2->recycleObj($memcache2);
  13. });