PHP code example of workbunny / webman-shared-cache

1. Go to this page and download the library: Download workbunny/webman-shared-cache 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/ */

    

workbunny / webman-shared-cache example snippets


  $result = [];
  # 默认正则匹配 - 以50条为一次分片查询
  \Workbunny\WebmanSharedCache\Cache::Search('/^abc.+$/', function ($key, $value) use (&$result) { 
      $result[$key] = $value;
  }, 50);
  
  # 通配符转正则
  \Workbunny\WebmanSharedCache\Cache::Search(
      \Workbunny\WebmanSharedCache\Cache::WildcardToRegex('abc*'),
      function ($key, $value) use (&$result) {
          $result[$key] = $value;
      }
  );
  

  # key-1、key-2、key-3会被当作一次原子性操作
  
  # 非阻塞执行 - 成功执行则返回true,失败返回false,锁冲突会导致执行失败
  $result = \Workbunny\WebmanSharedCache\Cache::Atomic('lock-test', function () { 
      \Workbunny\WebmanSharedCache\Cache::Set('key-1', 1);
      \Workbunny\WebmanSharedCache\Cache::Set('key-2', 2);
      \Workbunny\WebmanSharedCache\Cache::Set('key-3', 3);
  });
  # 阻塞等待执行 - 默认阻塞受Cache::$fuse阻塞保险影响
  $result = \Workbunny\WebmanSharedCache\Cache::Atomic('lock-test', function () { 
      \Workbunny\WebmanSharedCache\Cache::Set('key-1', 1);
      \Workbunny\WebmanSharedCache\Cache::Set('key-2', 2);
      \Workbunny\WebmanSharedCache\Cache::Set('key-3', 3);
  }, true);
  # 自行实现阻塞
  $result = false
  while (!$result) {
      # TODO 可以适当增加保险,以免超长阻塞
      $result = \Workbunny\WebmanSharedCache\Cache::Atomic('lock-test', function () { 
          \Workbunny\WebmanSharedCache\Cache::Set('key-1', 1);
          \Workbunny\WebmanSharedCache\Cache::Set('key-2', 2);
          \Workbunny\WebmanSharedCache\Cache::Set('key-3', 3);
      });
  }
  

  # 全量数据
  var_dump(\Workbunny\WebmanSharedCache\Cache::Info());
  
  # 不查询数据
  var_dump(\Workbunny\WebmanSharedCache\Cache::Info(true));
  

  # Hash数据的处理建立在写锁之上,如需调试,则使用该方法查询锁信息
  var_dump(\Workbunny\WebmanSharedCache\Cache::LockInfo());
  

  # 包括键的一些基础信息
  var_dump(\Workbunny\WebmanSharedCache\Cache::KeyInfo('test-key'));
  

  # 接受多个参数
  \Workbunny\WebmanSharedCache\Cache::Del($a, $b, $c, $d);
  # 接受一个key的数组
  \Workbunny\WebmanSharedCache\Cache::Del(...$keysArray);
  

  \Workbunny\WebmanSharedCache\Cache::Clear();
  

  $rate = \Workbunny\WebmanSharedCache\RateLimiter::traffic('test');
  if ($rate['is_limit'] ?? false) {
      // 限流逻辑 如可以抛出异常、返回错误信息等
      return new \support\Response(429, [
          'X-Rate-Reset' => $rate['reset'],
          'X-Rate-Limit' => $rate['limit'],
          'X-Rate-Remaining' => $rate['reset']
      ])
  }
  

  # 向一个名为test的通道发送临时消息;
  # 通道没有监听器时,临时消息会被忽略,只有通道存在监听器时,该消息才会被存入通道
  Cache::ChPublish('test', '这是一个测试消息', false);
  

  # 向一个名为test的通道发送暂存消息;
  # 通道存在监听器时,该消息会被存入通道内的所有子通道
  Cache::ChPublish('test', '这是一个测试消息', true);
  

  # 指定发送消息至当前通道内workerId为1的子通道
  Cache::ChPublish('test', '这是一个测试消息', true, 1);
  

  # 向一个名为test的通道创建一个workerId为1的监听器;
  # 通道消息先进先出,当有消息时会触发回调
  Cache::ChCreateListener('test', '1', function(string $channelKey, string|int $workerId, mixed $message) {
      // TODO 你的业务逻辑
      dump($channelKey, $workerId, $message);
  });
  

  # 向一个名为test的通道创建一个workerId为1的监听器;
  # 通道移除时不会移除其他子通道消息
  Cache::ChRemoveListener('test', '1', true);
  

  # 向一个名为test的通道创建一个workerId为1的监听器;
  # 通道移除时不会移除所有子通道消息
  Cache::ChRemoveListener('test', '1', false);
  
shell
	# 1. pecl安装
	pecl instanll apcu
	# 2. docker中请使用安装器安装
	curl -sSL https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions -o - | sh -s apcu