lingualeo/php-cache
Composer 安装命令:
composer require lingualeo/php-cache
包简介
LinguaLeo Caching Library
README 文档
README
PHP-Cache is a library designed to simplify your work with the different cache storage engines. Currently library is under heavy development so API can change in the future.
Example using Redis:
$redis = new \Redis(); $redis->connect('127.0.0.1'); $cache = new RedisCache($redis); $cache->set('test', 'data'); //write 'data' by key 'test' $cache->get('test'); //returns 'data'
Also atomic CAS-like operations supported (create and update). For example:
$modifier = function (&$data) { $data = 'value'; }; $result = $this->cache->create('test', $modifier);
Data represents the reference to information that storage already contains. You can modify or completly replace it inside the callable.
If other client will change Data before you call create you will get an AtomicViolationException. In your client code you can catch this exception and retry operation.
The main difference betweeen create and update methods is that update will do nothing in case storage doesn't contains the specified key.
Decorators
Currently library provides only one additional HotCacheDecorator which is very useful in a highloaded environment: it simply stores (and modifies) all cached data in a in-memory array. So if you call get once then all subsequent calls will be served only by hot cache without any requests to storage server.
To enable hot cache just wrap your creation of any CacheProvider object with the decorator:
$redis = new \Redis(); $redis->connect('127.0.0.1'); $cache = new HotCacheDecorator(new RedisCache($redis));
Cache key generation
Library provides simple mechanism to generate cache keys with generateCacheKey method of CacheProvider:
$key = CacheProvider::generateCacheKey('arg1', 'arg2'); //cache:arg1:arg2
You can pass any number of arguments (> 0) to this method in order to get a fresh shiny cache key. Also as the first parameter you can pass some class name and if this class will contain constant field VERSION then it's value will be mixed up with other arguments of generateCacheKey:
class Test { const VERSION = 2; } ... $key = CacheProvider::generateCacheKey('Test', 'arg1', 'arg2'); //cache:Test:arg1:arg2:2
This behavior is very useful when you work with ORM models and want to cache different versions of objects without need to drop all cache data after making some changes in a small single model.
Testing
Setup Vagrant and Virtualbox.
vagrant up phpunit
lingualeo/php-cache 适用场景与选型建议
lingualeo/php-cache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.25k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2014 年 04 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 lingualeo/php-cache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lingualeo/php-cache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 12.25k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-04-17