apricity/micro-cache

为PHP应用程序提供的简单内存缓存机制。

v1.10.2 2024-06-08 14:18 UTC

This package is auto-updated.

Last update: 2024-09-09 13:58:44 UTC


README

Latest Stable Version PHP Version Require License

MicroCache是一个轻量级且简单的内存缓存解决方案,允许您在内存中存储键值对。它特别适用于缓存生成或从外部源检索成本高昂的数据。

类提供了一种简单的方法来在内存中缓存数据,允许有效地存储和检索缓存项。

安装

composer require apricity/micro-cache

目录

  1. 用法
  2. 测试
  3. 贡献
  4. 变更日志
  5. 许可证

MicroCache::set

在缓存中存储一个项。

此方法将在指定的键下将一个项存储在缓存中。如果键已存在,其值将被新值覆盖。如果提供了TTL(生存时间)参数且大于零,则该项将在指定秒数后过期并被从缓存中移除。如果TTL为零,则项将无限期缓存。

public static function set(mixed $key, mixed $value, int $ttl = 0): string|int

参数:

  • mixed $key:存储项的键。可以是任何类型,但不能是Closure。
  • mixed $value:要存储的值。
  • int $ttl:生存时间(秒)。如果为零,则项无限期缓存。

返回string|int - 存储项的键。

抛出InvalidArgumentException - 如果键是可调用的或如果键为空且不是零或false。

示例

$key = MicroCache::set('my_key', 'my_value', 3600);

MicroCache::get

从缓存中检索项值。

此方法检索在指定键下存储在缓存中的项的值。如果项存在于缓存中且未过期,则返回其值。如果项不存在或已过期,则返回null。

public static function get(mixed $key): mixed

参数:

  • mixed $key:存储项的键。可以是任何类型,但不能是Closure。

返回mixed|null - 缓存项值或如果键不存在或已过期则返回null。

抛出InvalidArgumentException - 如果键是可调用的或如果键为空且不是零或false。

示例

$value = MicroCache::get('my_key');
if (is_null($value)) {
    echo "Cache miss";
} else {
    echo "Cache value: " . $value;
}

MicroCache::delete

从缓存中删除项。

此方法删除在指定键下存储在缓存中的项。如果项存在于缓存中,则将其删除。如果项不存在,则不执行任何操作。

public static function delete(mixed $key): void

参数:

  • mixed $key:存储项的键。可以是任何类型,但不能是Closure。

返回void

抛出InvalidArgumentException - 如果键是可调用的或如果键为空且不是零或false。

示例

MicroCache::delete('my_key');

MicroCache::clear

清除缓存中的所有项。

此方法清除缓存中存储的所有项,有效地将缓存重置为空状态。

public static function clear(): void

返回void

示例

MicroCache::clear();

MicroCache::has

检查项是否存在于缓存中且未过期。

此方法检查指定键是否存在于缓存中且未过期。如果项存在且未过期,则返回true;否则,返回false。

public static function has(mixed $key): bool

参数:

  • mixed $key:存储项的键。可以是任何类型,但不能是Closure。

返回bool - 如果项存在且未过期则返回true,否则返回false。

抛出InvalidArgumentException - 如果键是可调用的或如果键为空且不是零或false。

示例

if (MicroCache::has('my_key')) {
    echo "Cache exists";
} else {
    echo "Cache does not exist or has expired";
}

继承和创建唯一缓存

共享缓存

use Apricity\MicroCache;

class SharedCache extends MicroCache {}

SharedCache::set('shared_cache_key', 'shared_value');

SharedCache::get('shared_cache_key'); // shared_value
MicroCache::get('shared_cache_key'); // shared_value

MicroCache::set('cache_key', 'cached_value');

SharedCache::get('cache_key'); // cached_value
MicroCache::get('cache_key'); // cached_value

唯一缓存

use Apricity\MicroCache;

class UniqueCache extends MicroCache {
    protected static array $microCache = []; // Non-shared unique cache.
}

UniqueCache::set('unique_cache_key', 'unique_value');

UniqueCache::get('unique_cache_key'); // unique_value
MicroCache::get('unique_cache_key'); // null

MicroCache::set('cache_key', 'cached_value');

UniqueCache::get('cache_key'); // null
MicroCache::get('cache_key'); // cached_value

运行测试

composer test

贡献

我们欢迎社区贡献!有关贡献指南,请参阅CONTRIBUTING.md文件。

许可证

此项目根据BSD 3-Clause许可证授权 - 请参阅LICENSE文件以获取详细信息。

已从GitLab迁移此存储库。