1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soupmix\Cache; |
4
|
|
|
|
5
|
|
|
use Soupmix\Cache\Exceptions\InvalidArgumentException; |
6
|
|
|
use Psr\SimpleCache\CacheInterface; |
7
|
|
|
use Memcached; |
8
|
|
|
use DateInterval; |
9
|
|
|
use DateTime; |
10
|
|
|
|
11
|
|
|
class MemcachedCache implements CacheInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
const PSR16_RESERVED_CHARACTERS = ['{','}','(',')','/','@',':']; |
15
|
|
|
|
16
|
|
|
public $handler; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Connect to Memcached service |
20
|
|
|
* |
21
|
|
|
* @param Memcached $handler Memcached handler object |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
7 |
|
public function __construct(Memcached $handler) |
25
|
|
|
{ |
26
|
7 |
|
$this->handler = $handler; |
27
|
7 |
|
if (defined('Memcached::HAVE_IGBINARY') && extension_loaded('igbinary')) { |
28
|
7 |
|
ini_set('memcached.serializer', 'igbinary'); |
29
|
|
|
} |
30
|
7 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
*/ |
35
|
1 |
|
public function get($key, $default = null) |
36
|
|
|
{ |
37
|
|
|
|
38
|
1 |
|
$this->checkReservedCharacters($key); |
39
|
1 |
|
$value = $this->handler->get($key); |
40
|
1 |
|
return $value ?: $default; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritDoc} |
45
|
|
|
*/ |
46
|
5 |
View Code Duplication |
public function set($key, $value, $ttl = null) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
|
49
|
5 |
|
$this->checkReservedCharacters($key); |
50
|
3 |
|
if ($ttl instanceof DateInterval) { |
51
|
1 |
|
$ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
52
|
|
|
} |
53
|
3 |
|
return $this->handler->set($key, $value, (int) $ttl); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
1 |
|
public function delete($key) |
60
|
|
|
{ |
61
|
|
|
|
62
|
1 |
|
$this->checkReservedCharacters($key); |
63
|
1 |
|
return (bool) $this->handler->delete($key); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritDoc} |
68
|
|
|
*/ |
69
|
7 |
|
public function clear() |
70
|
|
|
{ |
71
|
7 |
|
return $this->handler->flush(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
*/ |
77
|
1 |
|
public function getMultiple($keys, $default = null) |
78
|
|
|
{ |
79
|
1 |
|
$defaults = array_fill(0, count($keys), $default); |
80
|
1 |
|
foreach ($keys as $key) { |
81
|
1 |
|
$this->checkReservedCharacters($key); |
82
|
|
|
} |
83
|
1 |
|
return array_merge($this->handler->getMulti($keys), $defaults); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
*/ |
89
|
1 |
View Code Duplication |
public function setMultiple($values, $ttl = null) |
|
|
|
|
90
|
|
|
{ |
91
|
1 |
|
foreach ($values as $key => $value) { |
92
|
1 |
|
$this->checkReservedCharacters($key); |
93
|
|
|
} |
94
|
1 |
|
if ($ttl instanceof DateInterval) { |
95
|
1 |
|
$ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
96
|
|
|
} |
97
|
1 |
|
return $this->handler->setMulti($values, (int) $ttl); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritDoc} |
102
|
|
|
*/ |
103
|
1 |
|
public function deleteMultiple($keys) |
104
|
|
|
{ |
105
|
1 |
|
foreach ($keys as $key) { |
106
|
1 |
|
$this->checkReservedCharacters($key); |
107
|
|
|
} |
108
|
1 |
|
return $this->handler->deleteMulti($keys); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritDoc} |
113
|
|
|
*/ |
114
|
1 |
|
public function increment($key, $step = 1) |
115
|
|
|
{ |
116
|
1 |
|
return $this->handler->increment($key, $step); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritDoc} |
121
|
|
|
*/ |
122
|
1 |
|
public function decrement($key, $step = 1) |
123
|
|
|
{ |
124
|
1 |
|
return $this->handler->decrement($key, $step); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritDoc} |
129
|
|
|
*/ |
130
|
1 |
|
public function has($key) |
131
|
|
|
{ |
132
|
1 |
|
$this->checkReservedCharacters($key); |
133
|
1 |
|
$value = $this->handler->get($key); |
134
|
1 |
|
return Memcached::RES_NOTFOUND !== $this->handler->getResultCode(); |
135
|
|
|
} |
136
|
|
|
|
137
|
6 |
|
private function checkReservedCharacters($key) |
138
|
|
|
{ |
139
|
6 |
|
if (!is_string($key)) { |
140
|
1 |
|
$message = sprintf('key %s is not a string.', $key); |
141
|
1 |
|
throw new InvalidArgumentException($message); |
142
|
|
|
} |
143
|
5 |
|
foreach (self::PSR16_RESERVED_CHARACTERS as $needle) { |
144
|
5 |
|
if (strpos($key, $needle) !== false) { |
145
|
1 |
|
$message = sprintf('%s string is not a legal value.', $key); |
146
|
1 |
|
throw new InvalidArgumentException($message); |
147
|
|
|
} |
148
|
|
|
} |
149
|
4 |
|
} |
150
|
|
|
} |
151
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.