Skip to content

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

Merged
merged 1 commit into from
Dec 17, 2015
Merged

Added a PSR6 cache plugin #15

merged 1 commit into from
Dec 17, 2015

Conversation

Nyholm
Copy link
Member

@Nyholm Nyholm commented Nov 27, 2015

Added a draft on a cache plugin. This can not be merged until PSR6 is final (currently voting).

  • require-dev psr/cache
  • write unit tests

*/
private function isCacheableResponse(ResponseInterface $response)
{
return floor($response->getStatusCode() / 100) === 2;
Copy link
Contributor

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:

https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/HttpFoundation/Response.php#L521-L529

i think we should respect cache control headers from the server.

@dbu
Copy link
Contributor

dbu commented Nov 27, 2015

looks cool! before we merge, i think you should add the psr cache to require-dev also and add some unit tests.

@Nyholm
Copy link
Member Author

Nyholm commented Nov 27, 2015

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)
Copy link
Member

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)

Copy link
Member Author

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.

Copy link
Contributor

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.

@sagikazarmark
Copy link
Member

Nice one @Nyholm, thanks.

A few more ideas:

  • Cache Control has Shared Max Age as well, although I am not sure we should care about it
  • Do we really need all those oneliners (checks mainly) in separate methods? Can they be reused?
  • Are there any possibility that we will have more options in the future? If yes, I think an options array in the constructor would be better.

@dbu
Copy link
Contributor

dbu commented Nov 27, 2015 via email

@dbu
Copy link
Contributor

dbu commented Nov 27, 2015

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.

@Nyholm
Copy link
Member Author

Nyholm commented Nov 27, 2015

Thank you for the feedback.

I have reduced the number of small one liner functions and improved the getCacheControlDirective.

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?

@sagikazarmark
Copy link
Member

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.

@dbu
Copy link
Contributor

dbu commented Nov 27, 2015

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.

@Nyholm
Copy link
Member Author

Nyholm commented Nov 27, 2015

Okey, I have added some tests and make sure to use an array as second constructor argument to be more extendable in the future.
The tests will fail because of missing dependencies for psr/cache.

@joelwurtz
Copy link
Member

What about etag and receving 304 Not modified ?

@Nyholm
Copy link
Member Author

Nyholm commented Nov 28, 2015

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".

@dbu
Copy link
Contributor

dbu commented Nov 29, 2015

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).

@Nyholm Nyholm changed the title [WIP] Added a PSR6 cache plugin Added a PSR6 cache plugin Dec 7, 2015
@Nyholm
Copy link
Member Author

Nyholm commented Dec 7, 2015

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();
Copy link
Member

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()) ?

Copy link
Member Author

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!

@Nyholm
Copy link
Member Author

Nyholm commented Dec 10, 2015

Thank you for the feedback @joelwurtz. I've updated the PR now.

"repositories": [
{
"type": "git",
"url": "https://github.com/php-fig/cache.git"
Copy link
Contributor

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.

Copy link
Member Author

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.

@joelwurtz
Copy link
Member

PSR6 available on packagist :), so just need need a update of the composer and it's good to merge IMO

@sagikazarmark
Copy link
Member

👍

@Nyholm Nyholm force-pushed the psr6 branch 2 times, most recently from 8815e67 to b2f3434 Compare December 17, 2015 12:02
@Nyholm
Copy link
Member Author

Nyholm commented Dec 17, 2015

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.

@sagikazarmark
Copy link
Member

Merged it into master, can you rebase, so that build can run here as well?

@Nyholm
Copy link
Member Author

Nyholm commented Dec 17, 2015

Rebased!

@sagikazarmark
Copy link
Member

@Nyholm thanks

sagikazarmark added a commit that referenced this pull request Dec 17, 2015
Added a PSR-6 cache plugin
@sagikazarmark sagikazarmark merged commit b165dc3 into php-http:master Dec 17, 2015
@Nyholm
Copy link
Member Author

Nyholm commented Dec 17, 2015

Thank you for all the feedback and comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants