vidoomymedia / phpfastcache-bundle
Phpfastcache Bundle 用于 Symfony 集成
3.0.4
2020-12-18 08:29 UTC
Requires
- php: ^7.2
- ext-json: *
- ext-mbstring: *
- phpfastcache/phpfastcache: ^8.0
- symfony/console: ^3.4 | ^4.0 || ^5.0
- symfony/dependency-injection: ^3.4 | ^4.0 | ^5.0
- symfony/framework-bundle: ^3.4 | ^4.0 | ^5.0
- symfony/http-foundation: ^3.4 | ^4.0 | ^5.0
- symfony/yaml: ^3.4 | ^4.0 | ^5.0
Requires (Dev)
- phpunit/phpunit: ^6.2 | ^7.1
- predis/predis: ^1.0
- symfony/browser-kit: ^3.4 | ^4.0 | ^5.0
- symfony/console: ^3.4 | ^4.0 | ^5.0
- symfony/phpunit-bridge: ^3.4 | ^4.0 | ^5.0
- symfony/profiler-pack: ^1.0
- symfony/twig-bundle: ^3.4 | ^4.0 | ^5.0
Suggests
- symfony/web-profiler-bundle: To use the data collector.
This package is not auto-updated.
Last update: 2024-09-22 00:07:52 UTC
README
Symfony Flex PhpFastCache Bundle
⚠️ 注意:V3 版本的 PhpFastCache Bundle 是一个重大的(不向后兼容)更新!
从 V3 版本开始,该组件与之前版本完全不兼容。
为了确保您能够顺利完成迁移,请检查 Resources/Docs 目录中的迁移指南。
最大的变化之一是 Phpfastcache 的依赖项,它未设置为 v7,完全不向后兼容。
👍 第 1 步:使用 composer 将 PhpFastCache Bundle 包含到您的项目中
composer require phpfastcache/phpfastcache-bundle
🚧 第 2 步:设置您的 config/packages/phpfastcache.yaml
以配置您的缓存实例
# PhpFastCache configuration phpfastcache: twig_driver: "filecache" # This option must be a valid declared driver, in our example: "filecache" twig_block_debug: false # This option will wrap CACHE/ENDCACHE blocks with block debug as HTML comment drivers: filecache: type: Files parameters: path: "%kernel.cache_dir%/phpfastcache/"
- 可以使用 Symfony recipes 跳过此步骤。
🚀 第 3 步:通过使用 PhpFastCache 服务加速您的应用程序
在控制器中缓存数据
public function indexAction(Request $request, Phpfastcache $phpfastcache) { $cache = $phpfastcache->get('filecache'); $item = $cache->getItem('myAppData'); if (!$item->isHit() || $item->get() === null) { $item->set('Wy app has now superpowers !!')->expiresAfter(3600);//1 hour $cache->save($item); } // replace this example code with whatever you need return $this->render('default/index.html.twig', [ 'myAppData' => $item->get(), 'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'), ]); }
或在您的模板中
<div> {# * 'myrandom6' Is your cache key identifier, must be unique * 300 Is the time to live (TTL) before the cache expires and get regenerated #} {% cache 'myrandom6' 300 %} <textarea> <!-- Some heavy stuff like Doctrine Lazy Entities --> {% for i in 1..1000 %}{{ random() }}{% endfor %} </textarea> {% endcache %} </div>
💻 CLI 命令交互
从 V3 版本开始,引入了一些命令行工具,主要用于 CRUD 类型的操作。
GET 操作
php bin/console phpfastcache:get filecache cacheKey
如果缓存项最终存在,这将显示缓存项的内容。
SET 操作
php bin/console phpfastcache:get filecache cacheKey '{"a": 14}' 300 -a 1
这将设置缓存项的内容。
TTL(300)秒是可选的,默认值由您的配置文件中填充。auto-type-cast
选项 "-a"(默认启用)将允许您自动类型转换您的变量
false
和true
将分别转换为 布尔值。1337
和1337.666
将分别转换为 整数 或 浮点数。null
将转换为 null。{"a": 14}
将使用 JSON 识别转换为关联 数组。常规字符串
将保持不变,并保持为字符串。
您可以通过关闭 auto-type-cast
选项来禁用此行为:-a 0
DELETE 操作
php bin/console phpfastcache:del filecache cacheKey
这将删除指定的缓存项。
CLEAR 操作
php bin/console phpfastcache:clear filecache
# OR to clear every caches:
php bin/console phpfastcache:clear
如果指定,这将清除单个缓存实例,否则清除所有配置的缓存实例。
💡 介绍可缓存响应(仅限 V3)
从 V3 版本开始,有一个更简单、更干净的方式来设置 HTTP 缓存,以减少您的服务器带宽以及 CPU 负载:可缓存响应。而且实现起来非常简单。
/** * @Route("/cached", name="cached") */ public function cachedAction(Phpfastcache $phpfastcache, Request $request): Response { return (new CacheableResponse($phpfastcache->get('filecache'), $request))->getResponse('cache_key', 3600, function () { return new Response('Random bytes: ' . \random_bytes(255)); }); }
CacheableResponse
由 \Phpfastcache\Bundle\Response\CacheableResponse
提供。此类将处理响应头(缓存控制、ETag 等)和 HTTP 状态(304 未修改)。
💥 PhpFastCache Bundle 支持
发现了一个问题或有想法?请在此 处告诉我们!