-
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 新增内存缓存驱动 * 更新文档 * 将缓存基类中的 2 个内部方法标记为 3.0 废弃
- Loading branch information
Showing
6 changed files
with
174 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' => [ | ||
], | ||
], | ||
], | ||
]; | ||
``` | ||
|
||
> 本驱动不支持也没有必要支持序列化 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
[toc] | ||
|
||
缓存数据存储在请求上下文里。 | ||
|
||
## 配置 | ||
|
||
```php | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters