webignition / simplytestable-pagecache-bundle
Symfony 4 扩展包,用于创建/验证可缓存响应
Requires
- php: >=7.2.0
- doctrine/doctrine-bundle: ^1.6
- doctrine/orm: ^2.6
- symfony/config: ^4.1
- symfony/dependency-injection: ^4.1
- symfony/framework-bundle: ^4.1
- symfony/http-kernel: ^4.1
- symfony/yaml: ^4.1
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ~7.0
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2024-09-05 20:15:41 UTC
README
简介
轻松地将可缓存 Response
对象返回给控制器,用于以下内容:
- 可能发生变化的内容
- 并非每次请求都会发生变化
- 可以缓存未知时长
创建的可缓存响应会设置以下头部信息
Cache-Control: must-revalidate, public
Last-Modified: <now>
Etag: <唯一标识符的哈希值>
随后的请求将包含一个 If-None-Match: <唯一标识符的哈希值>
头部信息,我们可以使用它来决定是否告诉浏览器重用之前缓存的响应。
安装
使用 composer 安装
使用 composer 将其添加为项目依赖项
composer require simplytestable-pagecache-bundle:^0.1
更新数据库模式
缓存相关头部的详细信息持久化到对象存储中。此扩展包提供了一个实体,您需要更新数据库模式以允许实体持久化。
./bin/console doctrine:migrations:diff
./bin/console doctrine:migrations:migrate
使用
此扩展包提供了一个 SimplyTestable\PageCacheBundle\Services\CacheableResponseFactory
服务。在控制器构造函数或控制器操作中使用此服务作为类型提示。
最小使用方法
<?php namespace App\Controller; use SimplyTestable\PageCacheBundle\Services\CacheableResponseFactory; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Twig_Environment; class ExampleController { public function exampleAction( CacheableResponseFactory $cacheableResponseFactory, Twig_Environment $twig, Request $request ): Response { // ... perform whatever operation your action requires // Create a cacheable response. This response is capable of being cached, but we don't // yet know if it is the correct response for the given request $response = CacheableResponseFactory->createResponse($request, []); // Check if the response can be returned as-is if (Response::HTTP_NOT_MODIFIED === $response->getStatusCode()) { return $response; } // The cached response cannot be returned as-is, it doesn't match what // the request is asking for. Render and return a response based on the response // already created $response->setContent($twig->render( '::base.html.twig', [] )); return $response; } }
唯一标识响应
默认情况下,请求路由($request->attributes->get('_route')
)和请求接受头部($request->headers->get('accept')
)用于确定请求的唯一性。
CacheableResponseFactory::createResponse()
的第二个参数是一个数组,它将用于上述参数。例如,如果你的页面渲染博客文章,你希望将文章的唯一 ID 添加到参数中。
数组键可以是任何你喜欢的。相应的数组值可以是任何东西,但不能是复杂类型,必须是标量类型或可以转换为标量的对象(例如具有 __toString()
方法的对象)。
<?php namespace App\Controller; use App\Entity\Post; use Doctrine\ORM\EntityManagerInterface; use SimplyTestable\PageCacheBundle\Services\CacheableResponseFactory; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Twig_Environment; class ExampleController { public function exampleAction( $post_id, EntityManagerInterface $entityManager, CacheableResponseFactory $cacheableResponseFactory, Twig_Environment $twig, Request $request ): Response { $postRepository = $entityManager->getRepository(Post::class); $post = $postRepository->find($post_id); $response = CacheableResponseFactory->createResponse($request, [ 'post_id' => $post_id, ]); if (Response::HTTP_NOT_MODIFIED === $response->getStatusCode()) { return $response; } $response->setContent($twig->render( 'post.html.twig', [ 'post' => $post, ] )); return $response; } }
删除缓存相关实体
只要唯一标识符仍然匹配,即使内容已更改,您的控制器操作也会继续返回 304 Not Modified
响应。
您需要在内容更改后删除缓存相关实体。在将更改部署到生产后立即执行此操作是个好时机。
提供了一个命令来简化此过程
./bin/console simplytestable:pagecache:cachvalidator:clear
您可能有很多很多缓存相关实体需要删除。这些处理可能需要一些时间。该命令将以批处理的方式处理删除操作。默认的批次大小是 100。
您可以指定所需的任何批次大小
./bin/console simplytestable:pagecache:cachevalidator:clear 1
./bin/console simplytestable:pagecache:cachevalidator:clear 10
./bin/console simplytestable:pagecache:cachevalidator:clear 345
./bin/console simplytestable:pagecache:cachevalidator:clear 1000
贡献
需要更改?发现了错误?请随意分叉并创建针对此存储库的 PR。我很乐意考虑任何更改。
PR 将启动 Travis CI 构建,它将运行代码质量检查并运行测试套件。
如果您进行任何更改,请确保:
- 您的代码符合 PSR2 编码标准
- 现有测试通过
- 功能更改由更新或添加的测试覆盖