Esempio n. 1
0
 /**
  * Processes the request, checking for attached
  * Cache decorators. If decorators are found,
  * the request performs the following tasks.
  * 
  * 1. Checks for existing cached response, returning valid hits
  * 2. Runs the Kohana_Request::execute() logic to create a response
  * 3. Caches the response using a decorator if valid
  *
  *     $request->execute();
  *
  * @return  Kohana_Response
  * @throws  Kohana_Exception
  * @uses    [Kohana::$profiling]
  * @uses    [Profiler]
  * @uses    [Request_Cache]
  */
 public function execute()
 {
     // If there are no cache adaptors
     if (!Request::$cache->cache_decorators) {
         // Get out of here
         return parent::execute();
     }
     // Create the cache key
     $key = $this->create_cache_key($this->uri());
     // Try and retrieve a cached response
     $response = Request::$cache->get($key);
     // If a response is returned
     if ($response instanceof Kohana_Response) {
         // Return the cached response
         return $response;
     } else {
         // Execute the contoller to get the response
         $response = parent::execute();
     }
     // If the response method is in the allow list
     if (!in_array($this->method, Request_Cache::$cache_methods_allow)) {
         // return FALSE
         return $response;
     } else {
         if (!Request_Cache::validate_set($response)) {
             // return the non-cached result
             return $response;
         }
     }
     // Set the response to cache
     Request::$cache->set($key, $response);
     // Return the response
     return $response;
 }
Esempio n. 2
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;
 }
Esempio n. 3
0
<?php

defined('SYSPATH') or die('No direct script access.');
/**
 * Applies Request_Cache_Decorators to the Kohana_Request
 * class. Multiple cache decorators can be applied and are
 * executed in the order that they are applied. This code
 * can be moved to the application/bootstrap.php file.
 */
Request::$cache = Request_Cache::instance()->attach(Request_Cache_Decorator::instance('null'))->attach(Request_Cache_Decorator::instance('memcache'));
// For now limit this to cli mode (need to find a good handle for this)
if (Kohana::$is_cli) {
    define('SUPPRESS_REQUEST', TRUE);
    define('GEARMAN_WORKER_' . Request_Async_Gearman::$context, TRUE);
}
Esempio n. 4
0
 /**
  * Gets a response from the cache decorators based on `$key`. [Request_Cache] will
  * iterate through each attached decorator searching for a hit. When a response
  * is found, if it is still fresh it will be returned.
  * 
  *     // Search for a response
  *     if (($response = Request::$cache->get('foo')) instanceof Kohana_Response)
  *     {
  *          // Return the response
  *          return $response;
  *     }
  *
  * @param   string   key of the response to fetch from cache
  * @return  boolean|Kohana_Response
  */
 public function get($key)
 {
     // If there are no decorators
     if (!$this->cache_decorators) {
         // return
         return FALSE;
     }
     // Foreach decorator
     foreach ($this->cache_decorators as $decorator) {
         try {
             // Try and load the response by key
             $cached_response = $decorator->get($key);
             // If the cache response is valid
             if ($cached_response instanceof Kohana_Response and Request_Cache::validate_get($cached_response)) {
                 // return the response
                 return $cached_response;
             }
         } catch (Exception $e) {
             // If cache exceptions should be thrown
             if (!$this->silent_cache_fail) {
                 // Throw the exception
                 throw $e;
             }
         }
     }
     // return false if no response was found
     return FALSE;
 }