Exemple #1
0
 /**
  * Prepares the response for caching by the decorator. Sets up headers and renders the response
  * body due to some caching methods not serialising [Kohana_View] correctly. The `_prepare_response()`
  * method performs the following.
  *
  * 1.  Set correct headers to the Response object, formatting `Cache-Control` correctly
  * 2.  Creates an `Expires:` header if required
  * 3.  Renders the [Kohana_Reponse]::`$body`
  *
  * @param   Kohana_Response  response 
  * @return  Kohana_Response
  */
 protected function _prepare_response(Kohana_Response $response)
 {
     // Get the cache control headers
     $cache_control = Request_Cache::parse_cache_control($response->headers);
     // Set the lifetime of the cache from the header
     $this->_lifetime = (int) $cache_control['max-age'];
     // Get time now
     $time = time();
     // If the expires header is not set
     if (!isset($response->headers['Expires'])) {
         // Calculate expires header (DateTime would probably be better here - SdF)
         $expires = gmdate('D, d M Y H:i:s T', $time + $cache_control['max-age']);
     }
     // Tell caches to check their validation
     $cache_control['must-revalidate'] = NULL;
     // Replace the headers with those that are not set
     $response->headers += array('Cache-Control' => Request_Cache::create_cache_control($cache_control), 'Expires' => $expires, 'Last-Modified' => gmdate('D, d M Y H:i:s T', $time), 'Content-Length' => strlen((string) $response->body));
     // Render the body (some caches have trouble serializing)
     $response->body = (string) $response->body;
     // return
     return $response;
 }
Exemple #2
0
 /**
  * Validates a cached response to check it is still fresh and available
  * to the client. All validation is based on the `max-age` Cache-Control
  * header.
  *
  *     // Get a response from the cache
  *     $response = Request::$cache->get('foo');
  * 
  *     // Test the response
  *     if (Request_Cache::validate_get($response))
  *     {
  *          // Return the valid cached response
  *          return $response;
  *     }
  *
  * @param   Kohana_Response resposne
  * @return  boolean
  */
 public static function validate_get(Kohana_Response $response)
 {
     // Get the Cache-Control Header
     $cache_control = Request_Cache::parse_cache_control((array) $response->headers);
     // If the response has expired
     if ($cache_control['max-age'] + time() < time()) {
         // Remove this entry
         Request_Cache::delete($key);
         // return false
         return FALSE;
     }
     // Return true
     return TRUE;
 }