【问题标题】:How to cache different formats/representations of REST resource?如何缓存 REST 资源的不同格式/表示?
【发布时间】:2012-07-15 00:20:59
【问题描述】:

我使用 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 调用中为 false。

如何缓存 2 种不同的表示并让浏览器为请求的接受标头显示正确的表示?

【问题讨论】:

  • 如果你改变 URL 怎么样?如果我理解您的问题,浏览器缓存不会根据请求中的 Accept 标头区分内容。如果您将.json 附加到JSON 数据包的URL,浏览器将能够根据该URL 进行缓存。
  • 谢谢。确实有效也让你想知道变化的意义是什么:接受是如果它不起作用并且必须使用“hacky workarounds”。
  • 猜测......也许Vary 是堆栈中不太成熟的部分之一。

标签: php rest http-headers http-caching


【解决方案1】:

答案在评论1:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多