1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace voku\cache; |
6
|
|
|
|
7
|
|
|
use voku\cache\Exception\InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* AdapterMemcache: Memcache-adapter |
11
|
|
|
* |
12
|
|
|
* @package voku\cache |
13
|
|
|
*/ |
14
|
|
|
class AdapterMemcache implements iAdapter |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
public $installed = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Memcache |
23
|
|
|
*/ |
24
|
|
|
private $memcache; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $compressed = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* __construct |
33
|
|
|
* |
34
|
|
|
* @param \Memcache|null $memcache |
35
|
|
|
*/ |
36
|
|
|
public function __construct($memcache = null) |
37
|
|
|
{ |
38
|
|
|
if ($memcache instanceof \Memcache) { |
39
|
|
|
$this->setMemcache($memcache); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param \Memcache $memcache |
45
|
|
|
*/ |
46
|
|
|
public function setMemcache(\Memcache $memcache) { |
47
|
|
|
$this->memcache = $memcache; |
48
|
|
|
$this->installed = true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
public function exists(string $key): bool |
55
|
|
|
{ |
56
|
|
|
return $this->get($key) !== false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
public function get(string $key) |
63
|
|
|
{ |
64
|
|
|
return $this->memcache->get($key); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
public function installed(): bool |
71
|
|
|
{ |
72
|
|
|
return $this->installed; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
public function remove(string $key): bool |
79
|
|
|
{ |
80
|
|
|
return $this->memcache->delete($key); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
|
|
public function removeAll(): bool |
87
|
|
|
{ |
88
|
|
|
return $this->memcache->flush(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function set(string $key, $value): bool |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
// Make sure we are under the proper limit |
97
|
|
|
if (\strlen($key) > 250) { |
98
|
|
|
throw new InvalidArgumentException('The passed cache key is over 250 bytes:' . print_r($key, true)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->memcache->set($key, $value, $this->getCompressedFlag()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritdoc |
106
|
|
|
*/ |
107
|
|
|
public function setExpired(string $key, $value, int $ttl = 0): bool |
108
|
|
|
{ |
109
|
|
|
if ($ttl > 2592000) { |
110
|
|
|
$ttl = 2592000; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->memcache->set($key, $value, $this->getCompressedFlag(), $ttl); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get the compressed-flag from MemCache. |
118
|
|
|
* |
119
|
|
|
* @return int 2 || 0 |
120
|
|
|
*/ |
121
|
|
|
private function getCompressedFlag(): int |
122
|
|
|
{ |
123
|
|
|
return $this->isCompressed() ? MEMCACHE_COMPRESSED : 0; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Check if compression from MemCache is active. |
128
|
|
|
* |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function isCompressed(): bool |
132
|
|
|
{ |
133
|
|
|
return $this->compressed; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Activate the compression from MemCache. |
138
|
|
|
* |
139
|
|
|
* @param mixed $value will be converted to bool |
140
|
|
|
*/ |
141
|
|
|
public function setCompressed($value) |
142
|
|
|
{ |
143
|
|
|
$this->compressed = (bool)$value; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
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.