1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Soupmix\Cache; |
6
|
|
|
|
7
|
|
|
use DateInterval; |
8
|
|
|
use DateTime; |
9
|
|
|
use Memcached; |
10
|
|
|
use Psr\SimpleCache\CacheInterface; |
11
|
|
|
|
12
|
|
|
use function array_fill; |
13
|
|
|
use function array_merge; |
14
|
6 |
|
use function count; |
15
|
|
|
use function defined; |
16
|
6 |
|
use function extension_loaded; |
17
|
6 |
|
use function in_array; |
18
|
6 |
|
use function ini_set; |
19
|
|
|
use function time; |
20
|
6 |
|
|
21
|
|
|
class MemcachedCache extends Common implements CacheInterface |
22
|
1 |
|
{ |
23
|
|
|
public $handler; |
24
|
1 |
|
|
25
|
1 |
|
public function __construct(Memcached $handler) |
26
|
1 |
|
{ |
27
|
|
|
$this->handler = $handler; |
28
|
|
|
if (! defined('Memcached::HAVE_IGBINARY') || ! extension_loaded('igbinary')) { |
29
|
4 |
|
return; |
30
|
|
|
} |
31
|
4 |
|
|
32
|
2 |
|
ini_set('memcached.serializer', 'igbinary'); |
33
|
1 |
|
} |
34
|
|
|
|
35
|
2 |
|
public function get($key, $default = null) |
36
|
|
|
{ |
37
|
|
|
$this->checkReservedCharacters($key); |
38
|
1 |
|
$value = $this->handler->get($key); |
39
|
|
|
|
40
|
1 |
|
return $value ?: $default; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
public function set($key, $value, $ttl = null): bool |
44
|
|
|
{ |
45
|
6 |
|
$this->checkReservedCharacters($key); |
46
|
|
|
if ($ttl instanceof DateInterval) { |
47
|
6 |
|
$ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
return $this->handler->set($key, $value, (int) $ttl); |
51
|
|
|
} |
52
|
1 |
|
|
53
|
1 |
|
public function delete($key): bool |
54
|
1 |
|
{ |
55
|
|
|
$this->checkReservedCharacters($key); |
56
|
1 |
|
|
57
|
|
|
return $this->handler->delete($key); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function clear(): bool |
61
|
|
|
{ |
62
|
1 |
|
return $this->handler->flush(); |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
public function getMultiple($keys, $default = null): array |
66
|
1 |
|
{ |
67
|
|
|
$defaults = array_fill(0, count($keys), $default); |
68
|
1 |
|
foreach ($keys as $key) { |
69
|
|
|
$this->checkReservedCharacters($key); |
70
|
|
|
} |
71
|
1 |
|
|
72
|
|
|
return array_merge($this->handler->getMulti($keys), $defaults); |
73
|
1 |
|
} |
74
|
1 |
|
|
75
|
|
|
public function setMultiple($values, $ttl = null): bool |
76
|
1 |
|
{ |
77
|
1 |
|
foreach ($values as $key => $value) { |
78
|
|
|
$this->checkReservedCharacters($key); |
79
|
|
|
} |
80
|
1 |
|
|
81
|
|
|
if ($ttl instanceof DateInterval) { |
82
|
|
|
$ttl = (new DateTime('now'))->add($ttl)->getTimeStamp() - time(); |
83
|
1 |
|
} |
84
|
|
|
|
85
|
1 |
|
return $this->handler->setMulti($values, (int) $ttl); |
86
|
1 |
|
} |
87
|
1 |
|
|
88
|
|
|
public function deleteMultiple($keys): bool |
89
|
|
|
{ |
90
|
|
|
foreach ($keys as $key) { |
91
|
|
|
$this->checkReservedCharacters($key); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$result = $this->handler->deleteMulti($keys); |
95
|
|
|
if (in_array(false, $result, true)) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function has($key): bool |
103
|
|
|
{ |
104
|
|
|
$this->checkReservedCharacters($key); |
105
|
|
|
$value = $this->handler->get($key); |
|
|
|
|
106
|
|
|
|
107
|
|
|
return $this->handler->getResultCode() !== Memcached::RES_NOTFOUND; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|