|
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\Cache\Helpers\CacheConfigHelper; |
|
8
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\Exceptions\LaravelCacheConfigNotSetException; |
|
9
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\RequestInfo; |
|
10
|
|
|
use hamburgscleanest\GuzzleAdvancedThrottle\TimeKeeper; |
|
11
|
|
|
use Illuminate\Cache\CacheManager; |
|
12
|
|
|
use Illuminate\Config\Repository; |
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
|
|
15
|
|
|
class LaravelAdapter extends BaseAdapter |
|
16
|
|
|
{ |
|
17
|
|
|
private CacheManager $_cacheManager; |
|
18
|
|
|
|
|
19
|
15 |
|
public function __construct(?Repository $config = null) |
|
20
|
|
|
{ |
|
21
|
15 |
|
if ($config === null || ($cacheConfig = $config->get('cache')) === null) { |
|
22
|
2 |
|
throw new LaravelCacheConfigNotSetException(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
13 |
|
$cacheRepository = new Repository($cacheConfig); |
|
26
|
13 |
|
$this->_cacheManager = CacheConfigHelper::getCacheManager($cacheRepository); |
|
27
|
10 |
|
$this->_ttl = $cacheRepository->get('ttl', self::DEFAULT_TTL); |
|
28
|
10 |
|
$this->_allowEmptyValues = $cacheRepository->get('allow_empty', $this->_allowEmptyValues); |
|
29
|
10 |
|
} |
|
30
|
|
|
|
|
31
|
4 |
|
public function save(string $host, string $key, int $requestCount, TimeKeeper $timeKeeper): void |
|
32
|
|
|
{ |
|
33
|
4 |
|
$expiration = $timeKeeper->getExpiration(); |
|
34
|
4 |
|
if ($expiration === null) { |
|
35
|
|
|
$this->_cacheManager->unset($this->_buildKey($host, $key)); |
|
36
|
|
|
|
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
4 |
|
$remainingSeconds = $timeKeeper->getRemainingSeconds(); |
|
41
|
|
|
|
|
42
|
4 |
|
$this->_cacheManager->put( |
|
43
|
4 |
|
$this->_buildKey($host, $key), |
|
44
|
4 |
|
RequestInfo::create( |
|
45
|
4 |
|
$requestCount, |
|
46
|
4 |
|
$expiration->getTimestamp(), |
|
47
|
4 |
|
$remainingSeconds |
|
48
|
|
|
), |
|
49
|
|
|
$remainingSeconds |
|
50
|
|
|
); |
|
51
|
4 |
|
} |
|
52
|
|
|
|
|
53
|
7 |
|
private function _buildKey(string $host, string $key): string |
|
54
|
|
|
{ |
|
55
|
7 |
|
return $host . '.' . $key; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
7 |
|
public function get(string $host, string $key): ?RequestInfo |
|
59
|
|
|
{ |
|
60
|
|
|
/** @noinspection PhpIncompatibleReturnTypeInspection */ |
|
61
|
7 |
|
return $this->_cacheManager->get($this->_buildKey($host, $key)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
5 |
|
protected function _saveResponse(ResponseInterface $response, string $host, string $path, string $key): void |
|
65
|
|
|
{ |
|
66
|
5 |
|
$this->_cacheManager->put( |
|
67
|
5 |
|
$this->_buildResponseKey($host, $path, $key), |
|
68
|
5 |
|
new CachedResponse($response), |
|
69
|
5 |
|
$this->_ttl |
|
70
|
|
|
); |
|
71
|
5 |
|
} |
|
72
|
|
|
|
|
73
|
9 |
|
private function _buildResponseKey(string $host, string $path, string $key): string |
|
74
|
|
|
{ |
|
75
|
9 |
|
return self::STORAGE_KEY . '.' . $host . '.' . $path . '.' . $key; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
9 |
|
protected function _getResponse(string $host, string $path, string $key): ?Response |
|
79
|
|
|
{ |
|
80
|
|
|
/** @var CachedResponse|null $cachedResponse */ |
|
81
|
9 |
|
$cachedResponse = $this->_cacheManager->get($this->_buildResponseKey($host, $path, $key)); |
|
82
|
|
|
|
|
83
|
9 |
|
return $cachedResponse ? $cachedResponse->getResponse() : null; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|