sobstel/metaphore
Composer 安装命令:
composer require sobstel/metaphore
包简介
PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates or stampending herd).
关键字:
README 文档
README
PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates, stampeding herd or Slashdot effect).
Problem: too many requests hit your website at the same time while it tries to regenerate same content slamming your database, eg. when cache expired.
Solution: first request generates new content while all the subsequent requests get (stale) content from cache until it's refreshed by the first request.
Read http://www.sobstel.org/blog/preventing-dogpile-effect/ for more details.
Installation
In composer.json file:
"require": {
"sobstel/metaphore": "2.0.*"
}
or just composer require sobstel/metaphore
Usage
use Metaphore\Cache; use Metaphore\Store\MemcachedStore; // initialize $memcached object (new Memcached()) $cache = new Cache(new MemcachedStore($memcached)); $cache->cache('key', function() { // generate content }, 30);
Public API (methods)
-
__construct(ValueStoreInterface $valueStore, LockManager $lockManager = null) -
cache($key, callable $callable, [$ttl, [$onNoStaleCacheCallable]])- returns result -
delete($key) -
getValue($key)- returns Value object -
setResult($key, $result, Ttl $ttl)- sets result (without anti-dogpile-effect mechanism) -
onNoStaleCache($callable) -
getValueStore() -
getLockManager()
Value store vs lock store
Cache values and locks can be handled by different stores.
$valueStore = new Metaphore\MemcachedStore($memcached); $lockStore = new Your\Custom\MySQLLockStore($connection); $lockManager = new Metaphore\LockManager($lockStore); $cache = new Metaphore\Cache($valueStore, $lockManager);
By default - if no 2nd argument passed to Cache constructor - value store is used as a lock store.
Sample use case might be to have custom MySQL GET_LOCK/RELEASE_LOCK for locks and still use in-built Memcached store for storing values.
Time-to-live
You can pass simple integer value...
$cache->cache('key', callback, 30); // cache for 30 secs
.. or use more advanced Metaphore\TTl object, which gives you control over grace period and lock ttl.
// $ttl, $grace_ttl, $lock_ttl $ttl = new Ttl(30, 60, 15); $cache->cache('key', callback, $ttl);
$ttl- regular cache time (in seconds)$grace_ttl- grace period, how long to allow to serve stale content while new one is being generated (in seconds), similar to HTTP's stale-while-revalidate, default is 60s$lock_ttl- lock time, how long to prevent other request(s) from generating same content, default is 5s
Ttl value is added to current timestamp (time() + $ttl).
No stale cache
In rare situations, when cache gets expired and there's no stale (generated earlier) content available, all requests will start generating new content.
You can add listener to catch this:
$cache->onNoStaleCache(function (NoStaleCacheEvent $event) { Logger::log(sprintf('no stale cache detected for key %s', $event->getKey())); });
You can also affect value that is returned:
$cache->onNoStaleCache(function (NoStaleCacheEvent $event) { $event->setResult('new custom result'); });
Tests
Run all tests: phpunit.
If no memcached or/and redis installed: phpunit --exclude-group=notisolated or phpunit --exclude-group=memcached,redis.
sobstel/metaphore 适用场景与选型建议
sobstel/metaphore 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 181.66k 次下载、GitHub Stars 达 100, 最近一次更新时间为 2014 年 07 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cache」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sobstel/metaphore 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sobstel/metaphore 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sobstel/metaphore 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Laravel 5 - Repositories to the database layer
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.
Laravel 5 - Repositories to the database layer
Rinvex Cacheable is a granular, intuitive, and fluent caching system for eloquent models. Simple, but yet powerful, plug-n-play with no hassle.
Small library for obtaining RSS feed with caching.
统计信息
- 总下载量: 181.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 100
- 点击次数: 29
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-07-07