Skip to content

Commit

Permalink
Merge pull request #14 from EAnushan/master
Browse files Browse the repository at this point in the history
Fix caching streams and cache lifetimes (WIP).
  • Loading branch information
Kevinrob committed Jul 20, 2015
2 parents 0b14ffa + 921e22b commit fccedf6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/CacheEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public function __sleep()
// Stream/Resource can't be serialized... So we copy the content
if ($this->response !== null) {
$this->responseBody = (string)$this->response->getBody();
$this->response->getBody()->rewind();
}

return array_keys(get_object_vars($this));
Expand Down
11 changes: 10 additions & 1 deletion src/PrivateCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,18 @@ public function fetch(RequestInterface $request)
public function cache(RequestInterface $request, ResponseInterface $response)
{
try {
return $this->storage->save($this->getCacheKey($request), serialize($this->getCacheObject($response)));
$cacheObject = $this->getCacheObject($response);
if(isset($cacheObject))
{
$lifeTime = $this->getCacheObject($response)->getStaleAt()->getTimestamp() - time();
if($lifeTime > 0) {
return $this->storage->save($this->getCacheKey($request), serialize($this->getCacheObject($response)), $lifeTime);
}
}
} catch (\Exception $ignored) {
return false;
}

return false;
}
}

0 comments on commit fccedf6

Please sign in to comment.