phpfastcache/phpfastcache-bundle

该包已被废弃,不再维护。未建议替代包。

Phpfastcache Bundle for Symfony集成

3.0.0 2018-05-27 14:54 UTC

README

Code Climate Scrutinizer Code Quality Build Status Latest Stable Version Total Downloads License

Symfony Flex PhpFastCache Bundle

⚠️ 注意:V3 是 PhpFastCache Bundle 的重大(BC 破坏性)更新!

从 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/"

🚀 步骤 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"(默认启用)将允许您自动类型转换您的变量

  • falsetrue 分别转换为 布尔值
  • 13371337.666 分别转换为 整数浮点数
  • null 转换为 null
  • {"a": 14} 使用 JSON 检测转换为关联 数组
  • 常规字符串 保持不变,并保留为字符串。

您可以通过关闭“auto-type-cast”选项来禁用此行为:-a 0

删除操作
php bin/console phpfastcache:del filecache cacheKey

这将删除指定的缓存项。

清除操作
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支持

发现了问题或有了想法?请到这里这里告诉我们!