germania-kg/cachecallable
Composer 安装命令:
composer require germania-kg/cachecallable
包简介
Callable convenience wrapper around PSR-6 Cache Item Pools: Seamlessly create, return, and store your data.
README 文档
README
Callable convenience wrapper around PSR-6 Cache Item Pools: Seamlessly creates, returns, and stores your data.
Caching business is pretty much always similar and can be outlined like this:
- Is caching enabled at all? If not, delete any according older entry first.
Create and return fresh content anyway, ending up here. - Does a given item exist? If so, return item content;
Otherwise, create, store and return content.
The CacheCallable class reduces these steps to a handy and customizable Callable.
Installation with Composer
$ composer require germania-kg/cachecallable
Example
Although this example uses phpfastcache, you should be able to pass in any Cache Item Pool. Use your favourite PSR-3 Logger; this example will use the well-known Monolog.
<?php use phpFastCache\CacheManager; use Monolog\Logger; use Germania\Cache\CacheCallable; // Setup dependencies $cacheItemPool = CacheManager::getInstance('files', [ options ]); $lifetime = 3600; $monolog = new Logger( "My App" ); $content_creator = function( $keyword ) { return "Cache keyword: " . $keyword; }; // // Setup Cache wrapper // $wrapped_cache = new CacheCallable( $cacheItemPool, $lifetime, $creator, $monolog // optional ); // Identifying key. Example for a web page: $keyword = sha1($_SERVER['REQUEST_URI']); echo $wrapped_cache( $keyword );
A word on cache keys
According to the PSR-6 specs, cache keys should be limited to A-Z, a-z, 0-9, _, and . to ensure maximum compatibilty. So if you pass in a PSR-6 Adapter from Symfony Cache component, class CacheCallable internally converts the given keys to a MD5 representation.
In case you'd like to provide a custom cache key creation, you may use the setCacheKeyCreator method whoch accepts any callable:
$wrapped_cache->setCacheKeyCreator( function($raw) { return sha1($raw); } );
PHP-FIG: PSR-6: Caching Interface
Implementing libraries MUST support keys consisting of the characters A-Z, a-z, 0-9, _, and . in any order in UTF-8 encoding and a length of up to 64 characters. Implementing libraries MAY support additional characters and encodings or longer lengths, but must support at least that minimum.
Symfony docs: “Cache Item Keys and Values”
The key of a cache item […] should only contain letters (A-Z, a-z), numbers (0-9) and the _ and . symbols.
The Cache lifetime
Think of a webpage that turns out to be not cached during script runtime — after we set up the Cache wrapper. For this reason, the Cache wrapper constructor also accepts a LifeTimeInterface implementation with a getValue method:
<?php namespace Germania\Cache; interface LifeTimeInterface { // Return seconds to expiration public function getValue(); }
The LifeTime class is a simple implementation of this interface. It additionally enables you to change the lifetime value during runtime. Since we passed it to our Cache constructor by reference, the Cache wrapper can decide after content creation wether to cache or not.
Create a Lifetime object
<?php use Germania\Cache\CacheCallable; use Germania\Cache\LifeTime; // Setup LifeTime object $lifetime_object = new LifeTime( 3600 ); // Use Factory method: $lifetime_object = LifeTime::create( 3600 ); // Create from Lifetime instance $another_lifetime = new LifeTime( $lifetime_object ); $another_lifetime = LifeTime::create( $lifetime_object );
Usage with CacheCallable
<?php use Germania\Cache\CacheCallable; use Germania\Cache\LifeTime; // Taken from example above $wrapped_cache = new CacheCallable( $cacheItemPool, $lifetime_object, $creator );
Your Logger will now output something like this:
MyLogger DEBUG Lifetime after content creation: 0
MyLogger NOTICE DO NOT store in cache
How to change lifetime during script runtime
After instantation, you may use the setValue method:
<?php namespace Germania\Cache; interface LifeTimeInterface { // Return seconds to expiration public function getValue(); }
The LifeTime class is a simple implementation of this interface. It additionally enables you to change the lifetime value during runtime. Since we passed it to our Cache constructor by reference, the Cache wrapper can decide after content creation wether to cache or not.
Create a Lifetime object:
<?php use Germania\Cache\CacheCallable; use Germania\Cache\LifeTime; // Setup LifeTime object $lifetime_object = new LifeTime( 3600 ); // Use Factory method: $lifetime_object = LifeTime::create( 3600 ); // Create from Lifetime instance $another_lifetime = new LifeTime( $lifetime_object ); $another_lifetime = LifeTime::create( $lifetime_object );
Set time value after instantation
// Change LifeTime value during runtime, // e.g. in router or controller $lifetime_object->setValue( 0 );
Usage with CacheCallable
<?php use Germania\Cache\CacheCallable; use Germania\Cache\LifeTime; // Taken from example above $wrapped_cache = new CacheCallable( $cacheItemPool, $lifetime_object, $creator );
Your Logger will now output something like this:
MyLogger DEBUG Lifetime after content creation: 0
MyLogger NOTICE DO NOT store in cache
How to override content creation
If you prefer singleton services, you may invoke the CacheCallable with a custom content creator parameter to override the default one:
// Default content creator $default_creator = function($file) { return json_decode( file_get_contents($file) ); }; // Setup Service $wrapped_cache = new CacheCallable( $cacheItemPool, $lifetime_object, $default_creator ); // Override content creation $config = $wrapped_cache("config.json", function( $file ) { return array('foo' => 'bar'); };
Issues
The PSR-6 Caching Interface mock in CacheCallableTest could need an overhaul. Discuss on #issue 3
Development
$ git clone https://github.com/GermaniaKG/CacheCallable.git
$ cd CacheCallable
$ composer install
Unit tests
Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:
$ composer test # or $ vendor/bin/phpunit
Useful Links
germania-kg/cachecallable 适用场景与选型建议
germania-kg/cachecallable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.82k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 10 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「cache」 「caching」 「middleware」 「callable」 「psr-6」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 germania-kg/cachecallable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 germania-kg/cachecallable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 germania-kg/cachecallable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Query caching for Laravel 5
Laravel 5 - Repositories to the database layer
Simple PHP caching system that uses a tmp folder in a Linux environment.
Provides support for PSR-6 compatible cache for Yii1 application
Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
统计信息
- 总下载量: 1.82k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-04