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