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