Skip to content

feat: HTTP method-aware web page caching#8364

Merged
kenjis merged 3 commits into
codeigniter4:4.5from
kenjis:page-cache-http-method
Dec 28, 2023
Merged

feat: HTTP method-aware web page caching#8364
kenjis merged 3 commits into
codeigniter4:4.5from
kenjis:page-cache-http-method

Conversation

@kenjis

@kenjis kenjis commented Dec 24, 2023

Copy link
Copy Markdown
Member

Description
Closes #8363

Enhancement to https://codeigniter4.github.io/CodeIgniter4/general/caching.html

  • add HTTP method to page cache key

The current issue:
If we use RESTful Resource Handling, operation Create and List have the same URI.
https://codeigniter4.github.io/CodeIgniter4/incoming/restful.html#presenter-controller-comparison
If we enable page caching to List (GET), Create (POST) will return the cached List response.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added enhancement PRs that improve existing functionalities 4.5 labels Dec 24, 2023
@kenjis kenjis force-pushed the page-cache-http-method branch 2 times, most recently from da37c9c to d182491 Compare December 24, 2023 04:11
Comment thread system/Filters/PageCache.php Outdated
{
assert($request instanceof CLIRequest || $request instanceof IncomingRequest);

if ($this->isNotCacheableMethod($request)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what's cachable? GET and HEAD? If that's all, and HEAD isn't something we really need to worry about caching, wouldn't it be simpler to just check if $request->getMethod() !== 'get'?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ResponseCache also caches the headers, so in theory, it can be useful for HEAD... however, as I mentioned in my comment, I would make it configurable and stick to the GET by default.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what's cachable?

GET, HEAD, POST, PATCH, CLI.
But a dev must put $this->cachePage($n); in controller.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But a dev must put $this->cachePage($n); in controller.

Ok, that changes a lot.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dropped the check.

To begin with, page caching must be specified by developers in controllers.
There is no need for the filter to dare to check the HTTP method to limit the cache functionality.

Besides, if a developer wants to determine to enable caching by HTTP method, they can customize the filter.

@michalsn

Copy link
Copy Markdown
Member

Enabling caching for POST requests by default is rather controversial. I would be in favor of adding a config for this.

While I can imagine a scenario where we want to cache the POST response, the most common action for POST requests (which change the state of the application) will be to respond with new content every time.

@kenjis

kenjis commented Dec 24, 2023

Copy link
Copy Markdown
Member Author

Ah, strictly speaking, the current this PR has a breaking change?
Because the current implementation (4.5) can cache any HTTP method request.

@kenjis kenjis force-pushed the page-cache-http-method branch from d182491 to c6a9112 Compare December 25, 2023 07:22

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kenjis kenjis force-pushed the page-cache-http-method branch from c6a9112 to 0c01fbc Compare December 25, 2023 21:55
@kenjis

kenjis commented Dec 25, 2023

Copy link
Copy Markdown
Member Author

I forgot to document. Added.

@kenjis kenjis force-pushed the page-cache-http-method branch from 2715695 to 5f7c7d5 Compare December 26, 2023 01:08
@lonnieezell

Copy link
Copy Markdown
Member

LGTM

@kenjis kenjis merged commit af197ea into codeigniter4:4.5 Dec 28, 2023
@kenjis kenjis deleted the page-cache-http-method branch December 28, 2023 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement PRs that improve existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants