|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hamburgscleanest\GuzzleAdvancedThrottle\Cache\Adapters; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Response; |
|
6
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Cache\CachedResponse; |
|
7
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\RequestInfo; |
|
8
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\SystemClock; |
|
9
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\TimeKeeper; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Illuminate\Config\Repository; |
|
12
|
|
|
|
|
13
|
|
|
class ArrayAdapter extends BaseAdapter |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
private const RESPONSE_KEY = 'response'; |
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
private const EXPIRATION_KEY = 'expires_at'; |
|
19
|
|
|
|
|
20
|
|
|
private array $_storage = []; |
|
21
|
|
|
|
|
22
|
38 |
|
public function __construct(?Repository $config = null) |
|
23
|
|
|
{ |
|
24
|
38 |
|
if ($config === null) { |
|
25
|
35 |
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
3 |
|
$this->_ttl = $config->get('cache.ttl', self::DEFAULT_TTL); |
|
29
|
3 |
|
$this->_allowEmptyValues = $config->get('cache.allow_empty', $this->_allowEmptyValues); |
|
30
|
3 |
|
} |
|
31
|
|
|
|
|
32
|
20 |
|
public function save(string $host, string $key, int $requestCount, TimeKeeper $timeKeeper): void |
|
33
|
|
|
{ |
|
34
|
20 |
|
$expiration = $timeKeeper->getExpiration(); |
|
35
|
20 |
|
if ($expiration === null) { |
|
36
|
2 |
|
unset($this->_storage[$host][$key]); |
|
37
|
2 |
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
19 |
|
$this->_storage[$host][$key] = RequestInfo::create( |
|
41
|
19 |
|
$requestCount, |
|
42
|
19 |
|
$expiration->getTimestamp(), |
|
43
|
19 |
|
$timeKeeper->getRemainingSeconds() |
|
44
|
|
|
); |
|
45
|
19 |
|
} |
|
46
|
|
|
|
|
47
|
28 |
|
public function get(string $host, string $key): ?RequestInfo |
|
48
|
|
|
{ |
|
49
|
28 |
|
return $this->_storage[$host][$key] ?? null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
9 |
|
protected function _saveResponse(ResponseInterface $response, string $host, string $path, string $key): void |
|
53
|
|
|
{ |
|
54
|
9 |
|
$this->_storage[self::STORAGE_KEY][$host][$path][$key] = [ |
|
55
|
9 |
|
self::RESPONSE_KEY => new CachedResponse($response), |
|
56
|
9 |
|
self::EXPIRATION_KEY => SystemClock::create()->advanceMinutes($this->_ttl)->now()->getTimestamp() |
|
57
|
|
|
]; |
|
58
|
9 |
|
} |
|
59
|
|
|
|
|
60
|
12 |
|
protected function _getResponse(string $host, string $path, string $key): ?Response |
|
61
|
|
|
{ |
|
62
|
12 |
|
$response = $this->_storage[self::STORAGE_KEY][$host][$path][$key] ?? null; |
|
63
|
|
|
|
|
64
|
12 |
|
if ($response !== null) { |
|
65
|
8 |
|
if ($response[self::EXPIRATION_KEY] > \time()) { |
|
66
|
|
|
/** @var CachedResponse|null $cachedResponse */ |
|
67
|
7 |
|
$cachedResponse = $response[self::RESPONSE_KEY]; |
|
68
|
|
|
|
|
69
|
7 |
|
return $cachedResponse ? $cachedResponse->getResponse() : null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
unset($this->_storage[self::STORAGE_KEY][$host][$path][$key]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
8 |
|
return null; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|