diff --git a/doc/components/cache/memory.md b/doc/components/cache/memory.md new file mode 100644 index 0000000000..f6d9be7920 --- /dev/null +++ b/doc/components/cache/memory.md @@ -0,0 +1,25 @@ +# 内存缓存 + +[toc] + +缓存数据储存在全局存储对象里。 + +## 配置 + +```php + [ + // 缓存名称 + 'alias1' => [ + // 缓存驱动类 + 'handlerClass' => \Imi\Cache\Handler\Memory::class, + // 驱动实例配置,暂无任何配置 + 'option' => [ + ], + ], + ], +]; +``` + +> 本驱动不支持也没有必要支持序列化 diff --git a/doc/components/cache/requestContext.md b/doc/components/cache/requestContext.md index d97be54ace..7166c44757 100644 --- a/doc/components/cache/requestContext.md +++ b/doc/components/cache/requestContext.md @@ -2,6 +2,8 @@ [toc] +缓存数据存储在请求上下文里。 + ## 配置 ```php diff --git a/src/Cache/Handler/Base.php b/src/Cache/Handler/Base.php index a0023da189..dcdc9728bd 100644 --- a/src/Cache/Handler/Base.php +++ b/src/Cache/Handler/Base.php @@ -72,18 +72,18 @@ protected function decode(string $data) /** * 检查key格式. + * + * @deprecated 3.0 */ protected function checkKey(string $key): void { - if (!\is_string($key)) - { - throw new InvalidArgumentException('Key must be a string'); - } } /** * 检查值是否是数组或Traversable. * + * @deprecated 3.0 + * * @param mixed $values */ protected function checkArrayOrTraversable($values): void diff --git a/src/Cache/Handler/Memory.php b/src/Cache/Handler/Memory.php new file mode 100644 index 0000000000..15d35e4f29 --- /dev/null +++ b/src/Cache/Handler/Memory.php @@ -0,0 +1,128 @@ +get($key, $default); + } + + /** + * {@inheritDoc} + */ + public function set($key, $value, $ttl = null) + { + // ttl 支持 \DateInterval 格式 + if ($ttl instanceof \DateInterval) + { + $ttl = DateTime::getSecondsByInterval($ttl); + } + self::$storage->set($key, $value, (int) $ttl); + + return true; + } + + /** + * {@inheritDoc} + */ + public function delete($key) + { + self::$storage->unset($key); + + return true; + } + + /** + * {@inheritDoc} + */ + public function clear() + { + self::$storage->clear(); + + return true; + } + + /** + * {@inheritDoc} + */ + public function getMultiple($keys, $default = null) + { + $this->checkArrayOrTraversable($keys); + $object = self::$storage; + $result = []; + foreach ($keys as $key) + { + $result[$key] = $object->get($key, $default); + } + + return $result; + } + + /** + * {@inheritDoc} + */ + public function setMultiple($values, $ttl = null) + { + $this->checkArrayOrTraversable($values); + // ttl 支持 \DateInterval 格式 + if ($ttl instanceof \DateInterval) + { + $ttl = DateTime::getSecondsByInterval($ttl); + } + $object = self::$storage; + foreach ($values as $key => $value) + { + $object->set($key, $value, $ttl ?? 0); + } + + return true; + } + + /** + * {@inheritDoc} + */ + public function deleteMultiple($keys) + { + $this->checkArrayOrTraversable($keys); + $object = self::$storage; + foreach ($keys as $key) + { + $object->unset($key); + } + + return true; + } + + /** + * {@inheritDoc} + */ + public function has($key) + { + return self::$storage->isset($key); + } +} diff --git a/tests/unit/Component/Tests/CacheMemoryTest.php b/tests/unit/Component/Tests/CacheMemoryTest.php new file mode 100644 index 0000000000..41b595a41a --- /dev/null +++ b/tests/unit/Component/Tests/CacheMemoryTest.php @@ -0,0 +1,10 @@ + [ ], ], + 'memory' => [ + 'handlerClass' => \Imi\Cache\Handler\Memory::class, + 'option' => [ + ], + ], ], // atmoic 配置 'atomics' => [