mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 18:17:49 +08:00
feat : common redis cache and common logger
This commit is contained in:
82
app/Cache/Redis/RedisCache.php
Normal file
82
app/Cache/Redis/RedisCache.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user