Skip to content

Commit

Permalink
Merge pull request #8364 from kenjis/page-cache-http-method
Browse files Browse the repository at this point in the history
feat: HTTP method-aware web page caching
  • Loading branch information
kenjis authored Dec 28, 2023
2 parents f34fe29 + 5f7c7d5 commit af197ea
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
2 changes: 1 addition & 1 deletion system/Cache/ResponseCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function generateCacheKey($request): string
? $uri->getQuery(is_array($this->cacheQueryString) ? ['only' => $this->cacheQueryString] : [])
: '';

return md5((string) $uri->setFragment('')->setQuery($query));
return md5($request->getMethod() . ':' . $uri->setFragment('')->setQuery($query));
}

/**
Expand Down
36 changes: 30 additions & 6 deletions system/HTTP/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,57 +19,81 @@
class Method
{
/**
* Safe: No
* Idempotent: No
* Cacheable: No
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT
*/
public const CONNECT = 'CONNECT';

/**
* Idempotent
* Safe: No
* Idempotent: Yes
* Cacheable: No
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/DELETE
*/
public const DELETE = 'DELETE';

/**
* Safe, Idempotent, Cacheable
* Safe: Yes
* Idempotent: Yes
* Cacheable: Yes
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET
*/
public const GET = 'GET';

/**
* Safe, Idempotent, Cacheable
* Safe: Yes
* Idempotent: Yes
* Cacheable: Yes
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD
*/
public const HEAD = 'HEAD';

/**
* Safe, Idempotent
* Safe: Yes
* Idempotent: Yes
* Cacheable: No
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
*/
public const OPTIONS = 'OPTIONS';

/**
* Safe: No
* Idempotent: No
* Cacheable: Only if freshness information is included
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH
*/
public const PATCH = 'PATCH';

/**
* Safe: No
* Idempotent: No
* Cacheable: Only if freshness information is included
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST
*/
public const POST = 'POST';

/**
* Idempotent
* Safe: No
* Idempotent: Yes
* Cacheable: No
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PUT
*/
public const PUT = 'PUT';

/**
* Safe, Idempotent
* Safe: Yes
* Idempotent: Yes
* Cacheable: No
*
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/TRACE
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/system/Cache/ResponseCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ public function testCachePageIncomingRequestWithCacheQueryString()
$this->assertNull($cachedResponse);
}

public function testCachePageIncomingRequestWithHttpMethods()
{
$pageCache = $this->createResponseCache();

$request = $this->createIncomingRequest('foo/bar');

$response = new Response($this->appConfig);
$response->setBody('The response body.');

$return = $pageCache->make($request, $response);

$this->assertTrue($return);

// Check cache with a request with the same URI path and different HTTP method
$request = $this->createIncomingRequest('foo/bar')->withMethod('POST');
$cachedResponse = $pageCache->get($request, new Response($this->appConfig));

$this->assertNull($cachedResponse);
}

public function testCachePageCLIRequest()
{
$pageCache = $this->createResponseCache();
Expand Down
3 changes: 3 additions & 0 deletions user_guide_src/source/changelogs/v4.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ Others
existing CSP directives. See :ref:`csp-clear-directives`.
- **HTTP:** Added ``Message::addHeader()`` method to add another header with
the same name. See :php:meth:`CodeIgniter\\HTTP\\Message::addHeader()`.
- **Web Page Caching:** ``ResponseCache`` has been improved to include the request
HTTP method in the cache key. This means that the same URI will be cached separately
if the HTTP method is different.

Message Changes
***************
Expand Down
14 changes: 10 additions & 4 deletions user_guide_src/source/general/caching.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ How Does Caching Work?
======================

Caching can be enabled on a per-page basis, and you can set the length
of time that a page should remain cached before being refreshed. When a
page is loaded for the first time, the page will be cached using the
of time that a page should remain cached before being refreshed.

.. note:: Per-page means per URI. Since v4.5.0, the HTTP method of the
request is also taken into account. This means that the same URI will be
cached separately if the HTTP method is different.

When a page is loaded for the first time, the page will be cached using the
currently configured cache engine. On subsequent page loads, the cache
will be retrieved and sent to the requesting user's browser. If it has
expired, it will be deleted and refreshed before being sent to the
will be retrieved and sent to the requesting user's browser.

If it has expired, it will be deleted and refreshed before being sent to the
browser.

.. note:: The Benchmark tag is not cached so you can still view your page
Expand Down

0 comments on commit af197ea

Please sign in to comment.