1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmp\Cache\Infrastructure\Backend; |
4
|
|
|
|
5
|
|
|
use Cmp\Cache\Domain\Cache; |
6
|
|
|
use Cmp\Cache\Domain\Exceptions\CacheException; |
7
|
|
|
use Cmp\Cache\Domain\Exceptions\ExpiredException; |
8
|
|
|
use Cmp\Cache\Domain\Exceptions\NotFoundException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ArrayCache |
12
|
|
|
* |
13
|
|
|
* A simple backend powered by an array in memory |
14
|
|
|
* |
15
|
|
|
* @package Cmp\Cache\Infrastureture\Backend |
16
|
|
|
*/ |
17
|
|
|
class ArrayCache implements Cache |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Stored items |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $items = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function delete($key) |
30
|
|
|
{ |
31
|
|
|
if (array_key_exists($key, $this->items)) { |
32
|
|
|
unset($this->items[$key]); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function set($key, $value, $timeToLive = 0) |
40
|
|
|
{ |
41
|
|
|
$this->items[$key] = [ |
42
|
|
|
'value' => $value, |
43
|
|
|
'expireTime' => $timeToLive === 0 ? $timeToLive : time() + $timeToLive, |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function has($key) |
51
|
|
|
{ |
52
|
|
|
if (!array_key_exists($key, $this->items)) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if ($this->hasExpired($key)) { |
57
|
|
|
return (bool) $this->delete($key); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function get($key) |
67
|
|
|
{ |
68
|
|
|
if (!array_key_exists($key, $this->items)) { |
69
|
|
|
throw new NotFoundException($key, static::class); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($this->hasExpired($key)) { |
73
|
|
|
$this->delete($key); |
74
|
|
|
throw new ExpiredException($key, static::class); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function pull($key, $default = null) |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
|
|
return $this->get($key); |
87
|
|
|
} catch (CacheException $exception) { |
88
|
|
|
return $default; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
private function hasExpired($key) |
96
|
|
|
{ |
97
|
|
|
return $this->items[$key]['expireTime'] !== 0 && $this->items[$key]['expireTime'] < time(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.