|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters; |
|
4
|
|
|
|
|
5
|
|
|
use DateInterval; |
|
6
|
|
|
use DateTime; |
|
7
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Helpers\RequestHelper; |
|
8
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\Interfaces\StorageInterface; |
|
9
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\RequestInfo; |
|
10
|
|
|
use Psr\Http\Message\RequestInterface; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ArrayAdapter |
|
15
|
|
|
* @package hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters |
|
16
|
|
|
*/ |
|
17
|
|
|
class ArrayAdapter implements StorageInterface |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private const STORAGE_KEY = 'requests'; |
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private const RESPONSE_KEY = 'response'; |
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
private const EXPIRATION_KEY = 'expires_at'; |
|
26
|
|
|
|
|
27
|
|
|
/** @var array */ |
|
28
|
|
|
private $_storage = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $host |
|
32
|
|
|
* @param string $key |
|
33
|
|
|
* @param int $requestCount |
|
34
|
|
|
* @param \DateTime $expiresAt |
|
35
|
|
|
* @param int $remainingSeconds |
|
36
|
|
|
*/ |
|
37
|
12 |
|
public function save(string $host, string $key, int $requestCount, DateTime $expiresAt, int $remainingSeconds) : void |
|
38
|
|
|
{ |
|
39
|
12 |
|
$this->_storage[$host][$key] = RequestInfo::create($requestCount, $expiresAt->getTimestamp(), $remainingSeconds); |
|
40
|
12 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $host |
|
44
|
|
|
* @param string $key |
|
45
|
|
|
* @return RequestInfo|null |
|
46
|
|
|
*/ |
|
47
|
19 |
|
public function get(string $host, string $key) : ? RequestInfo |
|
48
|
|
|
{ |
|
49
|
19 |
|
return $this->_storage[$host][$key] ?? null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param RequestInterface $request |
|
54
|
|
|
* @param ResponseInterface $response |
|
55
|
|
|
* @param int $duration |
|
56
|
|
|
* @throws \Exception |
|
57
|
|
|
*/ |
|
58
|
4 |
|
public function saveResponse(RequestInterface $request, ResponseInterface $response, int $duration = 300) : void |
|
59
|
|
|
{ |
|
60
|
4 |
|
[$host, $path] = RequestHelper::getHostAndPath($request); |
|
61
|
|
|
|
|
62
|
4 |
|
$this->_storage[self::STORAGE_KEY][$host][$path] = [ |
|
63
|
4 |
|
self::RESPONSE_KEY => $response, |
|
64
|
4 |
|
self::EXPIRATION_KEY => (new DateTime())->add(new DateInterval('PT' . $duration . 'M'))->getTimestamp() |
|
65
|
|
|
]; |
|
66
|
4 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param RequestInterface $request |
|
70
|
|
|
* @return ResponseInterface|null |
|
71
|
|
|
*/ |
|
72
|
6 |
|
public function getResponse(RequestInterface $request) : ? ResponseInterface |
|
73
|
|
|
{ |
|
74
|
6 |
|
[$host, $path] = RequestHelper::getHostAndPath($request); |
|
75
|
|
|
|
|
76
|
6 |
|
$response = $this->_storage[self::STORAGE_KEY][$host][$path] ?? null; |
|
77
|
6 |
|
if ($response !== null) |
|
78
|
|
|
{ |
|
79
|
4 |
|
if ($response[self::EXPIRATION_KEY] > \time()) |
|
80
|
|
|
{ |
|
81
|
3 |
|
return $response[self::RESPONSE_KEY]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
$this->_invalidate($host, $path); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
4 |
|
return null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $host |
|
92
|
|
|
* @param string $path |
|
93
|
|
|
*/ |
|
94
|
1 |
|
private function _invalidate(string $host, string $path) |
|
95
|
|
|
{ |
|
96
|
1 |
|
unset($this->_storage[self::STORAGE_KEY][$host][$path]); |
|
97
|
|
|
} |
|
98
|
|
|
} |