Skip to content

[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

Merged
merged 4 commits into from
Dec 25, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"require": {
"php": ">=5.4",
"php-http/httplug": "1.0.0-beta",
"php-http/client-tools": "^0.1@dev"
"php-http/client-tools": "^0.1@dev",
"php-http/message-factory": "^1.0",
"symfony/options-resolver": "^2.6|^3.0"
},
"require-dev": {
"phpspec/phpspec": "^2.4-alpha",
Expand Down
22 changes: 16 additions & 6 deletions spec/CachePluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
namespace spec\Http\Client\Plugin;

use Http\Client\Tools\Promise\FulfilledPromise;
use Http\Message\StreamFactory;
use PhpSpec\ObjectBehavior;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class CachePluginSpec extends ObjectBehavior
{
function let(CacheItemPoolInterface $pool)
function let(CacheItemPoolInterface $pool, StreamFactory $streamFactory)
{
$this->beConstructedWith($pool, ['default_ttl'=>60]);
$this->beConstructedWith($pool, $streamFactory, ['default_ttl'=>60]);
}

function it_is_initializable(CacheItemPoolInterface $pool)
Expand All @@ -26,17 +28,21 @@ function it_is_a_plugin()
$this->shouldImplement('Http\Client\Plugin\Plugin');
}

function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response)
function it_caches_responses(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream)
{
$httpBody = 'body';
$stream->__toString()->willReturn($httpBody);

$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn('/');
$response->getStatusCode()->willReturn(200);
$response->getBody()->willReturn($stream);
$response->getHeader('Cache-Control')->willReturn(array());
$response->getHeader('Expires')->willReturn(array());

$pool->getItem('e3b717d5883a45ef9493d009741f7c64')->shouldBeCalled()->willReturn($item);
$item->isHit()->willReturn(false);
$item->set($response)->willReturn($item)->shouldBeCalled();
$item->set(['response' => $response, 'body' => $httpBody])->willReturn($item)->shouldBeCalled();
$item->expiresAfter(60)->willReturn($item)->shouldBeCalled();
$pool->save($item)->shouldBeCalled();

Expand Down Expand Up @@ -78,11 +84,15 @@ function it_doesnt_store_post_requests(CacheItemPoolInterface $pool, CacheItemIn
}


function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response)
function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, ResponseInterface $response, StreamInterface $stream)
{
$httpBody = 'body';
$stream->__toString()->willReturn($httpBody);

$request->getMethod()->willReturn('GET');
$request->getUri()->willReturn('/');
$response->getStatusCode()->willReturn(200);
$response->getBody()->willReturn($stream);
$response->getHeader('Cache-Control')->willReturn(array('max-age=40'));
$response->getHeader('Age')->willReturn(array('15'));
$response->getHeader('Expires')->willReturn(array());
Expand All @@ -91,7 +101,7 @@ function it_calculate_age_from_response(CacheItemPoolInterface $pool, CacheItemI
$item->isHit()->willReturn(false);

// 40-15 should be 25
$item->set($response)->willReturn($item)->shouldBeCalled();
$item->set(['response' => $response, 'body' => $httpBody])->willReturn($item)->shouldBeCalled();
$item->expiresAfter(25)->willReturn($item)->shouldBeCalled();
$pool->save($item)->shouldBeCalled();

Expand Down
65 changes: 44 additions & 21 deletions src/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = [])
Copy link
Member

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? 😛

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

Okay, makes sense

{
$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);
}

/**
Expand All @@ -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']));
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

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, The body has to be a stream, always. That is part of PSR-7.

Copy link
Member

Choose a reason for hiding this comment

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

Actually, I think it can be null as well, can't it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, I might be wrong. But getBody should always return a stream.

Copy link
Member

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

@dbu dbu Dec 21, 2015 via email

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the comments.

Concretly we must add something like, if stream is seekable just rewind this, if not buffer it into a new stream.

Hm, okey. I think I understand what you mean.. Give me a minute and I'll update

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.

Agreed.

->expiresAfter($this->getMaxAge($response));
$this->pool->save($cacheItem);
}
Expand All @@ -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')) {
Expand Down Expand Up @@ -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
Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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');
}
}