1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
namespace Paymaxi\Component\CircuitBreaker\Storage\Adapter; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
9
|
|
|
|
10
|
|
|
final class PsrCacheAdapter extends BaseAdapter |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var CacheItemPoolInterface |
14
|
|
|
*/ |
15
|
|
|
private $cacheItemPool; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param CacheItemPoolInterface $cacheItemPool |
19
|
|
|
* @param int $ttl |
20
|
|
|
* @param string $cachePrefix |
21
|
|
|
*/ |
22
|
5 |
|
public function __construct(CacheItemPoolInterface $cacheItemPool, int $ttl = 3600, string $cachePrefix = null) |
23
|
|
|
{ |
24
|
5 |
|
$this->cacheItemPool = $cacheItemPool; |
25
|
5 |
|
parent::__construct($ttl, $cachePrefix); |
26
|
5 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Helper method to make sure that extension is loaded (implementation dependent) |
30
|
|
|
* |
31
|
|
|
* @throws \Paymaxi\Component\CircuitBreaker\Storage\StorageException if extension is not loaded |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
protected function checkExtension() |
35
|
|
|
{ |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Loads item by cache key. |
40
|
|
|
* |
41
|
|
|
* @param string $key |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
* |
45
|
|
|
* @throws \Paymaxi\Component\CircuitBreaker\Storage\StorageException if storage error occurs, handler can not be used |
46
|
|
|
*/ |
47
|
5 |
|
protected function load($key) |
48
|
|
|
{ |
49
|
5 |
|
$cacheItem = $this->cacheItemPool->getItem($key); |
50
|
5 |
|
if ($cacheItem->isHit()) { |
51
|
4 |
|
return $cacheItem->get(); |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
return ""; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Save item in the cache. |
59
|
|
|
* |
60
|
|
|
* @param string $key |
61
|
|
|
* @param string $value |
62
|
|
|
* @param int $ttl |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
* |
66
|
|
|
* @throws \Paymaxi\Component\CircuitBreaker\Storage\StorageException if storage error occurs, handler can not be used |
67
|
|
|
*/ |
68
|
5 |
|
protected function save($key, $value, $ttl) |
69
|
|
|
{ |
70
|
5 |
|
$cacheItem = $this->cacheItemPool->getItem($key); |
71
|
5 |
|
$cacheItem->set($value)->expiresAfter($ttl); |
72
|
5 |
|
$this->cacheItemPool->save($cacheItem); |
73
|
|
|
} |
74
|
|
|
} |