WeChatDeveloper/src/Support/PsrSimpleCacheStore.php
Anyon cdf868deec style(header): 统一核心契约缓存和测试文件格式
- 按 PHP-CS-Fixer 规则补齐核心契约、异常、缓存和 XML 工具文件头。

- 规范导入、PHPDoc 类型顺序与注释格式,保持项目头信息不丢失。

- 同步整理缓存键、缓存实现、XML 编解码相关测试和 bootstrap 格式。
2026-05-08 11:33:26 +08:00

68 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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);
}
}