|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chadicus\Marvel\Api\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Chadicus\Marvel\Api\Response; |
|
6
|
|
|
use Chadicus\Marvel\Api\ResponseInterface; |
|
7
|
|
|
use Chadicus\Marvel\Api\RequestInterface; |
|
8
|
|
|
use MongoDB\BSON\UTCDateTime; |
|
9
|
|
|
use MongoDB\Collection; |
|
10
|
|
|
use MongoDB\Model\BSONArray; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Concrete implementation of Cache using an array. |
|
14
|
|
|
*/ |
|
15
|
|
|
final class MongoCache extends AbstractCache implements CacheInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* MongoCollection containing the cached responses. |
|
19
|
|
|
* |
|
20
|
|
|
* @var \MongoCollection |
|
21
|
|
|
*/ |
|
22
|
|
|
private $collection; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Construct a new instance of MongoCache. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Collection $collection The collection containing the cached data. |
|
28
|
|
|
* @param integer $defaultTimeToLive The default time to live in seconds. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(Collection $collection, $defaultTimeToLive = CacheInterface::MAX_TTL) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->setDefaultTTL($defaultTimeToLive); |
|
33
|
|
|
$this->collection = $collection; |
|
|
|
|
|
|
34
|
|
|
$this->collection->createIndex(['expires' => 1], ['expireAfterSeconds' => 0, 'background' => true]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Store the api $response as the cached result of the api $request. |
|
39
|
|
|
* |
|
40
|
|
|
* @param RequestInterface $request The request for which the response will be cached. |
|
41
|
|
|
* @param ResponseInterface $response The reponse to cache. |
|
42
|
|
|
* @param integer $timeToLive The time in seconds that the cache should live. |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
* |
|
46
|
|
|
* @throws \InvalidArgumentException Throw if $timeToLive is not an integer between 0 and 86400. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function set(RequestInterface $request, ResponseInterface $response, $timeToLive = null) |
|
49
|
|
|
{ |
|
50
|
|
|
$timeToLive = self::ensureTTL($timeToLive ?: $this->getDefaultTTL()); |
|
51
|
|
|
|
|
52
|
|
|
$id = $request->getUrl(); |
|
53
|
|
|
$cache = [ |
|
54
|
|
|
'httpCode' => $response->getHttpCode(), |
|
55
|
|
|
'body' => $response->getBody(), |
|
56
|
|
|
'headers' => $response->getHeaders(), |
|
57
|
|
|
'expires' => new UTCDateTime(time() + $timeToLive), |
|
58
|
|
|
]; |
|
59
|
|
|
|
|
60
|
|
|
$this->collection->updateOne(['_id' => $id], ['$set' => $cache], ['upsert' => true]); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Retrieve the cached results of the api $request. |
|
65
|
|
|
* |
|
66
|
|
|
* @param RequestInterface $request A request for which the response may be cached. |
|
67
|
|
|
* |
|
68
|
|
|
* @return ResponseInterface|null |
|
69
|
|
|
*/ |
|
70
|
|
|
public function get(RequestInterface $request) |
|
71
|
|
|
{ |
|
72
|
|
|
$cached = $this->collection->findOne(['_id' => $request->getUrl()]); |
|
73
|
|
|
if ($cached === null) { |
|
74
|
|
|
return null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$headers = $cached['headers']; |
|
78
|
|
|
if ($headers instanceof BSONArray) { |
|
79
|
|
|
$headers = $headers->getArrayCopy(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$body = $cached['body']; |
|
83
|
|
|
if ($body instanceof BSONArray) { |
|
84
|
|
|
$body = $body->getArrayCopy(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return new Response($cached['httpCode'], $headers, $body); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..