webignition/simplytestable-pagecache-bundle
Composer 安装命令:
composer require webignition/simplytestable-pagecache-bundle
包简介
Symfony 4 bundle for creating/validating cacheable responses
README 文档
README
Introduction
Easily make cacheable Response objects to be returned from your controllers for content that:
- can change
- doesn't always change on every single request
- can be cached for an unknown amount of time
A created cacheable response has the following headers set:
Cache-Control: must-revalidate, publicLast-Modified: <now>Etag: <hash of unique identifier>
Subsequent requests made by a browser will include a If-None-Match: <hash of unique identifier> header which we can then use to determine if we should tell the browser to re-use the previously-cached response.
Installation
Install via composer:
Use composer to add as a project dependency:
composer require simplytestable-pagecache-bundle:^0.1
Update your database schema:
Details of cache-related headers previously sent are persisted to an object store. This bundle provides an entity, you need to update your database schema to allowt the entities to be persisted.
./bin/console doctrine:migrations:diff
./bin/console doctrine:migrations:migrate
Usage
This bundle provides a SimplyTestable\PageCacheBundle\Services\CacheableResponseFactory service. Use this as a type-hint in a controller constructor or in a controller action.
Minimal usage
<?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; } }
Uniquely identifiying a response
The request route ($request->attributes->get('_route') and the request accept header ($request->headers->get('accept') are used by default to determine the uniqueness of a request.
CacheableResponseFactory::createResponse() takes as its second argument an array of parameters that will be used in addition to the above. For example, if your page renders a blog post, you would want to add to the parameters the unique id of the post.
The array keys can be anything you like. The corresponding array values can be anything but they must be scalar types or objects that cast to scalars (such as an object with a __toString() method.
<?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; } }
Removing cache-related entities
Your controller actions will continue to return 304 Not Modified responses regardless of whether the content has changed so long as the unique identifiers still match.
You will need to remove cache-related entities upon content changes. A good time to do so is right after deploying changes to production.
A command is provided to make this super easy:
./bin/console simplytestable:pagecache:cachvalidator:clear
You may have many, many, many cache-related entities to remove. These may take some time to process. The command will process removals in batches. The default batch size is 100.
You can specify whatever batch size you need:
./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
Contrinbuting
Need a change? Spotted a bug? Feel free to fork and create a PR against this repo. I'm happy to consider any changes.
A PR will kick off a Travis CI build that runs code quality checks and runs the test suite.
If you make any changes, please make sure that:
- your code meets PSR2 coding standards
- existing tests pass
- changes to functionality are covered by updated or added tests
webignition/simplytestable-pagecache-bundle 适用场景与选型建议
webignition/simplytestable-pagecache-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 147 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 08 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「http」 「cache」 「response」 「symfony4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webignition/simplytestable-pagecache-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webignition/simplytestable-pagecache-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webignition/simplytestable-pagecache-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
The bundle for easy using json-rpc api on your project
Laravel 5 - Repositories to the database layer
Bundle Symfony DaplosBundle
http客户端
Easily add Excepct-CT header to your project
统计信息
- 总下载量: 147
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2018-08-27