1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soupmix\Cache; |
4
|
|
|
|
5
|
|
|
use Soupmix\Cache\Exceptions\InvalidArgumentException; |
6
|
|
|
use Psr\SimpleCache\CacheInterface; |
7
|
|
|
use DateInterval; |
8
|
|
|
use DateTime; |
9
|
|
|
|
10
|
|
|
class APCUCache implements CacheInterface |
11
|
|
|
{ |
12
|
|
|
const PSR16_RESERVED_CHARACTERS = ['{','}','(',')','/','@',':']; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* {@inheritDoc} |
16
|
|
|
*/ |
17
|
1 |
|
public function get($key, $default = null) |
18
|
|
|
{ |
19
|
1 |
|
$this->checkReservedCharacters($key); |
20
|
1 |
|
$value = apcu_fetch($key); |
21
|
1 |
|
return $value ?: $default; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritDoc} |
26
|
|
|
*/ |
27
|
5 |
|
public function set($key, $value, $ttl = null) |
28
|
|
|
{ |
29
|
5 |
|
$this->checkReservedCharacters($key); |
30
|
3 |
|
if ($ttl instanceof DateInterval) { |
31
|
1 |
|
$ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
32
|
|
|
} |
33
|
3 |
|
return apcu_store($key, $value, (int) $ttl); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
*/ |
39
|
1 |
|
public function delete($key) |
40
|
|
|
{ |
41
|
1 |
|
$this->checkReservedCharacters($key); |
42
|
1 |
|
return (bool) apcu_delete($key); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
1 |
|
public function clear() |
49
|
|
|
{ |
50
|
1 |
|
return apcu_clear_cache(); |
51
|
|
|
} |
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function getMultiple($keys, $default = null) |
56
|
|
|
{ |
57
|
1 |
|
$defaults = array_fill(0, count($keys), $default); |
58
|
1 |
|
foreach ($keys as $key) { |
59
|
1 |
|
$this->checkReservedCharacters($key); |
60
|
|
|
} |
61
|
1 |
|
return array_merge(apcu_fetch($keys), $defaults); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
1 |
|
public function setMultiple($values, $ttl = null) |
68
|
|
|
{ |
69
|
1 |
|
foreach ($values as $key => $value) { |
70
|
1 |
|
$this->checkReservedCharacters($key); |
71
|
|
|
} |
72
|
1 |
|
if ($ttl instanceof DateInterval) { |
73
|
1 |
|
$ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
74
|
|
|
} |
75
|
1 |
|
$result = apcu_store($values, null, $ttl); |
76
|
1 |
|
return empty($result); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritDoc} |
81
|
|
|
*/ |
82
|
1 |
|
public function deleteMultiple($keys) |
83
|
|
|
{ |
84
|
1 |
|
$ret = []; |
85
|
1 |
|
foreach ($keys as $key) { |
86
|
1 |
|
$this->checkReservedCharacters($key); |
87
|
1 |
|
$ret[$key] = apcu_delete($key); |
88
|
|
|
} |
89
|
1 |
|
return $ret; |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
public function increment($key, $step = 1) |
93
|
|
|
{ |
94
|
1 |
|
$this->checkReservedCharacters($key); |
95
|
1 |
|
return apcu_inc($key, $step); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
public function decrement($key, $step = 1) |
99
|
|
|
{ |
100
|
1 |
|
$this->checkReservedCharacters($key); |
101
|
1 |
|
return apcu_dec($key, $step); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritDoc} |
106
|
|
|
*/ |
107
|
1 |
|
public function has($key) |
108
|
|
|
{ |
109
|
1 |
|
$this->checkReservedCharacters($key); |
110
|
1 |
|
return apcu_exists($key); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
6 |
|
private function checkReservedCharacters($key) |
114
|
|
|
{ |
115
|
6 |
|
if (!is_string($key)) { |
116
|
1 |
|
$message = sprintf('key %s is not a string.', $key); |
117
|
1 |
|
throw new InvalidArgumentException($message); |
118
|
|
|
} |
119
|
5 |
|
foreach (self::PSR16_RESERVED_CHARACTERS as $needle) { |
120
|
5 |
|
if (strpos($key, $needle) !== false) { |
121
|
1 |
|
$message = sprintf('%s string is not a legal value.', $key); |
122
|
1 |
|
throw new InvalidArgumentException($message); |
123
|
|
|
} |
124
|
|
|
} |
125
|
4 |
|
} |
126
|
|
|
} |
127
|
|
|
|