Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony2:使用http缓存_Symfony_Http_Caching - Fatal编程技术网

Symfony2:使用http缓存

Symfony2:使用http缓存,symfony,http,caching,Symfony,Http,Caching,在Symfony2中,我尝试对一些ajax调用使用http缓存,这些调用在一天内总是返回相同的内容 我的控制器代码以以下内容结束: $response = new JsonResponse($data); $response->setPublic(); $response->setExpires(new \DateTime("+1 hour")); $response->setMaxAge(3600); return $response;

在Symfony2中,我尝试对一些ajax调用使用http缓存,这些调用在一天内总是返回相同的内容

我的控制器代码以以下内容结束:

    $response = new JsonResponse($data);
    $response->setPublic();
    $response->setExpires(new \DateTime("+1 hour"));
    $response->setMaxAge(3600);
    return $response;
在响应(来自Firefox或IE11)中,我可以找到预期的标题。但它总是返回200代码(OK),而不是304代码(未修改)。这意味着服务器总是生成响应,而我的目标是使服务器负载不足

我还尝试使用@Cache注释获得相同的结果


如何使其工作?

您可以使用app.PHP中的AppCache内核(用PHP编写的反向代理)使symfony返回304响应

$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$kernel = new AppCache($kernel); // Uncomment or add this line to app.php
在生产上,你应该考虑使用一个真正的反向代理服务器,比如ValEng.<

另一种方法是,在执行常规操作(如查询数据库和获取内容)之前,使用控制器中的
cache
服务手动缓存数据。只需检查是否已经存在缓存数据,然后将此数据发送给用户,否则,请执行常规操作并将新数据保存在缓存中
JsonResponse
class获取状态代码的第二个参数,您可以在此处手动设置304

$cache = $this->container->get( 'cache' );
$cache_key = 'trafficfeed';
    if ( $cache->contains( $cache_key ) ) {
        $data  = $cache->fetch($cache_key  );
        /** here $data is a cached data */
        /** Response::HTTP_NOT_MODIFIED => 304*/
        $response = new JsonResponse($data,Response::HTTP_NOT_MODIFIED);
        return $response;
    }
/** Fetch fresh data and save in cache */
$cache->save( $cache_key, $data, 3600 );
$response = new JsonResponse($data);
对于缓存服务,您需要在配置中定义它

services:

    cache:
        class: Doctrine\Common\Cache\PhpFileCache
        arguments: [%kernel.cache_dir%]

通过在响应上添加
LastModified
头,我成功地缓存了您的示例响应。 我做以下两件事:

1。将Symfony配置为反向代理

我创建了一个自定义环境,在web文件夹中,我创建了一个名为app_cache的文件。如下所述:

#web/app_cache.php
<?php

// web/app.php
require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
require_once __DIR__.'/../app/AppCache.php';

use Symfony\Component\HttpFoundation\Request;

$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();
// wrap the default AppKernel with the AppCache one
$kernel = new AppCache($kernel);

$request = Request::createFromGlobals();

$response = $kernel->handle($request);
$response->send();

$kernel->terminate($request, $response);
3。测试

我清除prod环境的缓存(
app/cache-e=prod
),并使用浏览器向url发出两个请求:

第一个返回200 Ok,第二个返回304 not modified


希望有此帮助

您在使用什么来执行AJAX请求?有些库不向AJAX请求添加缓存查询字符串。我使用Jquery,试图在$中设置{cache:true}。AJAX参数不会更改任何内容。使用setLastModified对我来说是可以的,但仅适用于公共缓存(非私有)。那已经很酷了!嗨@Rolintocour欢迎你!当然,你的问题是关于公共缓存的。如果你认为我的回答有用,请考虑赞成和/或接受我的回答。
/**
 * @Route("/cache-me", name="cacheme")
 */
public function cacheMeAction(Request $request)
{
    $data = array("hello" => "symfony");

    $response = new JsonResponse($data);
    $response->setPublic();
    $response->setExpires(new \DateTime("+1 hour"));
    $response->setLastModified(new \DateTime("-1 hour"));

    return $response;
}