|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cmp\Cache\Infrastructure; |
|
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 |
|
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); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($this->hasExpired($key)) { |
|
73
|
|
|
$this->delete($key); |
|
74
|
|
|
throw new ExpiredException($key); |
|
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
|
|
|
public function flush() |
|
96
|
|
|
{ |
|
97
|
|
|
$this->items = []; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Checks whether an item as expired |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $key |
|
104
|
|
|
* |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
|
|
private function hasExpired($key) |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->items[$key]['expireTime'] !== 0 && $this->items[$key]['expireTime'] < time(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|