Total Complexity | 12 |
Total Lines | 99 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 8 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
12 | class ArrayCache extends CacheProvider |
||
13 | { |
||
14 | /** @var array[] $data each element being a tuple of [$data, $expiration], where the expiration is int|bool */ |
||
15 | private $data = []; |
||
16 | |||
17 | /** @var int */ |
||
18 | private $hitsCount = 0; |
||
19 | |||
20 | /** @var int */ |
||
21 | private $missesCount = 0; |
||
22 | |||
23 | /** @var int */ |
||
24 | private $upTime; |
||
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | 160 | public function __construct() |
|
32 | 160 | } |
|
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | 145 | protected function doFetch($id) |
|
38 | { |
||
39 | 145 | if (! $this->doContains($id)) { |
|
40 | 81 | $this->missesCount += 1; |
|
41 | |||
42 | 81 | return false; |
|
43 | } |
||
44 | |||
45 | 134 | $this->hitsCount += 1; |
|
46 | |||
47 | 134 | return $this->data[$id][0]; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 156 | protected function doContains($id) |
|
54 | { |
||
55 | 156 | if (! isset($this->data[$id])) { |
|
56 | 156 | return false; |
|
57 | } |
||
58 | |||
59 | 144 | $expiration = $this->data[$id][1]; |
|
60 | |||
61 | 144 | if ($expiration && $expiration < time()) { |
|
62 | 2 | $this->doDelete($id); |
|
63 | |||
64 | 2 | return false; |
|
65 | } |
||
66 | |||
67 | 144 | return true; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | 152 | protected function doSave($id, $data, $lifeTime = 0) |
|
74 | { |
||
75 | 152 | $this->data[$id] = [$data, $lifeTime ? time() + $lifeTime : false]; |
|
76 | |||
77 | 152 | return true; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 93 | protected function doDelete($id) |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 2 | protected function doFlush() |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 2 | protected function doGetStats() |
|
111 | ]; |
||
112 | } |
||
113 | } |
||
114 |