phossa/phossa-cache
Composer 安装命令:
composer require phossa/phossa-cache
包简介
The PSR-6 compliant PHP caching library.
README 文档
README
See new lib at phoole/cache Introduction
Phossa-cache is a PSR-6 compliant caching library. It supports various drivers and useful features like bypass, encrypt, stampede protection, garbage collect, taggable item etc.
More information about PSR-6 and PSR-6 Meta
Installation
Install via the composer utility.
composer require "phossa/phossa-cache=1.*"
or add the following lines to your composer.json
{
"require": {
"phossa/phossa-cache": "^1.0.8"
}
}
Features
-
Fully PSR-6 compliant. Maybe the most feature-rich PSR-6 caching package you will find at github at this time.
-
Support all serializable PHP datatypes.
-
Extensions:
-
Bypass: If sees a trigger in URL (e.g. '?nocache=true'), bypass the cache.
-
Stampede: Whenever cached object's lifetime is less than a configurable time, by certain percentage, the cache will return false on 'isHit()' which will trigger re-generation of the object.
-
Encrypt: A simple extension to encrypt the serialized content
-
GarbageCollect: A simple extension to auto-clean the cache pool.
-
Taggable: Item is taggable and can be cleared by tag.
-
DistributeMiss: Even out the spikes of item misses by alter expiration time a little bit.
-
-
Drivers
-
FilesystemDriver
The filesystem driver stores cached item in filesystem. It stores cached items in a md5-filename flat file. Configurable settings are
dir_root: the base directory for the filesystem cachehash_level: hashed subdirectory level. default to 2file_pref: cached item filename prefixfile_suff: cached item filename suffix
/* * construct the driver manually */ $driver = new Driver\FilesystemDriver([ 'hash_level' => 1, 'file_pref' => 'cache.', 'dir_root' => '/var/tmp/cache', ]);
-
NullDriver
The blackhole driver. used as fallback driver for all other drivers.
-
Fallback driver
User may configure a fallback driver if the desired driver is not ready. The
NullDriveris the final fallback for all other drivers.// default memcache driver $driver = new Driver\MemcacheDriver([ 'server' => [ '127.0.0.1', 11211 ] ]); // set a fallback filesystem driver $driver->setFallback(new Driver\FilesystemDriver([ 'dir_root' => '/var/tmp/cache' ])); $cache = new CachePool($driver);
-
CompositeDriver
The
CompositeDriverconsists of two drivers, the front-end driver and the backend driver. User filters cachable objects by defining atestercallable which will determine which objects stores to both ends or backend only./* * set the composite driver */ $driver = new Driver\CompositeDriver( // front-end driver new Driver\MemcacheDriver([ 'server' => [ '127.0.0.1', 11211 ] ]), // backend driver new Driver\FilesystemDriver([ 'dir_root' => '/var/tmp/cache' ]), // other settings [ // if size > 10k, stores at backend only 'tester' => function($item) { if (strlen($item->get()) > 10240) return false; return true; } ] );
-
-
Logging
The phossa-cache supports psr-3 compliant logger. Also provides a
log()method for logging./* * set the logger */ $cache->setLogger($psrLogger); $cache->log('info', 'this is an info');
Or configure with logger at cache init
/* * the third argument is used for configuring CachePool */ $cache = new CachePool($driver, [], 'logger' => $psrLogger ); $cache->log('info', 'this is an info');
-
Error
No exceptions thrown during caching process. So only errors will be used.
/* * create cache pool, exceptions may thrown here */ $cache = new CachePool(); $cache->setLogger($psrLogger); $item = $cache->getItem('widget_list'); $val = $item->get(); if ($cache->hasError()) { $cache->log('error', $cache->getError()); $widget_list = compute_expensive_widget_list(); $item->set($widget_list); $item->expiresAfter(3600); // expires after an hour $cache->save($item); if ($cache->hasError()) $cache->log('error', $cache->getError()); } else { $widget_list = $val; }
-
I18n
Messages are in
Message\Message.php. I18n is possible. See phossa-shared package for detail. -
Support PHP 5.4+, PHP 7.0, HHVM.
-
PHP7 ready for return type declarations and argument type declarations.
-
PSR-1, PSR-2, PSR-3, PSR-4, PSR-6 compliant.
Usage
-
The simple usage
/* * use the default FilesystemDriver which also set default cache * directory to sys_get_temp_dir() .'/cache' */ $cache = new CachePool(); $item = $cache->getItem('widget_list'); if (!$item->isHit()) { $value = compute_expensive_widget_list(); $item->set($value); $cache->save($item); } $widget_list = $item->get();
-
Configure the driver
$driver = new Driver\FilesystemDriver([ 'hash_level' => 1, // subdirectory hash levels 'file_pref' => 'cache.', // cache file prefix 'file_suff' => '.txt', // cache file suffix 'dir_root' => '/var/tmp/cache' // reset cache root ]); $cache = new CachePool($driver);
-
Use extensions
/* * SerializeExtension is the default ext, always used. * Second argument is an array of ExtensionInterface or config array */ $cache = new CachePool( $driver, [ new Extension\BypassExtension(), new Extension\StampedeExtension(['probability' => 80 ]) ] );
or
addExtension()$cache = new CachePool($driver); $cache->addExtension(new Extension\BypassExtension());
-
Hierarchal cache support
Directory-style hierarchal structure is supported in
FilesystemDriverand so other coming drivers.// hierarchy key $item = $cache->getItem('mydomain/host1/newfile_xxx'); // ending '/' means delete the hierarchy structure $cache->deleteItem('mydomain/host1/');
Dependencies
- PHP >= 5.4.0
- phossa/phossa-shared 1.0.6
- psr/cache 1.*
License
phossa/phossa-cache 适用场景与选型建议
phossa/phossa-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 45 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 01 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 「caching」 「psr-6」 「phossa」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phossa/phossa-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phossa/phossa-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phossa/phossa-cache 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Simple PSR-16 cache implementations for WordPress transient the OOP way
Query caching for Laravel 5
Stash Cache pool wrapper
Stash Cache middleware for Relay
PSR-6 Cache for Craft CMS
统计信息
- 总下载量: 45
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-21