-
Notifications
You must be signed in to change notification settings - Fork 6
Added a PSR6 cache plugin #15
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
Conversation
*/ | ||
private function isCacheableResponse(ResponseInterface $response) | ||
{ | ||
return floor($response->getStatusCode() / 100) === 2; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think $statusCode >= 200 && $statusCode < 300 would be more readable. symfony is doing this:
i think we should respect cache control headers from the server.
looks cool! before we merge, i think you should add the psr cache to require-dev also and add some unit tests. |
Thank you for the feedback. I agree and I will make some changes. I have not added psr/cache to the require dev because it is not on packagist yet. |
* @param int $defaultTtl | ||
* @param bool $respectCacheHeaders | ||
*/ | ||
public function __construct(CacheItemPoolInterface $pool, $defaultTtl = 60, $respectCacheHeaders = true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, but shouldn't this be multiline? (PSR)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only if more that 80 chars if I remember correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for a $options array with default_ttl
and respect_cache_headers
options for now. will be easier to scale.
Nice one @Nyholm, thanks. A few more ideas:
|
I have not added psr/cache to the require dev because it is not on
packagist yet.
ah sorry, makes sense :-) i will update the PR description with a
tasklist to keep an overview
|
i would not use s-maxage i think. writing a proxy in PHP sounds like a weird idea. people could still extend the plugin if they found a use case for this and overwrite the getMaxAge method. or do a pull request for an option to use s-maxage instead of max-age. |
Thank you for the feedback. I have reduced the number of small one liner functions and improved the About the Shared Max Age: I think (not sure) that shared max age should not be used by browser or clients. Only by reversed proxies. About using array in the constructor: Is this forward compatible? Wouldn't a configuration object be better? |
I don't think we need a configuration object. An array can be extended dynamically without BC breaks, new options can be documented. Not sure if we need it, just mentioned the possibility. |
we could use the OptionsResolver from symfony would allow to validate the configuration. could be a nice compromise. a value object for configuration is a lot of effort and a bit awkward e.g. in symfony. |
Okey, I have added some tests and make sure to use an array as second constructor argument to be more extendable in the future. |
What about etag and receving 304 Not modified ? |
Do we want to support 304 or etag? If we do, we still have to make a HTTP request. The win is that we can get a small request back with a 304 header if the resource is "Not modified". |
If-None-Match / If-Not-Modified headers would be really cool i think. for example a client doing some polling on a server that supports etags / last-modified headers. i was thinking if we can add that separately, but i think its tightly coupled - you need the cached answer for the 304 to make any sense. the user of HTTPlug will still want to receive the answer. so ideally we just have an option if validation should be attempted - i guess it can default to true, its part of the HTTP spec. in terms of caching, the tricky bit will be to keep answers cached for longer than max-age says, if they have an etag/last-modified header. basically we could keep them indefinitely. maybe we could have an optional config option for a maximum cache lifetime. btw, note that if both last-modified and etag are present, BOTH have to be validated (in the sense of an AND, not OR). |
I've updated the PR. The composer.json includes the git repository for psr/cache. Once the voting is complete we could use packagist instead. |
|
||
if ($cacheItem->isHit()) { | ||
// return cached response | ||
return $cacheItem->get(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum, we save a response and so we return a reponse (when hitting the cache), but this method must return a promise, don't think it will work.
Should it not be a new FulfilledPromise($cacheItem->get())
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are correct. Thank you for finding this issue!
Thank you for the feedback @joelwurtz. I've updated the PR now. |
"repositories": [ | ||
{ | ||
"type": "git", | ||
"url": "https://github.com/php-fig/cache.git" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just to not forget: we need to remove this before we merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, PSR-6 should be tagged and added to packagist this week.
PSR6 available on packagist :), so just need need a update of the composer and it's good to merge IMO |
👍 |
8815e67
to
b2f3434
Compare
I've updated the PR. I see that StyleCI fails but it not because of my changes. I added #17 to fix the code style of the repo. |
Merged it into master, can you rebase, so that build can run here as well? |
Rebased! |
@Nyholm thanks |
Added a PSR-6 cache plugin
Thank you for all the feedback and comments |
Added a draft on a cache plugin. This can not be merged until PSR6 is final (currently voting).