Symfony 2 ESI Cache

时间:2016-04-07 14:27:18

标签: php symfony caching esi

我有一个在我的所有页面中调用的操作(仅限记录的人),此操作从我的推特帐户中检索最近的推文。

API访问受限,所以我希望此操作的结果在缓存中持续10分钟

public function socialAction(){

   $consumerKey = $this->container->getParameter('consumer_key');
   $consumerSecret = $this->container->getParameter('consumer_secret');
   $accessToken = $this->container->getParameter('access_token');
   $accessTokenSecret = $this->container->getParameter('access_token_secret');

   // on appel l'API
   $tweet = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
   $screen_name = "blabla";
   $tweets = $tweet->get('statuses/user_timeline', [
       'screen_name' => $screen_name,
       'exclude_replies' => true,
       'count' => 50
   ]);
   $tweets = array_splice($tweets, 0, 5);

   $response = $this->render('GestionJeuBundle:Default:social.html.twig', array("tweets" => $tweets));

   $response->setPublic();
   $response->setSharedMaxAge(600);

   return $response;

}

要启用缓存,我进行了以下更改

app/config/config.yml

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

app/AppCache.php


<?php

require_once __DIR__.'/AppKernel.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,
        );
    }
}

web/app_dev.php

<?php

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;

// If you don't want to setup permissions the proper way, just uncomment the following PHP line
// read http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
//umask(0000);

// This check prevents access to debug front controllers that are deployed by accident to production servers.
// Feel free to remove this, extend it, or make something more sophisticated.
if (isset($_SERVER['HTTP_CLIENT_IP'])
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
    || !in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '.....', 'fe80::1', '::1'))
) {
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
Debug::enable();

require_once __DIR__.'/../app/AppKernel.php';
require_once __DIR__.'/../app/AppCache.php';

$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$kernel = new AppCache($kernel);

// When using the HttpCache, you need to call the method in your front controller instead of relying on the configuration parameter
Request::enableHttpMethodParameterOverride();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();

$kernel->terminate($request, $response);
error_log($kernel->getLog());

尽管每次页面刷新都会更新页面(在测试之后,它在生产环境中执行完全相同的操作,同时也会在app.php上进行更改)

我是否误解或忘记了某件事?

提前感谢您的帮助。

编辑解决:我正在使用

呈现此操作
{{render(controller("GestionJeuBundle:Default:social")) }}

将其更改为

{{render_esi(controller("GestionJeuBundle:Default:social")) }}

解决我的问题

Hexune

2 个答案:

答案 0 :(得分:2)

我用

渲染了这个动作
{{render_esi(controller("GestionJeuBundle:Default:social")) }}

将其更改为

addFloatingIp

解决我的问题

答案 1 :(得分:0)

就我上周的实验而言,值得注意的是,如果您在Symfony中使用调试环境,则Varnish 总是将您的请求传递给后端。