gwa / gw-cache
PHP 简单缓存类
v2.0
2015-05-14 07:36 UTC
Requires
- gwa/gw-filesystem: ~1.4
Requires (Dev)
- fabpot/php-cs-fixer: ~1.7
- phpunit/phpunit: ~4.6.6
This package is not auto-updated.
Last update: 2024-09-14 16:13:34 UTC
README
一个简单但灵活的 PHP 缓存
用法
安装
通过 composer 安装 package。
composer require gwa/gw-cache
使用缓存
use Gwa\Cache\Cache; // Create a persistence layer for the cache // Cache directory should be writable. // (Cache will try to create it if it does not exist.) $cachedir = __DIR__ . '/cachestore'; $persistence = new CacheDirectoryPersistence($cachedir); // Set an optional group for the cache $group = ''; // Set cache validity in minutes $cacheminutes = 60 * 12; // create a cache instance using an identifier unique to the group $cache = new Cache('myidentifier', $group, $cacheminutes); $cache->setPersistence($persistence); $iscached = $cache->isCached(); // false // write a value to the cache $cache->set('foo'); // new object, same group and identifier $cache2 = new Cache('myidentifier', $group, $cacheminutes); $cache2->setPersistence($persistence); $iscached = $cache2->isCached(); // true $value = $cache2->get(); // 'foo' // clear the cache $cache2->clear(); $iscached = $cache2->isCached(); // false
使用工厂
除了始终传递持久层,还可以使用工厂。
工厂可以在您的应用程序中设置为一个服务(例如,使用 Pimple)。创建缓存实例不再依赖于持久层。
$factory = new CacheFactory(new CacheDirectoryPersistence($cachedir)); $cache = $factory->create('myidentifier', $group, $cacheminutes);
缓存序列化数据
要缓存复杂的 PHP 类型(例如对象),在创建缓存实例时设置缓存类型参数。
$cache = new Cache('myidentifier', $group, $cacheminutes, Cache::TYPE_OBJECT); // with a factory $cache = $factory->create('myidentifier', $group, $cacheminutes, Cache::TYPE_OBJECT);
自定义持久层
您可以通过创建一个实现 CachePersistenceInterface
接口的类来自定义持久层(MySQL、memcache、redis)。
如果您使用的是作为服务的工厂架构,您可以在不同的环境中使用不同的持久层,而无需更改使用该服务的代码。
贡献
所有代码贡献 - 包括具有提交访问权限的人的贡献 - 必须通过拉取请求,并由核心开发者批准后才能合并。这是为了确保对所有代码进行适当的审查。
分支项目,创建一个功能分支,并发送给我们一个拉取请求。
为了确保代码库的一致性,您应该确保代码遵循我们借鉴的 PSR-2 的 编码标准。
确保遵循编码标准的最佳方法是提交前运行 vendor/bin/php-cs-fixer fix
。