|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class MemcachedCache |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource MemcachedCache.php |
|
6
|
|
|
* @created 25.05.2017 |
|
7
|
|
|
* @package chillerlan\SimpleCache |
|
8
|
|
|
* @author Smiley <[email protected]> |
|
9
|
|
|
* @copyright 2017 Smiley |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
* |
|
12
|
|
|
* @noinspection PhpComposerExtensionStubsInspection |
|
13
|
|
|
* @phan-file-suppress PhanUndeclaredClassMethod, PhanUndeclaredTypeProperty, PhanUndeclaredTypeParameter |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace chillerlan\SimpleCache; |
|
17
|
|
|
|
|
18
|
|
|
use chillerlan\Settings\SettingsContainerInterface; |
|
19
|
|
|
use Memcached; |
|
20
|
|
|
use Psr\Log\LoggerInterface; |
|
21
|
|
|
|
|
22
|
|
|
use function array_keys; |
|
23
|
|
|
|
|
24
|
|
|
class MemcachedCache extends CacheDriverAbstract{ |
|
25
|
|
|
|
|
26
|
|
|
protected Memcached $memcached; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* MemcachedCache constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param \Memcached $memcached |
|
32
|
|
|
* @param \chillerlan\Settings\SettingsContainerInterface|null $options |
|
33
|
|
|
* @param \Psr\Log\LoggerInterface|null $logger |
|
34
|
|
|
* |
|
35
|
|
|
* @throws \chillerlan\SimpleCache\CacheException |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(Memcached $memcached, SettingsContainerInterface $options = null, LoggerInterface $logger = null){ |
|
38
|
|
|
parent::__construct($options, $logger); |
|
39
|
|
|
|
|
40
|
|
|
$this->memcached = $memcached; |
|
41
|
|
|
|
|
42
|
|
|
if(empty($this->memcached->getServerList())){ |
|
43
|
|
|
throw new CacheException('no memcache server available'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** @inheritdoc */ |
|
49
|
|
|
public function get($key, $default = null){ |
|
50
|
|
|
$value = $this->memcached->get($this->checkKey($key)); |
|
51
|
|
|
|
|
52
|
|
|
if($value !== false){ |
|
53
|
|
|
return $value; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $default; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** @inheritdoc */ |
|
60
|
|
|
public function set($key, $value, $ttl = null):bool{ |
|
61
|
|
|
return $this->memcached->set($this->checkKey($key), $value, $this->getTTL($ttl) ?? 0); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @inheritdoc */ |
|
65
|
|
|
public function delete($key):bool{ |
|
66
|
|
|
return $this->memcached->delete($this->checkKey($key)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** @inheritdoc */ |
|
70
|
|
|
public function clear():bool{ |
|
71
|
|
|
return $this->memcached->flush(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** @inheritdoc */ |
|
75
|
|
|
public function getMultiple($keys, $default = null):array{ |
|
76
|
|
|
$keys = $this->getData($keys); |
|
77
|
|
|
|
|
78
|
|
|
$this->checkKeyArray($keys); |
|
79
|
|
|
|
|
80
|
|
|
$values = $this->memcached->getMulti($keys); |
|
81
|
|
|
$return = []; |
|
82
|
|
|
|
|
83
|
|
|
foreach($keys as $key){ |
|
84
|
|
|
$return[$key] = $values[$key] ?? $default; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $return; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** @inheritdoc */ |
|
91
|
|
|
public function setMultiple($values, $ttl = null):bool{ |
|
92
|
|
|
$values = $this->getData($values); |
|
93
|
|
|
|
|
94
|
|
|
$this->checkKeyArray(array_keys($values)); |
|
95
|
|
|
|
|
96
|
|
|
return $this->memcached->setMulti($values, $this->getTTL($ttl) ?? 0); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** @inheritdoc */ |
|
100
|
|
|
public function deleteMultiple($keys):bool{ |
|
101
|
|
|
$keys = $this->getData($keys); |
|
102
|
|
|
|
|
103
|
|
|
$this->checkKeyArray($keys); |
|
104
|
|
|
|
|
105
|
|
|
return $this->checkReturn($this->memcached->deleteMulti($keys)); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|