-
Notifications
You must be signed in to change notification settings - Fork 6
[CachePlugin] Streams are not cached #21
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,11 @@ | |
namespace Http\Client\Plugin; | ||
|
||
use Http\Client\Tools\Promise\FulfilledPromise; | ||
use Http\Message\StreamFactory; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Allow for caching a response. | ||
|
@@ -20,33 +22,32 @@ class CachePlugin implements Plugin | |
private $pool; | ||
|
||
/** | ||
* Default time to store object in cache. This value is used if CachePlugin::respectCacheHeaders is false or | ||
* if cache headers are missing. | ||
* | ||
* @var int | ||
* @var StreamFactory | ||
*/ | ||
private $defaultTtl; | ||
private $streamFactory; | ||
|
||
/** | ||
* Look at the cache headers to know whether this response may be cached and to | ||
* decide how it can be cached. | ||
* | ||
* @var bool Defaults to true | ||
* @var array | ||
*/ | ||
private $respectCacheHeaders; | ||
private $config; | ||
|
||
/** | ||
* Available options are | ||
* - respect_cache_headers: Whether to look at the cache directives or ignore them. | ||
* | ||
* - default_ttl: If we do not respect cache headers or can't calculate a good ttl, use this value. | ||
* | ||
* @param CacheItemPoolInterface $pool | ||
* @param array $options | ||
* @param StreamFactory $streamFactory | ||
* @param array $config | ||
*/ | ||
public function __construct(CacheItemPoolInterface $pool, array $options = []) | ||
public function __construct(CacheItemPoolInterface $pool, StreamFactory $streamFactory, array $config = []) | ||
{ | ||
$this->pool = $pool; | ||
$this->defaultTtl = isset($options['default_ttl']) ? $options['default_ttl'] : null; | ||
$this->respectCacheHeaders = isset($options['respect_cache_headers']) ? $options['respect_cache_headers'] : true; | ||
$this->streamFactory = $streamFactory; | ||
|
||
$optionsResolver = new OptionsResolver(); | ||
$this->configureOptions($optionsResolver); | ||
$this->config = $optionsResolver->resolve($config); | ||
} | ||
|
||
/** | ||
|
@@ -67,12 +68,16 @@ public function handleRequest(RequestInterface $request, callable $next, callabl | |
|
||
if ($cacheItem->isHit()) { | ||
// return cached response | ||
return new FulfilledPromise($cacheItem->get()); | ||
$data = $cacheItem->get(); | ||
$response = $data['response']; | ||
$response = $response->withBody($this->streamFactory->createStream($data['body'])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does it make sense that the body is always a stream? should we keep what it was when we had a cache miss? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, The body has to be a stream, always. That is part of PSR-7. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I think it can be null as well, can't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, I might be wrong. But There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right. Initially, it was the plan to return null when the body is empty. All these PSR7 versions in my head sometimes make me wonder about the REAL interfaces. |
||
|
||
return new FulfilledPromise($response); | ||
} | ||
|
||
return $next($request)->then(function (ResponseInterface $response) use ($cacheItem) { | ||
if ($this->isCacheable($response)) { | ||
$cacheItem->set($response) | ||
$cacheItem->set(['response' => $response, 'body' => $response->getBody()->__toString()]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to have some sort of buffered stream, as some implementations provide a unbuffered stream, so reading a stream once will certainly make it not available afterwards. Also even if the stream is buffered by the implementation, it must be rewind here. Concretly we must add something like, if stream is seekable just rewind this, if not buffer it into a new stream. We may face memory problem if big file are downloaded, but don't know if we can avoid that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what happens with a stream body when setting the cache? what happens without a stream body? duplicated data in cache? maybe we could check if the body is a stream and if so set it to the cache item, otherwise only set the response? and when restoring check if we have a body in the cache and if we do create the stream from that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may face memory problem if big file are downloaded, but don't know if
we can avoid that
sounds like a warning for the cache plugin doc: if you handle large
downloads, make sure the implementation works as expected. or contribute
to make it better.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the comments.
Hm, okey. I think I understand what you mean.. Give me a minute and I'll update
Agreed. |
||
->expiresAfter($this->getMaxAge($response)); | ||
$this->pool->save($cacheItem); | ||
} | ||
|
@@ -93,7 +98,7 @@ protected function isCacheable(ResponseInterface $response) | |
if (!in_array($response->getStatusCode(), [200, 203, 300, 301, 302, 404, 410])) { | ||
return false; | ||
} | ||
if (!$this->respectCacheHeaders) { | ||
if (!$this->config['respect_cache_headers']) { | ||
return true; | ||
} | ||
if ($this->getCacheControlDirective($response, 'no-store') || $this->getCacheControlDirective($response, 'private')) { | ||
|
@@ -148,8 +153,8 @@ private function createCacheKey(RequestInterface $request) | |
*/ | ||
private function getMaxAge(ResponseInterface $response) | ||
{ | ||
if (!$this->respectCacheHeaders) { | ||
return $this->defaultTtl; | ||
if (!$this->config['respect_cache_headers']) { | ||
return $this->config['default_ttl']; | ||
} | ||
|
||
// check for max age in the Cache-Control header | ||
|
@@ -169,6 +174,24 @@ private function getMaxAge(ResponseInterface $response) | |
return (new \DateTime($header))->getTimestamp() - (new \DateTime())->getTimestamp(); | ||
} | ||
|
||
return $this->defaultTtl; | ||
return $this->config['default_ttl']; | ||
} | ||
|
||
/** | ||
* Configure an options resolver. | ||
* | ||
* @param OptionsResolver $resolver | ||
* | ||
* @return array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does not return anything. |
||
*/ | ||
private function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults([ | ||
'default_ttl' => null, | ||
'respect_cache_headers' => true, | ||
]); | ||
|
||
$resolver->setAllowedTypes('default_ttl', ['int', 'null']); | ||
$resolver->setAllowedTypes('respect_cache_headers', 'bool'); | ||
} | ||
} |
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.
What's the problem with options? 😛
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.
None, but SocketHttpClient was using
$config
so I thought I would be consistent.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.
Okay, makes sense