juhara / zzzcache
一个极简的缓存系统
Requires
- php: >=5.4
- psr/cache: ^1.0
- psr/simple-cache: ^1.0
Requires (Dev)
- php: >=5.6
- phpunit/phpunit: ^5
This package is not auto-updated.
Last update: 2024-09-20 08:32:34 UTC
README
一个极简且简单的PHP缓存库实现。更多信息请访问文档。
需求
安装
通过Composer运行
$ composer require juhara/zzzcache
如何使用
实现Cacheable接口
任何需要存储在缓存管理器中的类都需要实现 Juhara\ZzzCache\Contracts\Cacheable
接口,该接口包含 data()
和 ttl()
方法。
data()
方法应返回类数据。ttl()
应返回毫秒为单位的整数,该值决定了数据在缓存中保留多久,直到被认为是过期。
当从缓存中读取数据时,缓存管理器依赖于缓存存储接口实现,以在读写数据时提供适当的序列化/反序列化。
提供了一个 Juhara\ZzzCache\Contracts\Cacheable
实现类,Juhara\ZzzCache\Helpers\ClosureCacheable
,它将数据作为闭包实现。
$ttl = 60 * 60 * 1; //cache item for 1 hour
$cacheableItem = new Juhara\ZzzCache\Helpers\ClosureCacheable(function () {
return [
'dummyData' => 'dummy data'
];
}, $ttl);
当调用 $cacheableItem->data()
时,它会调用构造函数中传入的闭包函数,并返回闭包中定义的数据。
Juhara\ZzzCache\Helpers\ClosureCacheableFactory
类实现了 Juhara\ZzzCache\Contracts\CacheableFactoryInterface
,并作为 Juhara\ZzzCache\Helpers\ClosureCacheable
类的工厂。
$cacheableFactory = new \Juhara\ZzzCache\Helpers\ClosureCacheableFactory();
$cacheableItem = $cacheableFactory->build($data, $ttl);
当然,你可以自由地实现自己的。
初始化Cache实例
Juhara\ZzzCache\Cache
类是该库提供的默认 Juhara\ZzzCache\Contracts\CacheInterface
实现。
要使用它,您需要提供 Juhara\ZzzCache\Contracts\CacheStorageInterface
和 Juhara\ZzzCache\Contracts\ExpiryCalculatorInterface
实现。
请参阅存储实现以获取可用的 CacheStorageInterface
实现。
有一个 Juhara\ZzzCache\Helpers\ExpiryCalculator
类,它是该库的默认 ExpiryCalculatorInterface
实现。请参阅示例
<?php
use Juhara\ZzzCache\Cache;
use Juhara\ZzzCache\Storages\File;
use Juhara\ZzzCache\Helpers\ExpiryCalculator;
use Juhara\ZzzCache\Helpers\Md5Hash;
// create a file-based cache where all cache
// files is stored in directory name
// app/storages/cache with
// filename prefixed with string 'cache'
$cache = new Cache(
new File(
new Md5Hash(),
'app/storages/cache/',
'cache'
),
new ExpiryCalculator()
);
存储实现
缓存需要存储在某处。ZzzCache不实现存储接口。它将此委托给单独的库以提供存储实现,因此开发人员可以使用符合他们需求的存储实现。目前支持基于文件和基于Redis的存储。
基于文件的存储
安装,运行composer
$ composer require juhara/zzzfile
请参阅zzzfile。
基于Redis的存储
安装,运行composer
$ composer require juhara/zzzredis
请参阅zzzredis。
示例
使用zzzfile将文件作为缓存存储。
<?php
use Juhara\ZzzCache\Cache;
use Juhara\ZzzCache\Storages\File;
use Juhara\ZzzCache\Helpers\ExpiryCalculator;
use Juhara\ZzzCache\Helpers\Md5Hash;
// create a file-based cache where all cache
// files is stored in directory name
// app/storages/cache with
// filename prefixed with string 'cache'
$cache = new Cache(
new File(
new Md5Hash(),
'app/storages/cache/',
'cache'
),
new ExpiryCalculator()
);
使用zzzredis将Redis作为缓存存储。
<?php
use Juhara\ZzzCache\Cache;
use Juhara\ZzzCache\Storages\Redis;
use Juhara\ZzzCache\Helpers\ExpiryCalculator;
// create a redis-based cache
$cache = new Cache(
new Redis(new \Predis\Client()),
new ExpiryCalculator()
);
获取数据,如果缓存中有数据则从缓存中获取,否则从较慢的存储中获取。
<?php
...
try {
//try to get data from cache if available
$cachedData = $cache->get('itemNeedToBeCache');
} catch (\Juhara\ZzzCache\Exceptions\CacheNameNotFound $e) {
$acacheableItem = new \Juhara\ZzzCache\Helpers\ClosureCacheable(
function () {
//get data from slower storage
return ['dummyData'=>'dummyData'];
},
60 * 60 * 1 //cache item for 1 hour
);
$cachedData = $cache->add('itemNeedToBeCache', $acacheableItem)
->get('itemNeedToBeCache');
}
PSR-16 CacheInterface支持
ZzzCache通过Juhara\ZzzCache\Psr\AdapterCache
类支持PSR-16 CacheInterface,该类充当适配器。它实现了 Psr\SimpleCache\CacheInterface
。例如,
$psr16Cache = new \Juhara\ZzzCache\Psr\AdapterCache(
new \Juhara\ZzzCache\Cache(
new \Juhara\ZzzCache\Storages\Redis(new \Predis\Client()),
new \Juhara\ZzzCache\Helpers\ExpiryCalculator()
),
new \Juhara\ZzzCache\Helpers\ClosureCacheableFactory(),
60 * 60 * 1 //default time to live for 1 hour
);
贡献
如果你想要改进它,只需创建PR。
谢谢。