/** * Executes the supplied [Request] with the supplied [Request_Client]. * Before execution, the HTTP_Cache adapter checks the request type, * destructive requests such as `POST`, `PUT` and `DELETE` will bypass * cache completely and ensure the response is not cached. All other * Request methods will allow caching, if the rules are met. * * @param Request_Client $client client to execute with Cache-Control * @param Request $request request to execute with client * @return [Response] */ public function execute(Request_Client $client, Request $request, Response $response) { if (!$this->_cache instanceof Cache) { return $client->execute_request($request, $response); } // If this is a destructive request, by-pass cache completely if (in_array($request->method(), array(HTTP_Request::POST, HTTP_Request::PUT, HTTP_Request::DELETE))) { // Kill existing caches for this request $this->invalidate_cache($request); $response = $client->execute_request($request, $response); $cache_control = HTTP_Header::create_cache_control(array('no-cache', 'must-revalidate')); // Ensure client respects destructive action return $response->headers('cache-control', $cache_control); } // Create the cache key $cache_key = $this->create_cache_key($request, $this->_cache_key_callback); // Try and return cached version if (($cached_response = $this->cache_response($cache_key, $request)) instanceof Response) { return $cached_response; } // Start request time $this->_request_time = time(); // Execute the request with the Request client $response = $client->execute_request($request, $response); // Stop response time $this->_response_time = time() - $this->_request_time; // Cache the response $this->cache_response($cache_key, $request, $response); $response->headers(HTTP_Cache::CACHE_STATUS_KEY, HTTP_Cache::CACHE_STATUS_MISS); return $response; }
/** * Tests that `create_cache_control()` outputs the correct cache control * string from the supplied input * * @dataProvider provider_create_cache_control * * @param array $input input * @param string $expected expected * @return void */ public function test_create_cache_control(array $input, $expected) { $this->assertSame($expected, HTTP_Header::create_cache_control($input)); }