1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace NiR\CircuitBreaker\Storage; |
6
|
|
|
|
7
|
|
|
use NiR\CircuitBreaker\Exception\StorageFailure; |
8
|
|
|
use NiR\CircuitBreaker\Service\Configuration; |
9
|
|
|
use NiR\CircuitBreaker\Service\Status; |
10
|
|
|
use NiR\CircuitBreaker\Storage; |
11
|
|
|
|
12
|
|
|
class Apcu implements Storage |
13
|
|
|
{ |
14
|
|
|
/** @var string */ |
15
|
|
|
private $keyPrefix; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string $keyPrefix Prefix used for the apcu key of stored statuses. |
19
|
|
|
* Can be useful to mitigate conflicts between multiple projects. |
20
|
|
|
*/ |
21
|
|
|
public function __construct(string $keyPrefix = 'nir.circuit_breaker.') |
22
|
|
|
{ |
23
|
|
|
if (!extension_loaded('apcu')) { |
24
|
|
|
throw new \RuntimeException('You need to install/enable APCu extension if you want to use Apcu storage.'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (php_sapi_name() === 'cli' && ini_get('apc.enable_cli') === '0') { |
28
|
|
|
throw new \RuntimeException('APCu is not enabled in cli environment. You should either enable it ("apc.enable_cli=1" in your php.ini) or use another storage.'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->keyPrefix = $keyPrefix; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function loadStatus(Configuration $config): Status |
35
|
|
|
{ |
36
|
|
|
$key = $this->getItemKey($config); |
37
|
|
|
|
38
|
|
|
if (!apcu_exists($key)) { |
39
|
|
|
return new Status($config); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (false === $fetched = apcu_fetch($key)) { |
43
|
|
|
throw StorageFailure::failedToLoad(__CLASS__, $config->getServiceName()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
list($ttl, $status) = array_values($fetched); |
47
|
|
|
|
48
|
|
|
// If a ttl was stored with the status and it is outdated, the default status (closed breaker) is returned |
49
|
|
View Code Duplication |
if ($ttl !== 0 && $status->getLastUpdate() + $ttl < time()) { |
|
|
|
|
50
|
|
|
$status = new Status($config); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $status; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function saveStatus(Configuration $config, Status $status) |
57
|
|
|
{ |
58
|
|
|
$key = $this->getItemKey($config); |
59
|
|
|
$ttl = $status->isClose() ? $config->getDismissDelay() : 0; |
60
|
|
|
$item = ['ttl' => $ttl, 'status' => $status]; |
61
|
|
|
|
62
|
|
|
if (apcu_store($key, $item, $ttl) === false) { |
63
|
|
|
throw StorageFailure::failedToSave(__CLASS__, $config->getServiceName()); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function getItemKey(Configuration $config): string |
68
|
|
|
{ |
69
|
|
|
return sprintf('%s%s', $this->keyPrefix, $config->getServiceName()); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.