icanboogie / storage
存储和检索值,使用不同的/多个存储后端。
Requires
- php: >=7.1
Requires (Dev)
- ext-apcu: *
- ext-json: *
README
icanboogie/storage 定义了一个存储和检索值的 API,同时提供了不同的存储后端。
可以使用运行时内存、Redis、APC、文件系统等存储后端来存储值。存储集合使用多个不同的存储实例来检索和存储值,这些实例通常从成本较低(且更易变)到成本较高(且更持久)不等。
本软件包包括以下存储后端
- RunTimeStorage:使用 PHP 数组。
- RedisStorage:使用 Redis 实例。
- APCStorage:使用 APC 或 APCu。
- FileStorage:使用文件系统。
- StorageCollection:使用存储集合。
存储
存储用于存储值并在稍后检索它。一个独特的键用于识别值。不同的存储使用不同的机制来存储值,有时持续的时间也不同。根据持久性和存储的成本,选择最适合特定情况的存储总是一个好的主意。
注意:存储类实现了 Storage 接口,包括 StorageCollection 类。
<?php use ICanBoogie\Storage\RunTimeStorage; $storage = new RunTimeStorage; $storage->exists('icanboogie'); // false $storage->retrieve('icanboogie'); // null $storage->store('icanboogie', "Yes Sir, I Can Boogie"); $storage->retrieve('icanboogie'); // "Yes Sir, I Can Boogie" $storage->eliminate('icanboogie'); $storage->exists('icanboogie'); // false $storage->retrieve('icanboogie'); // null
生存时间(TTL)
一个项目的生存时间(TTL)是指该项目从存储到被认为是过时的秒数。
警告:如果您想使用 APCU 的该功能,需要将
apc.use_request_time
设置为false
。
<?php use ICanBoogie\Storage\RunTimeStorage; $storage = new RunTimeStorage; $storage->store('icanboogie', "Yes Sir, I Can Boogie", $ttl = 3); $storage->retrieve('icanboogie'); // "Yes Sir, I Can Boogie" sleep(4); $storage->exists('icanboogie'); // false $storage->retrieve('icanboogie'); // null
像数组一样使用存储
存储实现了 ArrayAccess
接口,可以作为数组访问。
<?php use ICanBoogie\Storage\RunTimeStorage; $storage = new RunTimeStorage; isset($storage['icanboogie']); // false $storage['icanboogie']; // null $storage['icanboogie'] = "Yes Sir, I Can Boogie"; $storage['icanboogie']; // "Yes Sir, I Can Boogie" unset($storage['icanboogie']); isset($storage['icanboogie']); // false $storage['icanboogie']; // null
迭代存储键
存储实现了 IteratorAggregate
接口,可以在 foreach
中迭代其键。
<?php $storage['one'] = 1; $storage['two'] = 2; $storage['three'] = 3; foreach ($storage as $key) { echo "defined: $key\n"; }
defined: one
defined: two
defined: three
适配器
文件存储使用 适配器 来写入和读取写入文件系统的数据。任何实现了 Adapter 接口的类都可以使用,本软件包提供了以下适配器
-
SerializeAdapter:使用
serialize()
和unserialize()
编码和解码数据。默认使用。 -
JSONAdapter:使用
json_encode()
和json_decode()
编码和解码数据。 -
PHPAdapter:使用
var_export()
和require
编码和读取数据。
存储集合
在实现缓存时,始终建议使用从成本较低(且更易变)到成本较高(且更耐用)的一系列 Storage 实例的集合。
以下示例演示了如何使用多个存储实例创建存储集合
<?php use ICanBoogie\Storage\StorageCollection; use ICanBoogie\Storage\RunTimeStorage; use ICanBoogie\Storage\APCStorage; use ICanBoogie\Storage\RedisStorage; use ICanBoogie\Storage\FileStorage; $storage = new StorageCollection([ new RunTimeStorage, new APCStorage('my-prefix'), new RedisStorage($redis_client, 'my-prefix'), new FileStorage('/path/to/directory') ]);
集合中的所有存储实例都通过 store()
、eliminate()
和 clear()
方法进行更新。当从 更昂贵的 存储中检索值时,存储实例也会更新,以便下次请求值时从成本较低的存储中检索。
缓存和缓存集合
Cache 接口和 CacheCollection 类实现了 Storage 接口和 StorageCollection 类的一个子集,它们仅提供只读功能。
支持函数
APCStorage::is_available()
可用于检查 APC 是否可用。
要求
该软件包需要 PHP 7.1 或更高版本。
安装
安装此软件包的推荐方法是使用 Composer。
$ composer require icanboogie/storage
文档
该软件包作为 ICanBoogie 框架 文档 的一部分进行文档记录。文档由 ApiGen 生成,使用 make doc
命令在 build/docs
目录中。可以使用 make clean
命令清理该目录。
本地开发和测试
对于本地开发,请使用提供的测试容器。您需要 Docker。运行 make test-container
以在容器内打开终端会话,然后运行 make test
以运行测试套件。或者,运行 make test-coverage
以运行测试套件并在 build/coverage
中生成覆盖率报告。
Travis CI 会持续测试此软件包。
许可
icanboogie/storage 根据 New BSD 许可证许可 - 有关详细信息,请参阅 LICENSE 文件。