itkg/combinedhttpcache
Composer 安装命令:
composer require itkg/combinedhttpcache
包简介
Combined HttpCache with Redis centralized storage for metadata
README 文档
README
About
This library provides integration of distributed HttpCache based on HttpCache Symfony component on Redis storage.
- Metadata of request and response stay on Redis
- Cache entries come from Redis and are flushed locally so that fast integration and ESI support is kept with local filesystem
Installation
Add the library to your project thanks to composer:
composer require itkg/combinedhttpcache
Integrate in Symfony
Edit app/AppCache.php to change HttpCache base class :
<?php require_once __DIR__.'/AppKernel.php'; use Itkg\CombinedHttpCache\HttpCache\HttpCache; class AppCache extends HttpCache { }
Then change for testing in dev environment the web/app_dev.php.
Please note you have to push correct Redis configuration string as a second argument of AppCache constructor.
... $loader = require_once __DIR__.'/../app/bootstrap.php.cache'; Debug::enable(); require_once __DIR__.'/../app/AppKernel.php'; require_once __DIR__.'/../app/AppCache.php'; $kernel = new AppKernel('dev', true); $kernel->loadClassCache(); $kernel = new AppCache($kernel, array('redis_connection' => 'tcp://127.0.0.1:6379')); // When using the HttpCache, you need to call the method in your front controller instead of relying on the configuration parameter Request::enableHttpMethodParameterOverride(); $request = Request::createFromGlobals(); $response = $kernel->handle($request); $response->send(); $kernel->terminate($request, $response);
If everything works correctly, cache can then be activated for 'prod' environment. For that, just uncomment the code lines that Symfony keeps in comment to activate the cache.
Using cache
From that point, cache annotations or explicit cache settings for a Response will be managed to make storage of the current request (or ESI request) in conformity to URL unicity and Vary headers.
See :
- http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/cache.html
- http://symfony.com/doc/current/book/http_cache.html
Manage tagging for cached requests
Any request can be cached (if needed of course) and any cached request can be tagged. To rely on the same conventions has been the choice to manage tagging. For example, it will ease the process to move to Varnish even for the projects relying at the moment on this Redis version of HttpCache.
The library expects meta data tagging in responses by positionning X-ITKG-Cache-Tags. Tags must be separated thanks to ',' character. Each tag name is trimmed in case space-like characters are present (when splitting the header on comas).
Note :
- We cannot temporarily use the convention defined in FosHttpCache (https://foshttpcachebundle.readthedocs.org/en/latest/features/tagging.html) .
- The tagging can be followed easily thanks to the web debug toolbar for any "master request" and any ESI request.
Internal design
The cache storage is managed in two places:
- in Redis for metadata
- in Redis and filesystem for content digest
Note 1 : The double storage for content digest is quite handy because it keeps the reliability and speed for ESI management like the standard HttpCache that deals with a PHP include strategy to avoid unserialiazation and get benefits of standard opcode cache.
Note 2 : Opcode cache needs however correct memory settings because it must mostly avoid evicting cache entries to be efficient.
A default configuration value can be done however for starting.
memory = size(all php files) + size(all cache/prod/http_cache/)
In bash it becomes:
du -h --max-depth=0 /var/frontend/cache/prod/http_cache/ /var/www
In case (for ITKG it is the case), the best is to measure memory usage thanks to APC.php script to see how it behaves.
For simple invalidation out of Symfony
A specific integration is possible for backend responsible of blocks configuration hence the need of invalidation.
<?php use Itkg\CombinedHttpCache\Client\RedisClient; // declare correct autoloading require_once __DIR__.'/../vendor/autoload.php'; // create the client $client = new RedisClient('tcp://127.0.0.1:6379');
Then the client can be used this way:
// Here, we remove the keys being both in tag-a and tag-b which means an intersection is computed to make the removal $res = $client->removeKeysFromTags(array('tag-a', 'tag-b')); // Here, all the keys present in tag-a are removed and all keys in tag-b too (it makes a union). $res = $client->removeKeysFromTags(array(array('tag-a'), array('tag-b')));
Note : manual key set/get/del should not be performed here because key hashing is a very complex task that only HttpCache should manage.
License
See LICENSE file
itkg/combinedhttpcache 适用场景与选型建议
itkg/combinedhttpcache 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 10 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 itkg/combinedhttpcache 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 itkg/combinedhttpcache 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 23
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-10-05