change to psr-16

This commit is contained in:
crazywhalecc 2022-12-31 19:02:41 +08:00
parent ab83194bbe
commit a2404482a3
No known key found for this signature in database
GPG Key ID: 4B0FFA175E762022
5 changed files with 59 additions and 66 deletions

View File

@ -24,6 +24,7 @@
"onebot/libonebot": "^0.5",
"php-di/php-di": "^7",
"psr/container": "^2.0",
"psr/simple-cache": "^3.0",
"psy/psysh": "^0.11.8",
"symfony/console": "^6.0",
"symfony/polyfill-ctype": "^1.19",

View File

@ -1,11 +1,13 @@
<?php
declare(strict_types=1);
use OneBot\Driver\Driver;
use OneBot\Driver\Process\ProcessManager;
use Psr\Log\LoggerInterface;
use ZM\Framework;
/**
/*
* 这里是容器的配置文件,你可以在这里配置容器的绑定和其他一些参数。
* 选用的容器是 PHP-DI你可以在这里查看文档https://php-di.org/doc/
* 我们建议你在使用容器前先阅读以下章节:
@ -19,8 +21,8 @@ return [
// 这里定义的是全局容器的绑定,不建议在此处直接调用框架、应用内部的类或方法,因为这些类可能还没有被加载或初始化
// 你可以使用匿名函数来延迟加载
'definitions' => [
'worker_id' => fn() => ProcessManager::getProcessId(),
Driver::class => fn() => Framework::getInstance()->getDriver(),
LoggerInterface::class => fn() => logger(),
'worker_id' => fn () => ProcessManager::getProcessId(),
Driver::class => fn () => Framework::getInstance()->getDriver(),
LoggerInterface::class => fn () => logger(),
],
];

View File

@ -1,47 +1,10 @@
<?php
declare(strict_types=1);
namespace ZM\Store\KV;
use Psr\SimpleCache\CacheInterface;
interface KVInterface
{
/**
* 打开一个 KV
*
* @param string $name KV 的库名称
*/
public static function open(string $name = ''): KVInterface;
/**
* 返回一个 KV 键值对的数据
*
* @param string $key 键名
* @param null|mixed $default 如果不存在时返回的默认值
*/
public function get(string $key, mixed $default = null): mixed;
/**
* 设置一个 KV 键值对的数据
*
* @param string $key 键名
* @param mixed $value 键值
* @param int $ttl 超时秒数(如果等于 0 代表永不超时)
*/
public function set(string $key, mixed $value, int $ttl = 0): bool;
/**
* 强制删除一个 KV 键值对数据
*
* @param string $key 键名
* @return bool 当键存在并被删除时返回 true
*/
public function unset(string $key): bool;
/**
* 键值对数据是否存在
*
* @param string $key 键名
*/
public function isset(string $key): bool;
public static function open(string $name = ''): CacheInterface;
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace ZM\Store\KV;
use Psr\SimpleCache\CacheInterface;
use ZM\Exception\InvalidArgumentException;
use ZM\Process\ProcessStateManager;
use ZM\Store\FileSystem;
@ -11,7 +12,7 @@ use ZM\Store\FileSystem;
/**
* 轻量、基于本地 JSON 文件的 KV 键值对缓存
*/
class LightCache implements KVInterface
class LightCache implements CacheInterface, KVInterface
{
/** @var array 存放库对象的列表 */
private static array $objs = [];
@ -49,7 +50,7 @@ class LightCache implements KVInterface
/**
* @throws InvalidArgumentException
*/
public static function open(string $name = ''): KVInterface
public static function open(string $name = ''): CacheInterface
{
if (!isset(self::$objs[$name])) {
self::$objs[$name] = new LightCache($name);
@ -82,18 +83,6 @@ class LightCache implements KVInterface
], JSON_THROW_ON_ERROR));
}
/**
* 删除该 KV 库的所有数据,并且永远无法恢复
*/
public function removeSelf(): bool
{
if (file_exists($this->find_dir . '/' . $this->name . '.json')) {
unlink($this->find_dir . '/' . $this->name . '.json');
}
unset(self::$caches[$this->name], self::$ttys[$this->name], self::$objs[$this->name]);
return true;
}
public function get(string $key, mixed $default = null): mixed
{
// 首先判断在不在缓存变量里
@ -115,24 +104,62 @@ class LightCache implements KVInterface
/**
* @throws InvalidArgumentException
*/
public function set(string $key, mixed $value, int $ttl = 0): bool
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
{
$this->validateKey($key);
self::$caches[$this->name][$key] = $value;
if ($ttl > 0) {
if ($ttl !== null) {
if ($ttl instanceof \DateInterval) {
$ttl = $ttl->days * 86400 + $ttl->h * 3600 + $ttl->i * 60 + $ttl->s;
}
self::$ttys[$this->name][$key] = time() + $ttl;
}
return true;
}
public function unset(string $key): bool
public function delete(string $key): bool
{
unset(self::$caches[$this->name][$key], self::$ttys[$this->name][$key]);
return true;
}
public function isset(string $key): bool
public function clear(): bool
{
if (file_exists($this->find_dir . '/' . $this->name . '.json')) {
unlink($this->find_dir . '/' . $this->name . '.json');
}
unset(self::$caches[$this->name], self::$ttys[$this->name], self::$objs[$this->name]);
return true;
}
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
foreach ($keys as $v) {
yield $v => $this->get($v, $default);
}
}
public function setMultiple(iterable $values, \DateInterval|int|null $ttl = null): bool
{
foreach ($values as $k => $v) {
if (!$this->set($k, $v, $ttl)) {
return false;
}
}
return true;
}
public function deleteMultiple(iterable $keys): bool
{
foreach ($keys as $v) {
if (!$this->delete($v)) {
return false;
}
}
return true;
}
public function has(string $key): bool
{
if (!isset(self::$caches[$this->name][$key])) {
return false;

View File

@ -17,7 +17,7 @@ class LightCacheTest extends TestCase
$a = LightCache::open('asd');
$this->assertInstanceOf(LightCache::class, $a);
/* @phpstan-ignore-next-line */
$this->assertTrue($a->removeSelf());
$this->assertTrue($a->clear());
}
public function testSet()
@ -27,7 +27,7 @@ class LightCacheTest extends TestCase
public function testIsset()
{
$this->assertFalse(LightCache::open()->isset('test111'));
$this->assertFalse(LightCache::open()->has('test111'));
}
public function testGet()
@ -41,7 +41,7 @@ class LightCacheTest extends TestCase
$kv = LightCache::open('sss');
$kv->set('test', 'test');
$this->assertSame($kv->get('test'), 'test');
$kv->unset('test');
$kv->delete('test');
$this->assertNull($kv->get('test'));
}
}