mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 15:57:50 +08:00
85 lines
1.8 KiB
PHP
85 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Cache\Redis;
|
|
|
|
use App\Cache\Redis\Lua\RateLimit;
|
|
use App\Lib\Log\Logger;
|
|
use Hyperf\Contract\ConfigInterface;
|
|
use Hyperf\Redis\RedisFactory;
|
|
use Hyperf\Redis\RedisProxy;
|
|
|
|
/**
|
|
* @mixin RedisProxy
|
|
* @template T
|
|
*/
|
|
class RedisCache
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
private string $poolName = 'default';
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
private array $luaHandlers = [];
|
|
|
|
/**
|
|
* @param RedisFactory $redisFactory
|
|
* @param ConfigInterface $config
|
|
* @param Logger $logger
|
|
*/
|
|
public function __construct(
|
|
protected readonly RedisFactory $redisFactory,
|
|
protected readonly ConfigInterface $config,
|
|
protected readonly Logger $logger
|
|
) {}
|
|
|
|
/**
|
|
* @param string $poolName
|
|
* @return $this
|
|
*/
|
|
public function with(string $poolName = 'default'): self
|
|
{
|
|
$new = clone $this;
|
|
$new->poolName = $poolName;
|
|
return $new;
|
|
}
|
|
|
|
/**
|
|
* @return RedisProxy
|
|
*/
|
|
public function client(): RedisProxy
|
|
{
|
|
return $this->redisFactory->get($this->poolName);
|
|
}
|
|
|
|
/**
|
|
* @template TReturn
|
|
* @param class-string<TReturn> $scriptClass
|
|
* @return TReturn
|
|
*/
|
|
public function lua(string $scriptClass)
|
|
{
|
|
$poolName = $this->poolName ?? 'default';
|
|
$key = $poolName . ':' . $scriptClass;
|
|
|
|
if (!isset($this->luaHandlers[$key])) {
|
|
$this->luaHandlers[$key] = new $scriptClass(
|
|
$this->client(),
|
|
$this->config,
|
|
$this->logger
|
|
);
|
|
}
|
|
|
|
return $this->luaHandlers[$key];
|
|
}
|
|
|
|
/**
|
|
* 魔术方法代理原生 Redis 命令 (默认连接池)
|
|
*/
|
|
public function __call(string $method, array $arguments)
|
|
{
|
|
return $this->client()->{$method}(...$arguments);
|
|
}
|
|
} |