【问题标题】:Symfony2: ESI setMaxAge CacheSymfony2:ESI setMaxAge 缓存
【发布时间】:2015-06-03 08:21:16
【问题描述】:

我有一个控制器,其动作在树枝中呈现

{{ render_esi(controller('MyWebsiteBundle:Element:header')) }}

Action 本身如下所示:

/**
     * @return Response
     */
    public function headerAction()
    {
        $currentLocale = $this->getCurrentLocale();

        $response = $this->render('MyWebsiteBundle:Element:header.html.twig', array(
            'currentLocale' => $currentLocale,
            'myTime' => time()
        ));
        $response->setPublic();
        $response->setSharedMaxAge(3600);

        return $response;
    }

当我重新加载浏览器时,"myTime" 每次都会更改。

如何使用setShardeMaxAge(),让Twig只有在MaxAge过期后才渲染?

【问题讨论】:

  • 你是用app_dev.php还是app.php访问页面?
  • 目前我使用 app_dev.php
  • 您是否在您的app_dev.php (howto?) 中启用了symfony 的内部缓存代理AppCache
  • require_once DIR.'/../app/AppCache.php'; $kernel = new AppKernel('dev', true); $kernel->loadClassCache(); // 用 AppCache 一包装默认的 AppKernel $kernel = new AppCache($kernel);
  • 我看到你已经接受了 Dan Revah 的回答 .. 只是出于好奇 - 你错过了答案的哪一部分?

标签: symfony caching esi


【解决方案1】:

在 Symfony2 中,您需要做一些事情来激活 esi 缓存。

1) 在app/config/config.yml 中确保您激活了带有片段路径的 esi。

framework:
    esi: { enabled: true }
    fragments: { path: /_proxy }

2) 用 AppCache 对象包裹内核

// web/app.php
$kernel = new AppCache($kernel); 

3) 设置 AppCache 配置

// app/AppCache.php
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;

class AppCache extends HttpCache
{
    protected function getOptions()
    {
        return array(
            'debug'                  => false,
            'default_ttl'            => 0,
            'private_headers'        => array('Authorization', 'Cookie'),
            'allow_reload'           => false,
            'allow_revalidate'       => false,
            'stale_while_revalidate' => 2,
            'stale_if_error'         => 60,
        );
    }
}

关于您的问题,如果它正在缓存您的响应,唯一的问题是每次刷新页面时它都会重新加载。确保配置 allow_reload 属性设置为 false。

您可以在此处阅读更多信息: http://symfony.com/doc/current/book/http_cache.html

【讨论】:

    猜你喜欢
    • 2012-07-11
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2011-09-20
    • 2014-10-06
    相关资源
    最近更新 更多