cache() public method

Sets the correct headers to instruct the client to cache the response.
public cache ( string $since, string $time = '+1 day' ) : void
$since string a valid time since the response text has not been modified
$time string a valid time for cache expiry
return void
Beispiel #1
0
 /**
  * Sends an asset file to the client
  *
  * @param \Cake\Network\Request $request The request object to use.
  * @param \Cake\Network\Response $response The response object to use.
  * @param string $assetFile Path to the asset file in the file system
  * @param string $ext The extension of the file to determine its mime type
  * @return void
  */
 protected function _deliverAsset(Request $request, Response $response, $assetFile, $ext)
 {
     $compressionEnabled = $response->compress();
     if ($response->type($ext) === $ext) {
         $contentType = 'application/octet-stream';
         $agent = $request->env('HTTP_USER_AGENT');
         if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
             $contentType = 'application/octetstream';
         }
         $response->type($contentType);
     }
     if (!$compressionEnabled) {
         $response->header('Content-Length', filesize($assetFile));
     }
     $response->cache(filemtime($assetFile), $this->_cacheTime);
     $response->sendHeaders();
     readfile($assetFile);
     if ($compressionEnabled) {
         ob_end_flush();
     }
 }
Beispiel #2
0
 /**
  * Tests the cache method
  *
  * @return void
  */
 public function testCache()
 {
     $response = new Response();
     $since = time();
     $time = new \DateTime('+1 day', new \DateTimeZone('UTC'));
     $response->expires('+1 day');
     $expected = ['Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT', 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT', 'Expires' => $time->format('D, j M Y H:i:s') . ' GMT', 'Cache-Control' => 'public, max-age=' . ($time->format('U') - time())];
     $response->cache($since);
     $this->assertEquals($expected, $response->header());
     $response = new Response();
     $since = time();
     $time = '+5 day';
     $expected = ['Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT', 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT', 'Expires' => gmdate("D, j M Y H:i:s", strtotime($time)) . " GMT", 'Cache-Control' => 'public, max-age=' . (strtotime($time) - time())];
     $response->cache($since, $time);
     $this->assertEquals($expected, $response->header());
     $response = new Response();
     $since = time();
     $time = time();
     $expected = ['Date' => gmdate("D, j M Y G:i:s ", $since) . 'GMT', 'Last-Modified' => gmdate("D, j M Y H:i:s ", $since) . 'GMT', 'Expires' => gmdate("D, j M Y H:i:s", $time) . " GMT", 'Cache-Control' => 'public, max-age=0'];
     $response->cache($since, $time);
     $this->assertEquals($expected, $response->header());
 }
Beispiel #3
0
 /**
  * Sends an asset file to the client
  *
  * @param \Cake\Network\Request $request The request object to use.
  * @param \Cake\Network\Response $response The response object to use.
  * @param string $assetFile Path to the asset file in the file system
  * @param string $ext The extension of the file to determine its mime type
  * @return void
  */
 protected function _deliverCacheFile(Request $request, Response $response, $file, $ext)
 {
     $compressionEnabled = $response->compress();
     if ($response->type($ext) === $ext) {
         $contentType = 'application/octet-stream';
         $agent = $request->env('HTTP_USER_AGENT');
         if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
             $contentType = 'application/octetstream';
         }
         $response->type($contentType);
     }
     if (!$compressionEnabled) {
         $response->header('Content-Length', filesize($file));
     }
     $content = file_get_contents($file);
     $cacheInfo = $this->extractCacheInfo($content);
     $modifiedTime = filemtime($file);
     $cacheTime = $cacheInfo['time'];
     if (!$cacheTime) {
         $cacheTime = $this->_cacheTime;
     }
     $response->cache($modifiedTime, $cacheTime);
     $response->type($cacheInfo['ext']);
     if (Configure::read('debug') || $this->config('debug')) {
         if ($cacheInfo['ext'] === 'html') {
             $content = '<!--created:' . $modifiedTime . '-->' . $content;
         }
     }
     $response->body($content);
 }