effiana/phpfastcache-bundle

Phpfastcache Bundle for Symfony集成

安装: 52

依赖者: 0

建议者: 0

安全: 0

星级: 0

观察者: 1

分支: 2

类型:symfony-bundle

3.0.3 2020-01-24 11:25 UTC

This package is auto-updated.

Last update: 2024-09-24 21:34:34 UTC


README

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

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

🚀 步骤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-like操作。

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

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支持

发现了一个问题或有一个想法?请在此告诉我们!