mirror of
https://gitee.com/zoujingli/WeChatDeveloper.git
synced 2026-09-04 23:12:09 +08:00
- 按 PHP-CS-Fixer 规则补齐核心契约、异常、缓存和 XML 工具文件头。 - 规范导入、PHPDoc 类型顺序与注释格式,保持项目头信息不丢失。 - 同步整理缓存键、缓存实现、XML 编解码相关测试和 bootstrap 格式。
68 lines
1.6 KiB
PHP
68 lines
1.6 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
/**
|
||
* This file is part of HyperfAdmin.
|
||
*
|
||
* @Link https://thinkadmin.top
|
||
* @Author Anyon<zoujingli@qq.com>
|
||
*/
|
||
|
||
namespace We\Support;
|
||
|
||
use Psr\SimpleCache\CacheInterface;
|
||
use We\Contract\StoreCacheInterface;
|
||
use We\Exception\WechatException;
|
||
|
||
/**
|
||
* PSR-16 缓存适配器;缓存能力委托给 PSR Simple Cache,实现侧需注入原子锁回调以支持刷新锁。
|
||
*/
|
||
final class PsrSimpleCacheStore implements StoreCacheInterface
|
||
{
|
||
/**
|
||
* 创建 PSR-16 缓存适配器,并可选注入锁回调。
|
||
*
|
||
* @param null|callable(string,int,callable):mixed $locker
|
||
*/
|
||
public function __construct(
|
||
private readonly CacheInterface $cache,
|
||
private readonly mixed $locker = null,
|
||
) {}
|
||
|
||
/**
|
||
* 从 PSR-16 缓存读取值。
|
||
*/
|
||
public function get(string $key, mixed $default = null): mixed
|
||
{
|
||
return $this->cache->get($key, $default);
|
||
}
|
||
|
||
/**
|
||
* 写入 PSR-16 缓存并设置 TTL。
|
||
*/
|
||
public function set(string $key, mixed $value, int $ttl): void
|
||
{
|
||
$this->cache->set($key, $value, max(1, $ttl));
|
||
}
|
||
|
||
/**
|
||
* 从 PSR-16 缓存删除值。
|
||
*/
|
||
public function del(string $key): void
|
||
{
|
||
$this->cache->delete($key);
|
||
}
|
||
|
||
/**
|
||
* 通过业务注入的锁回调执行互斥逻辑。
|
||
*/
|
||
public function lock(string $key, int $ttl, callable $callback): mixed
|
||
{
|
||
if (!is_callable($this->locker)) {
|
||
throw new WechatException('PsrSimpleCacheStore 未配置锁能力');
|
||
}
|
||
|
||
return ($this->locker)($key, max(1, $ttl), $callback);
|
||
}
|
||
}
|