itkg / combinedhttpcache
结合Redis集中存储元数据的HttpCache
Requires
- php: >=5.3.9
- ext-redis: ^2.2
- symfony/framework-bundle: ^2.7
- symfony/http-kernel: ^2.7
This package is not auto-updated.
Last update: 2024-09-18 11:13:26 UTC
README
关于
这个库提供了基于Redis存储的分布式HttpCache的集成,基于HttpCache Symfony组件。
- 请求和响应的元数据保留在Redis中
- 缓存条目来自Redis,并在本地刷新,以保持与本地文件系统的快速集成和ESI支持
安装
使用composer将库添加到您的项目中
composer require itkg/combinedhttpcache
在Symfony中集成
编辑app/AppCache.php以更改HttpCache基类
<?php require_once __DIR__.'/AppKernel.php'; use Itkg\CombinedHttpCache\HttpCache\HttpCache; class AppCache extends HttpCache { }
然后更改dev环境中的web/app_dev.php以进行测试。
请注意,您必须将正确的Redis配置字符串作为AppCache构造函数的第二个参数推送。
... $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);
如果一切正常,则可以激活'prod'环境中的缓存。为此,只需取消注释Symfony中保留的注释以激活缓存的代码行。
使用缓存
从那时起,将管理缓存注释或对Response的显式缓存设置,以确保当前请求(或ESI请求)的存储符合URL的唯一性和Vary头。
见
- https://symfony.com.cn/doc/current/bundles/SensioFrameworkExtraBundle/annotations/cache.html
- https://symfony.com.cn/doc/current/book/http_cache.html
管理缓存请求的标记
任何请求都可以被缓存(如果需要的话),任何缓存的请求都可以被标记。选择使用相同的约定来管理标记,例如,这将简化移动到Varnish的过程,即使对于目前依赖于这个Redis版本的HttpCache的项目。
库期望通过将X-ITKG-Cache-Tags置于响应中以位置方式管理元数据标记。标记必须由逗号字符分隔。如果存在类似空格的字符(当在逗号上拆分头时),则每个标记名称都会被修剪。
注意
- 我们不能暂时使用FosHttpCache中定义的约定(https://foshttpcachebundle.readthedocs.org/en/latest/features/tagging.html)。
- 可以通过任何“主请求”和任何ESI请求的Web调试工具栏轻松跟踪标记。
内部设计
缓存存储在两个地方管理
- 在Redis中管理元数据
- 在Redis和文件系统中管理内容摘要
注意1:对于内容摘要的双存储相当方便,因为它保持了标准HttpCache在ESI管理方面的可靠性和速度,该HttpCache使用PHP包含策略来避免反序列化并利用标准opcode缓存的优势。
注意2:然而,由于必须主要避免驱逐缓存条目以高效,因此需要正确的内存设置。
然而,可以进行默认配置值以启动。
memory = size(all php files) + size(all cache/prod/http_cache/)
在bash中变为
du -h --max-depth=0 /var/frontend/cache/prod/http_cache/ /var/www
在(对于ITKG来说是这种情况),最好的办法是通过APC.php脚本来测量内存使用情况,看看它的表现如何。
对于Symfony之外的简单无效化
对于负责块配置的后端,可以实现特定的集成,因此需要无效化。
<?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');
然后客户端可以这样使用
// 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')));
注意:在此处不应执行手动键设置/获取/删除,因为键哈希是一个非常复杂的任务,只有HttpCache应该管理。
许可证
见LICENSE文件