corneltek/universal-cache
Composer 安装命令:
composer require corneltek/universal-cache
包简介
A Generic PHP Cache Interface with Cascade Caching
README 文档
README
A Generic PHP Cache Interface with Cascade Caching.
DESCRIPTION
This package was inspired by a Perl module - Cache::Cascade.
In a multiprocess, and especially a multiserver application caching is a very effective means of improving results.
The tradeoff of increasing the scale of the caching is in added complexity. For example, caching in a FastMmap based storage is much slower than using a memory based cache, because pages must be locked to ensure that no corruption will happen. Likewise Memcached is even more overhead than FastMmap because it is network bound, and uses blocking IO (on the client side).
This module attempts to make a transparent cascade of caches using several backends.
The idea is to search from the cheapest backend to the most expensive, and depending on the options also cache results in the cheaper backends.
The benefits of using a cascade are that if the chance of a hit is much higher in a slow cache, but checking a cheap cache is negligible in comparison, we may already have the result we want in the cheap cache. Configure your expiration policy so that there is approximately an order of magnitude better probability of cache hits (bigger cache) for each level of the cascade.
INSTALL
composer require corneltek/universal-cache
UniversalCache
UniversalCache class provides a consistent interface to operate on different cache backend, you may put the fastest cache backend on the first position, so that you can fetch the cache very quickly when there is a fresh cache in the fastest storage.
use UniversalCache\ApcuCache; use UniversalCache\FileSystemCache; use UniversalCache\UniversalCache; $cache = new UniversalCache([ new ArrayCache, // Fetch cache without serialization if there is a request-wide cache exists. new ApcuCache('app_', 60), // Faster then file system cache. new FileSystemCache(__DIR__ . '/cache'), ]); $cache->set('key', 'value'); $value = $cache->get('key');
ApcuCache
$cache = new UniversalCache\ApcuCache('app_', 3600); // 3600 = expiry time $cache->set($name,$val); $val = $cache->get($name); $cache->remove($name);
ArrayCache
ArrayCache implements a pure php based cache, the cache is not persistent between different request. However, it made the request-wide cache simple.
$cache = new UniversalCache\ArrayCache; $cache->set($name,$val); $val = $cache->get($name); $cache->remove($name);
MemcacheCache
$cache = new UniversalCache\MemcacheCache([ 'servers' => [['localhost', 123123], ['server2',123123] ] ]); $cache->set($name,$val); $val = $cache->get($name); $cache->remove($name);
RedisCache
$cache = new UniversalCache\RedisCache($redisConnection); $cache->set($name,$val); $val = $cache->get($name); $cache->remove($name);
FileSystemCache
$serializer = new SerializerKit\JsonSerializer(); $cache = new UniversalCache\FileSystemCache(__DIR__ . '/cache', [ 'expiry' => 30, 'serializer' => $serializer, ]);
Hacking
Install dependencies
composer install --prefer-source
Install redis extension
phpbrew ext install github:phpredis/phpredis php7
Install memcached extension
phpbrew ext install github:php-memcached-dev/php-memcached php7 -- --disable-memcached-sasl
Install APCu extension
phpbrew ext install github:krakjoe/apcu
Be sure to enable apcu and cli mode
apc.enabled=1
apc.enable_cli=1
Run tests
phpunit
LICENSE
This package is released under MIT license.
corneltek/universal-cache 适用场景与选型建议
corneltek/universal-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.1k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2013 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 corneltek/universal-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 corneltek/universal-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-03-20