1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Api; |
4
|
|
|
|
5
|
|
|
use DominionEnterprises\Util; |
6
|
|
|
use DominionEnterprises\Util\Arrays; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class to cache API results in a Redis store. |
10
|
|
|
*/ |
11
|
|
|
final class PredisCache implements Cache |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Predis client for storing cache. |
15
|
|
|
* |
16
|
|
|
* @var \Predis\Client |
17
|
|
|
*/ |
18
|
|
|
private $client; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Construct a cache instance. |
22
|
|
|
* |
23
|
|
|
* @param \Predis\Client $client The predis client to send data to. |
24
|
|
|
*/ |
25
|
|
|
public function __construct(\Predis\Client $client) |
26
|
|
|
{ |
27
|
|
|
$this->client = $client; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @see Cache::set() |
32
|
|
|
*/ |
33
|
|
|
public function set(Request $request, Response $response, $expires = null) |
34
|
|
|
{ |
35
|
|
View Code Duplication |
if ($expires === null) { |
|
|
|
|
36
|
|
|
$expiresHeader = null; |
37
|
|
|
if (!Arrays::tryGet($response->getResponseHeaders(), 'Expires', $expiresHeader)) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$expires = Util::ensureNot( |
42
|
|
|
false, |
43
|
|
|
strtotime($expiresHeader[0]), |
44
|
|
|
"Unable to parse Expires value of '{$expiresHeader[0]}'" |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$key = self::getKey($request); |
49
|
|
|
$this->client->set( |
50
|
|
|
$key, |
51
|
|
|
json_encode( |
52
|
|
|
[ |
53
|
|
|
'httpCode' => $response->getHttpCode(), |
54
|
|
|
'headers' => $response->getResponseHeaders(), |
55
|
|
|
'body' => $response->getResponse(), |
56
|
|
|
] |
57
|
|
|
) |
58
|
|
|
); |
59
|
|
|
$this->client->expireat($key, $expires); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @see Cache::get() |
64
|
|
|
*/ |
65
|
|
|
public function get(Request $request) |
66
|
|
|
{ |
67
|
|
|
$cached = $this->client->get(self::getKey($request)); |
68
|
|
|
if ($cached !== null) { |
69
|
|
|
$data = json_decode($cached, true); |
70
|
|
|
return new Response($data['httpCode'], $data['headers'], $data['body']); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Helper method to get a unique key for an API request. |
78
|
|
|
* |
79
|
|
|
* This generator does not use the request headers so there is a chance for conflicts |
80
|
|
|
* |
81
|
|
|
* @param Request $request The request from which to generate an unique key |
82
|
|
|
* |
83
|
|
|
* @return string the unique identifier |
84
|
|
|
*/ |
85
|
|
|
private static function getKey(Request $request) |
86
|
|
|
{ |
87
|
|
|
return "{$request->getUrl()}:{$request->getBody()}"; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.