2012-07-16 32 views
1

我使用tonic.php(http://peej.github.com/tonic/)庫創建我的REST資源。數據非常穩定,並且緩存時間長可能更可取。我設置了緩存頭(使用tonic.php庫):如何緩存REST資源的不同格式/表示形式?

$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網頁製作和返回的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的$

與阿賈克斯接受我的資源的調用:text/html的將只顯示數據作爲JSON,而不是網頁(火狐)或拋出一個錯誤(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個不同的表示並讓瀏覽器爲請求的accept-header顯示正確的表示?

+1

如果您更改網址,那麼該怎麼辦?如果我瞭解您的問題,則瀏覽器緩存不會根據請求中的Accept標頭區分內容。如果將'.json'附加到JSON數據包的URL,則瀏覽器將能夠根據URL進行緩存。 – Cheeso 2012-07-17 05:40:03

+0

謝謝。這樣做的工作也讓你想知道什麼點不同:接受是如果它不工作,並且必須使用「hacky解決方法」。 – 2012-07-27 06:32:56

+0

猜測......也許'Vary'是堆棧中不太成熟的部分之一。 – Cheeso 2012-07-27 16:50:24

回答

0

答案是註釋1:

怎麼樣,如果你改變網址?如果我瞭解您的問題,則 瀏覽器緩存不會根據請求中的Accept標頭 來區分內容。如果將.json附加到JSON數據包的URL,則 瀏覽器將能夠根據URL進行緩存。

相關問題