greg-md / php-cache
一个强大的PHP缓存。
Requires
- php: ^7.3
- ext-pdo: *
- ext-redis: *
Requires (Dev)
- phpunit/phpunit: ^8
This package is auto-updated.
Last update: 2024-09-24 19:50:52 UTC
README
一个强大的PHP缓存。
目录
要求
- PHP版本
^7.1
支持的驱动
- 数组 - 使用PHP数组作为存储;
- 临时文件 - 使用临时文件作为存储。当脚本结束时,它们会自动删除;
- 文件 - 使用基于文件的存储;
- Memcached - 使用Memcached驱动作为存储;
- Redis - 使用Redis驱动作为存储;
- SQLite - 使用SQLite数据库作为存储。
工作原理
有两种方式来处理缓存策略:直接或通过缓存管理器。
缓存管理器可以拥有许多缓存策略和一个默认策略。缓存管理器实现相同的缓存策略,如果定义了,可以充当默认策略。
在下一个示例中,我们将使用缓存管理器。
首先,您必须初始化一个缓存管理器并注册一些策略
$manager = new \Greg\Cache\CacheManager(); // Register a file cache $manager->registerStrategy('store1', new \Greg\Cache\FileCache(__DIR__ . '/storage')); // Register a sqlite cache $manager->register('store2', function() { $pdo = new \PDO('sqlite:' . __DIR__ . '/storage/store2.sqlite'); return new \Greg\Cache\SqliteCache($pdo); }); // Register a redis cache $manager->register('store3', function() { $redis = new \Redis(); $redis->connect('127.0.0.1'); return new \Greg\Cache\RedisCache($redis); });
可选,您可以定义一个默认存储,供缓存管理器使用。
$manager->setDefaultStoreName('store2');
然后,您可以 设置 或 获取 一些数据
// Add some data in "store1" $manager->store('store1')->set('foo', 'FOO'); // Add some data in default store, which is "store2" $manager->set('bar', 'BAR'); // Get "bar" value from default store. $value = $manager->get('bar'); // result: BAR
缓存策略
如果您想,您可以创建自己的策略。它们应该实现 \Greg\Cache\CacheStrategy
接口。
以下是一个 支持的方法 列表。
- has - 确定一个项是否存在于缓存中;
- hasMultiple - 确定多个项是否存在于缓存中;
- get - 从缓存中获取一个值;
- getMultiple - 通过其唯一键获取多个缓存项;
- set - 将数据持久化到缓存中,通过一个键唯一引用,可选的TTL时间;
- setMultiple - 将一组
key => value
对持久化到缓存中,可选的TTL; - forever - 将数据永久持久化到缓存中,通过一个键唯一引用;
- foreverMultiple - 将一组
key => value
对永久持久化到缓存中; - delete - 通过其唯一键从缓存中删除一个项;
- deleteMultiple - 通过其唯一键从缓存中删除多个项;
- clear - 清除存储;
- remember - 有时您可能希望从缓存中检索一个项,但如果请求的项不存在,也存储一个默认值;
- increment - 增加一个值;
- decrement - 减少一个值;
- incrementFloat - 增加一个浮点值;
- decrementFloat - 减少一个浮点值;
- touch - 为项目设置新的过期时间;
- pull - 从缓存中检索并删除一个项目;
- add - 如果缓存中不存在,则持久化数据;
has
确定项目是否存在于缓存中。
has(string $key): bool
$key
- 缓存项目键。
示例
$strategy->has('foo');
hasMultiple
确定项目是否存在于缓存中。
hasMultiple(array $keys): bool
$keys
- 缓存项目键。
示例
$strategy->hasMultiple(['foo', 'bar']);
get
从缓存中获取值。
get(string $key, mixed $default = null): mixed
$key
- 该项目在缓存中的唯一键;
$default
- 如果键不存在,则返回的默认值。
返回缓存的项目的值,或者在没有找到缓存的情况下返回 $default
。
示例
$strategy->get('foo');
getMultiple
通过其唯一键获取多个缓存项目。
getMultiple(array $keys, $default = null): mixed
$keys
- 可以在单个操作中获取的键列表;
$default
- 对于不存在的键返回的默认值。
返回一个 key => value
对的列表。不存在的或过期的缓存键将具有 $default
作为值。
示例
$strategy->get('foo');
set
通过一个键以及可选的过期TTL时间来持久化缓存中的数据。
set(string $key, $value, ?int $ttl = null): $this
$key
- 要存储的项目的键;
$value
- 要存储的项目的值,必须是可序列化的;
$ttl
- 可选。此项目的TTL值。如果没有发送值,并且驱动程序支持TTL,则库可能会为它设置一个默认值,或者让驱动程序处理。
示例
$strategy->set('foo', 'FOO');
setMultiple
通过可选的TTL持久化缓存中的多个 key => value
对。
setMultiple(array $values, ?int $ttl = null): $this
$values
- 多个-set操作的 key => value
对的列表;
$ttl
- 可选。此项目的TTL值。如果没有发送值,并且驱动程序支持TTL,则库可能会为它设置一个默认值,或者让驱动程序处理。
示例
$strategy->setMultiple(['foo' => 'FOO', 'bar' => 'BAR']);
forever
通过键永久持久化缓存中的数据。
forever(string $key, $value): $this
$key
- 要存储的项目的键;
$value
- 要存储的项目的值,必须是可序列化的。
示例
$strategy->forever('foo', 'FOO'); // or $strategy->set('foo', 'FOO', 0);
foreverMultiple
永久持久化缓存中的多个 key => value
对。
foreverMultiple(array $values): $this
$values
- 多个-set操作的 key => value
对的列表。
示例
$strategy->foreverMultiple(['foo' => 'FOO', 'bar' => 'BAR']); // or $strategy->setMultiple(['foo' => 'FOO', 'bar' => 'BAR'], 0);
delete
通过其唯一键从缓存中删除一个项目。
delete(string $key): $this
$key
- 要删除的项目唯一的缓存键。
示例
$strategy->delete('foo');
deleteMultiple
通过其唯一键从缓存中删除多个项目。
deleteMultiple(array $keys): $this
$keys
- 要删除的项目的唯一缓存键。
示例
$strategy->deleteMultiple(['foo', 'bar']);
clear
清除缓存的所有键。
clear(): $this
示例
$strategy->clear();
remember
有时您可能希望从缓存中检索一个项目,但如果请求的项目不存在,则也存储一个默认值。
remember(string $key, callable($this): mixed $callable, ?int $ttl = null): mixed
$key
- 该项目在缓存中的唯一键;
$callable
- 当键不在缓存中时存储的项的值可调用的值。值必须是可序列化的;
$ttl
- 可选。此项目的TTL值。如果没有发送值,并且驱动程序支持TTL,则库可能会为它设置一个默认值,或者让驱动程序处理。
示例
$strategy->remember('foo', function() { return 'FOO'; });
increment
增加一个值。
increment(string $key, int $amount = 1, ?int $ttl = null): $this
$key
- 该项目在缓存中的唯一键;
$abount
- 要增加的数量;
$ttl
- 可选。此项目的TTL值。如果没有发送值,并且驱动程序支持TTL,则库可能会为它设置一个默认值,或者让驱动程序处理。
示例
$strategy->increment('foo'); $strategy->increment('foo', 10);
decrement
减少一个值。
decrement(string $key, int $amount = 1, ?int $ttl = null): $this
$key
- 该项目在缓存中的唯一键;
$abount
- 要减少的数量;
$ttl
- 可选。此项目的TTL值。如果没有发送值,并且驱动程序支持TTL,则库可能会为它设置一个默认值,或者让驱动程序处理。
示例
$strategy->decrement('foo'); $strategy->decrement('foo', 10);
incrementFloat
增加一个浮点值。
incrementFloat(string $key, float $amount = 1.0, ?int $ttl = null): $this
$key
- 该项目在缓存中的唯一键;
$abount
- 要增加的数量;
$ttl
- 可选。此项目的TTL值。如果没有发送值,并且驱动程序支持TTL,则库可能会为它设置一个默认值,或者让驱动程序处理。
示例
$strategy->incrementFloat('foo'); $strategy->incrementFloat('foo', 1.5);
decrementFloat
减少一个浮点值。
decrementFloat(string $key, float $amount = 1.0, ?int $ttl = null): $this
$key
- 该项目在缓存中的唯一键;
$abount
- 要减少的数量;
$ttl
- 可选。此项目的TTL值。如果没有发送值,并且驱动程序支持TTL,则库可能会为它设置一个默认值,或者让驱动程序处理。
示例
$strategy->decrementFloat('foo'); $strategy->decrementFloat('foo', 1.5);
touch
设置一个项目的新过期时间。
touch(string $key, ?int $ttl = null): $this
$key
- 该项目在缓存中的唯一键;
$ttl
- 可选。此项目的TTL值。如果没有发送值,并且驱动程序支持TTL,则库可能会为它设置一个默认值,或者让驱动程序处理。
示例
$strategy->touch('foo', 100);
pull
从缓存中检索并删除一个项目。
pull(string $key, $default = null): mixed
$key
- 该项目在缓存中的唯一键;
$default
- 对于不存在的键返回的默认值。
返回缓存的项目的值,或者在没有找到缓存的情况下返回 $default
。
示例
$strategy->pull('foo'); // return foo value $strategy->pull('foo'); // return null
add
如果不存在,则持久化数据到缓存中。
add(string $key, $value, ?int $ttl = null): $this
$key
- 要存储的项目的键;
$value
- 要存储的项目的值,必须是可序列化的;
$ttl
- 可选。此项目的TTL值。如果没有发送值,并且驱动程序支持TTL,则库可能会为它设置一个默认值,或者让驱动程序处理。
如果项目实际添加到缓存中,则返回 true
。否则,返回 false
。
示例
$strategy->add('foo', 'FOO'); // return true $strategy->add('foo', 'FOO2'); // return false
许可证
MIT © Grigorii Duca