Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: HTTP method-aware web page caching #8364

Merged
merged 3 commits into from
Dec 28, 2023

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Dec 24, 2023

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
@@ -65,6 +66,10 @@ public function after(RequestInterface $request, ResponseInterface $response, $a
{
assert($request instanceof CLIRequest || $request instanceof IncomingRequest);

if ($this->isNotCacheableMethod($request)) {
Copy link
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
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
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
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
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
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
Copy link
Member Author

kenjis commented Dec 24, 2023

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

Copy link
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

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

LGTM

@kenjis
Copy link
Member Author

kenjis commented Dec 25, 2023

I forgot to document. Added.

@lonnieezell
Copy link
Member

LGTM

@kenjis kenjis merged commit af197ea into codeigniter4:4.5 Dec 28, 2023
46 checks passed
@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
4.5 enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants