From a2404482a3bbe957fbc1bc2d5b1e322864f324ce Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 31 Dec 2022 19:02:41 +0800 Subject: [PATCH] change to psr-16 --- composer.json | 1 + config/container.php | 10 +++-- src/ZM/Store/KV/KVInterface.php | 43 ++---------------- src/ZM/Store/KV/LightCache.php | 65 ++++++++++++++++++++-------- tests/ZM/Store/KV/LightCacheTest.php | 6 +-- 5 files changed, 59 insertions(+), 66 deletions(-) diff --git a/composer.json b/composer.json index 1279518f..ea84a726 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/config/container.php b/config/container.php index 6bc44c2f..5a79ffda 100644 --- a/config/container.php +++ b/config/container.php @@ -1,11 +1,13 @@ [ - '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(), ], ]; diff --git a/src/ZM/Store/KV/KVInterface.php b/src/ZM/Store/KV/KVInterface.php index 038e1d15..b9a2fdad 100644 --- a/src/ZM/Store/KV/KVInterface.php +++ b/src/ZM/Store/KV/KVInterface.php @@ -1,47 +1,10 @@ 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; diff --git a/tests/ZM/Store/KV/LightCacheTest.php b/tests/ZM/Store/KV/LightCacheTest.php index 98ee38a3..1578f751 100644 --- a/tests/ZM/Store/KV/LightCacheTest.php +++ b/tests/ZM/Store/KV/LightCacheTest.php @@ -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')); } }