feat : common redis cache and common logger

This commit is contained in:
2025-09-14 15:58:45 +08:00
parent 48ad2ebd1b
commit 0db9995c19
15 changed files with 640 additions and 14 deletions

View File

@@ -0,0 +1,82 @@
<?php
namespace App\Cache\Redis;
use App\Lib\Log\Logger;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Redis\RedisFactory;
use Hyperf\Redis\RedisProxy;
/**
* @mixin RedisProxy
*/
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);
}
/**
* @param string $scriptClass
* @return mixed
*/
public function lua(string $scriptClass): mixed
{
$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);
}
}