1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmp\Cache\Backend; |
4
|
|
|
|
5
|
|
|
use Cmp\Cache\Cache; |
6
|
|
|
use Cmp\Cache\Exceptions\CacheException; |
7
|
|
|
use Cmp\Cache\Exceptions\ExpiredException; |
8
|
|
|
use Cmp\Cache\Exceptions\NotFoundException; |
9
|
|
|
use Cmp\Cache\FlushableCache; |
10
|
|
|
use Cmp\Cache\PurgableCache; |
11
|
|
|
use Cmp\Cache\Traits\MultiCacheTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ArrayCache |
15
|
|
|
* |
16
|
|
|
* A simple backend powered by an array in memory |
17
|
|
|
* |
18
|
|
|
* @package Cmp\Cache\Infrastureture\Backend |
19
|
|
|
*/ |
20
|
|
|
class ArrayCache extends TaggableCache |
21
|
|
|
{ |
22
|
|
|
use MultiCacheTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Stored items |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $items = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
1 |
|
public function set($key, $value, $timeToLive = null) |
35
|
|
|
{ |
36
|
1 |
|
$this->items[$key] = [ |
37
|
1 |
|
'value' => $value, |
38
|
1 |
|
'expireTime' => $timeToLive === null || $timeToLive === 0 ? null : time() + $timeToLive, |
39
|
|
|
]; |
40
|
|
|
|
41
|
1 |
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
1 |
|
public function has($key) |
48
|
|
|
{ |
49
|
1 |
|
if (!array_key_exists($key, $this->items)) { |
50
|
1 |
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
if ($this->hasExpired($key)) { |
54
|
1 |
|
$this->delete($key); |
55
|
|
|
|
56
|
1 |
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
1 |
|
public function get($key, $default = null) |
66
|
|
|
{ |
67
|
|
|
try { |
68
|
1 |
|
return $this->demand($key); |
69
|
1 |
|
} catch (CacheException $exception) { |
70
|
1 |
|
return $default; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
1 |
|
public function demand($key) |
78
|
|
|
{ |
79
|
1 |
|
if (!array_key_exists($key, $this->items)) { |
80
|
1 |
|
throw new NotFoundException($key); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
if ($this->hasExpired($key)) { |
84
|
1 |
|
$this->delete($key); |
85
|
1 |
|
throw new ExpiredException($key); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
return $this->items[$key]['value']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
1 |
|
public function delete($key) |
95
|
|
|
{ |
96
|
1 |
|
if (array_key_exists($key, $this->items)) { |
97
|
1 |
|
unset($this->items[$key]); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
1 |
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
1 |
|
public function flush() |
107
|
|
|
{ |
108
|
1 |
|
$this->items = []; |
109
|
|
|
|
110
|
1 |
|
return true; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Checks whether an item as expired |
115
|
|
|
* |
116
|
|
|
* @param string $key |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
1 |
|
private function hasExpired($key) |
121
|
|
|
{ |
122
|
1 |
|
return $this->items[$key]['expireTime'] !== null && $this->items[$key]['expireTime'] < time(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Gets the remaining time to live for an item |
127
|
|
|
* |
128
|
|
|
* @param $key |
129
|
|
|
* |
130
|
|
|
* @return int|null |
131
|
|
|
*/ |
132
|
1 |
|
public function getTimeToLive($key) |
133
|
|
|
{ |
134
|
1 |
|
if (!$this->has($key)) { |
135
|
1 |
|
return null; |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
return !$this->items[$key]['expireTime'] ? null : $this->items[$key]['expireTime'] - time(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|