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 int */ |
22
|
|
|
private const DEFAULT_TTL = 300; |
23
|
|
|
/** @var string */ |
24
|
|
|
private const STORAGE_KEY = 'requests'; |
25
|
|
|
/** @var string */ |
26
|
|
|
private const RESPONSE_KEY = 'response'; |
27
|
|
|
/** @var string */ |
28
|
|
|
private const EXPIRATION_KEY = 'expires_at'; |
29
|
|
|
/** @var int Time To Live in minutes */ |
30
|
|
|
private $_ttl = self::DEFAULT_TTL; |
31
|
|
|
|
32
|
|
|
/** @var array */ |
33
|
|
|
private $_storage = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* StorageInterface constructor. |
37
|
|
|
* @param Repository|null $config |
38
|
|
|
*/ |
39
|
32 |
|
public function __construct(?Repository $config = null) |
40
|
|
|
{ |
41
|
32 |
|
if ($config === null) |
42
|
|
|
{ |
43
|
31 |
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
$this->_ttl = $config->get('cache.ttl', self::DEFAULT_TTL); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $host |
51
|
|
|
* @param string $key |
52
|
|
|
* @param int $requestCount |
53
|
|
|
* @param \DateTime $expiresAt |
54
|
|
|
* @param int $remainingSeconds |
55
|
|
|
*/ |
56
|
16 |
|
public function save(string $host, string $key, int $requestCount, DateTime $expiresAt, int $remainingSeconds) : void |
57
|
|
|
{ |
58
|
16 |
|
$this->_storage[$host][$key] = RequestInfo::create($requestCount, $expiresAt->getTimestamp(), $remainingSeconds); |
59
|
16 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $host |
63
|
|
|
* @param string $key |
64
|
|
|
* @return RequestInfo|null |
65
|
|
|
*/ |
66
|
24 |
|
public function get(string $host, string $key) : ? RequestInfo |
67
|
|
|
{ |
68
|
24 |
|
return $this->_storage[$host][$key] ?? null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param RequestInterface $request |
73
|
|
|
* @param ResponseInterface $response |
74
|
|
|
* @throws \Exception |
75
|
|
|
*/ |
76
|
6 |
|
public function saveResponse(RequestInterface $request, ResponseInterface $response) : void |
77
|
|
|
{ |
78
|
6 |
|
[$host, $path] = RequestHelper::getHostAndPath($request); |
79
|
|
|
|
80
|
6 |
|
$this->_storage[self::STORAGE_KEY][$host][$path][RequestHelper::getStorageKey($request)] = [ |
81
|
6 |
|
self::RESPONSE_KEY => $response, |
82
|
6 |
|
self::EXPIRATION_KEY => (new DateTime())->add(new DateInterval('PT' . $this->_ttl . 'M'))->getTimestamp() |
83
|
|
|
]; |
84
|
6 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param RequestInterface $request |
88
|
|
|
* @return ResponseInterface|null |
89
|
|
|
*/ |
90
|
8 |
|
public function getResponse(RequestInterface $request) : ? ResponseInterface |
91
|
|
|
{ |
92
|
8 |
|
[$host, $path] = RequestHelper::getHostAndPath($request); |
93
|
8 |
|
$key = RequestHelper::getStorageKey($request); |
94
|
|
|
|
95
|
8 |
|
$response = $this->_storage[self::STORAGE_KEY][$host][$path][$key] ?? null; |
96
|
8 |
|
if ($response !== null) |
97
|
|
|
{ |
98
|
6 |
|
if ($response[self::EXPIRATION_KEY] > \time()) |
99
|
|
|
{ |
100
|
5 |
|
return $response[self::RESPONSE_KEY]; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
$this->_invalidate($host, $path, $key); |
104
|
|
|
} |
105
|
|
|
|
106
|
6 |
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $host |
111
|
|
|
* @param string $path |
112
|
|
|
* @param string $key |
113
|
|
|
*/ |
114
|
1 |
|
private function _invalidate(string $host, string $path, string $key) : void |
115
|
|
|
{ |
116
|
1 |
|
unset($this->_storage[self::STORAGE_KEY][$host][$path][$key]); |
117
|
|
|
} |
118
|
|
|
} |