Skip to content

Commit

Permalink
新增内存缓存驱动 (#490)
Browse files Browse the repository at this point in the history
* 新增内存缓存驱动

* 更新文档

* 将缓存基类中的 2 个内部方法标记为 3.0 废弃
  • Loading branch information
Yurunsoft authored Apr 6, 2023
1 parent e5b4d52 commit fafb131
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 4 deletions.
25 changes: 25 additions & 0 deletions doc/components/cache/memory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 内存缓存

[toc]

缓存数据储存在全局存储对象里。

## 配置

```php
<?php
return [
'caches' => [
// 缓存名称
'alias1' => [
// 缓存驱动类
'handlerClass' => \Imi\Cache\Handler\Memory::class,
// 驱动实例配置,暂无任何配置
'option' => [
],
],
],
];
```

> 本驱动不支持也没有必要支持序列化
2 changes: 2 additions & 0 deletions doc/components/cache/requestContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[toc]

缓存数据存储在请求上下文里。

## 配置

```php
Expand Down
8 changes: 4 additions & 4 deletions src/Cache/Handler/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
128 changes: 128 additions & 0 deletions src/Cache/Handler/Memory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

declare(strict_types=1);

namespace Imi\Cache\Handler;

use Imi\Bean\Annotation\Bean;
use Imi\Util\DateTime;
use Imi\Util\ExpiredStorage;

/**
* @Bean("MemoryCache")
*/
class Memory extends Base
{
protected static ?ExpiredStorage $storage = null;

public function __construct(array $option = [])
{
if (null === self::$storage)
{
self::$storage = new ExpiredStorage();
}
parent::__construct($option);
}

/**
* {@inheritDoc}
*/
public function get($key, $default = null)
{
return self::$storage->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);
}
}
10 changes: 10 additions & 0 deletions tests/unit/Component/Tests/CacheMemoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Imi\Test\Component\Tests;

class CacheMemoryTest extends BaseCacheTest
{
protected string $cacheName = 'memory';
}
5 changes: 5 additions & 0 deletions tests/unit/Component/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@
'option' => [
],
],
'memory' => [
'handlerClass' => \Imi\Cache\Handler\Memory::class,
'option' => [
],
],
],
// atmoic 配置
'atomics' => [
Expand Down

0 comments on commit fafb131

Please sign in to comment.