Symfony2缓存过期

时间:2014-03-25 14:08:27

标签: symfony caching

我的控制器中有以下操作:

public function popularPlacesAction()
{
    $em = $this->getDoctrine()->getManager();

    $query = $em->createQuery(
        "SELECT COUNT(s.id) as total, l.city as name, l.country_code as country_code
          FROM AcmeMyBundle:Sample s 
          LEFT JOIN s.location l 
          WHERE l.city != ''
          GROUP BY name
          ORDER BY total DESC"
    )->setMaxResults(15);

    $cities = $query->getResult();

    return array(
        'cities' => $cities
    );
}

我想使用缓存来设置响应的过期日期,如果查询结果发生了变化则不重要。我只想将响应设置为公共,并且每小时刷新响应例如。当响应来自缓存时,如何在不执行学说查询的情况下完成此操作?

1 个答案:

答案 0 :(得分:3)

您需要使用一些缓存方法(可能是Varnish或symfony&#39的反向代理),然后为您的操作响应设置一些缓存标头。看看这里: Expiration with the Cache-Control Header 例如:

public function popularPlacesAction()
{
    ... 
    $response = new Response();
    $response->setSharedMaxAge(3600);
    $response->setContent(....);
    return $response;
}

希望这会有所帮助。其他文档: HTTP Cache