如何缓存REST资源的不同格式/表示?

时间:2012-07-16 06:58:00

标签: php rest http-headers http-caching

我使用tonic.php(http://peej.github.com/tonic/)库来创建我的REST资源。数据非常稳定,并且具有长缓存时间将是优选的。我设置了缓存头(使用tonic.php库):

$lastModified = gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME']) . ' GMT';                    
$expires = gmdate('D, d M Y H:i:s', ($_SERVER['REQUEST_TIME'] + $httpCacheDuration)) . ' GMT'; 
$response->addHeader('Cache-Control', 'public,max-age='.$httpCacheDuration.',must-revalidate');                    
$response->addHeader('Expires', $expires);
$response->addHeader('Last-Modified', $lastModified); 

问题在于,当请求html时,会对php页面进行cURL调用,并将返回的html放入响应正文中:

$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url . '?identifier=' . $identifier);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$html = curl_exec($ch); 
curl_close($ch);

$response->body = $html;

然后,返回的页面通过对同一资源的AJAX调用获取实际数据,但接受标题为“application / json”而不是“text / html”。 AJAX调用是用jquery完成的,如果我设置

cache: true

在jquery $ .ajax中使用accept:text / html调用我的资源只会将数据显示为JSON而不是网页(Firefox)或抛出错误(IE8)。 代码:

switch ($format) {

    case 'html':

        $response->addHeader('Content-type', 'text/html');
        $lastModified = gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME']) . ' GMT';                    
        $expires = gmdate('D, d M Y H:i:s', ($_SERVER['REQUEST_TIME'] + $httpCacheDuration)) . ' GMT';                                                      
        $response->addHeader('Cache-Control', 'public,max-age='.$httpCacheDuration.',must-revalidate');                    
        $response->addHeader('Expires', $expires);
        $response->addHeader('Last-Modified', $lastModified);  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_COOKIE, $strCookie);
        curl_setopt($ch, CURLOPT_URL, url . '?identifier=' . $identifier);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $html = curl_exec($ch);         
        curl_close($ch);
        $response->body = $html;
        return $response;
        break;

    case 'json':

        $result = DataManager::get($identifier);
        if (empty($result)) {
            $response->code = Response::NOTFOUND;
            return $response;
        }

        $lastModified = gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME']) . ' GMT';                    
        $expires = gmdate('D, d M Y H:i:s', ($_SERVER['REQUEST_TIME'] + $httpCacheDuration)) . ' GMT';                                                      
        $response->addHeader('Cache-Control', 'public,max-age='.$httpCacheDuration.',must-revalidate');                    
        $response->addHeader('Expires', $expires);
        $response->addHeader('Last-Modified', $lastModified);
        $response->addHeader('Content-type', 'application/json');
        $response->code = Response::OK;
        $response->body = json_encode($result);
        return $response;
        break;

    // we don't have a suitable format, so do a 406 response instead
    default:
        $response->code = Response::NOTACCEPTABLE;
        $response->addHeader('Content-type', 'text/plain');
        $response->body = getErrorPage(Response::NOTACCEPTABLE);
        return $response;
        break;
}

添加

$response->addHeader('Vary', 'Accept');

让它发挥作用。但是json永远不会被缓存,导致与在Jquery ajax调用中设置cache:false相同的行为。

如何缓存2个不同的表示并让浏览器为请求的accept-header显示正确的表示?

1 个答案:

答案 0 :(得分:0)

答案在评论1中:

  

如果更改网址怎么样?如果我理解你的问题,那么   浏览器缓存不区分基于Accept标头的内容   在请求中。如果将.json附加到JSON数据包的URL,   浏览器将能够根据URL进行缓存。

相关问题