These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
|||||||||||
2 | /** |
|||||||||||
3 | * Kotori.php |
|||||||||||
4 | * |
|||||||||||
5 | * A Tiny Model-View-Controller PHP Framework |
|||||||||||
6 | * |
|||||||||||
7 | * This content is released under the Apache 2 License |
|||||||||||
8 | * |
|||||||||||
9 | * Copyright (c) 2015-2017 Kotori Technology. All rights reserved. |
|||||||||||
10 | * |
|||||||||||
11 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|||||||||||
12 | * you may not use this file except in compliance with the License. |
|||||||||||
13 | * You may obtain a copy of the License at |
|||||||||||
14 | * |
|||||||||||
15 | * http://www.apache.org/licenses/LICENSE-2.0 |
|||||||||||
16 | * |
|||||||||||
17 | * Unless required by applicable law or agreed to in writing, software |
|||||||||||
18 | * distributed under the License is distributed on an "AS IS" BASIS, |
|||||||||||
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||||||||||
20 | * See the License for the specific language governing permissions and |
|||||||||||
21 | * limitations under the License. |
|||||||||||
22 | */ |
|||||||||||
23 | ||||||||||||
24 | /** |
|||||||||||
25 | * Memcached Caching Class |
|||||||||||
26 | * |
|||||||||||
27 | * @package Kotori |
|||||||||||
28 | * @subpackage Cache |
|||||||||||
29 | * @author Kokororin |
|||||||||||
30 | * @link https://kotori.love |
|||||||||||
31 | */ |
|||||||||||
32 | namespace Kotori\Core\Cache; |
|||||||||||
33 | ||||||||||||
34 | use Kotori\Core\Container; |
|||||||||||
35 | use Kotori\Debug\Hook; |
|||||||||||
36 | use Kotori\Exception\CacheException; |
|||||||||||
37 | ||||||||||||
38 | class Memcached |
|||||||||||
39 | { |
|||||||||||
40 | /** |
|||||||||||
41 | * Holds the memcached object |
|||||||||||
42 | * |
|||||||||||
43 | * @var object |
|||||||||||
44 | */ |
|||||||||||
45 | protected $memcached; |
|||||||||||
46 | ||||||||||||
47 | /** |
|||||||||||
48 | * Memcached configuration |
|||||||||||
49 | * |
|||||||||||
50 | * @var array |
|||||||||||
51 | */ |
|||||||||||
52 | protected $memcacheConf = [ |
|||||||||||
53 | 'default' => [ |
|||||||||||
54 | 'host' => '127.0.0.1', |
|||||||||||
55 | 'port' => 11211, |
|||||||||||
56 | 'weight' => 1, |
|||||||||||
57 | ], |
|||||||||||
58 | ]; |
|||||||||||
59 | ||||||||||||
60 | /** |
|||||||||||
61 | * Class constructor |
|||||||||||
62 | * |
|||||||||||
63 | * Setup Memcache(d) |
|||||||||||
64 | * |
|||||||||||
65 | * @param array $config |
|||||||||||
66 | * @return void |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
67 | * |
|||||||||||
68 | * @throws \Kotori\Exception\CacheException |
|||||||||||
69 | */ |
|||||||||||
70 | 1 | public function __construct($config = []) |
||||||||||
71 | { |
|||||||||||
72 | // Try to load memcached server info from the config file. |
|||||||||||
73 | 1 | $defaults = $this->memcacheConf['default']; |
||||||||||
74 | 1 | if (empty($config)) { |
||||||||||
75 | 1 | $config = Container::get('config')->get('cache'); |
||||||||||
76 | } |
|||||||||||
77 | ||||||||||||
78 | 1 | $memcacheConf = isset($config['memcached']) ? $config['memcached'] : null; |
||||||||||
79 | ||||||||||||
80 | 1 | if (is_array($memcacheConf)) { |
||||||||||
81 | $this->memcacheConf = []; |
|||||||||||
82 | ||||||||||||
83 | foreach ($memcacheConf as $name => $conf) { |
|||||||||||
84 | $this->memcacheConf[$name] = $conf; |
|||||||||||
85 | } |
|||||||||||
86 | } |
|||||||||||
87 | ||||||||||||
88 | 1 | if (class_exists('Memcached', false)) { |
||||||||||
89 | 1 | $this->memcached = new \Memcached(); |
||||||||||
90 | } elseif (class_exists('Memcache', false)) { |
|||||||||||
91 | $this->memcached = new \Memcache(); |
|||||||||||
92 | } else { |
|||||||||||
93 | throw new CacheException('Failed to create Memcache(d) object; extension not loaded?'); |
|||||||||||
94 | } |
|||||||||||
95 | ||||||||||||
96 | 1 | foreach ($this->memcacheConf as $cacheServer) { |
||||||||||
97 | 1 | if (!isset($cacheServer['host'])) { |
||||||||||
98 | $cacheServer['host'] = $defaults['host']; |
|||||||||||
99 | } |
|||||||||||
100 | ||||||||||||
101 | 1 | if (!isset($cacheServer['port'])) { |
||||||||||
102 | $cacheServer['port'] = $defaults['port']; |
|||||||||||
103 | } |
|||||||||||
104 | ||||||||||||
105 | 1 | if (!isset($cacheServer['weight'])) { |
||||||||||
106 | $cacheServer['weight'] = $defaults['weight']; |
|||||||||||
107 | } |
|||||||||||
108 | ||||||||||||
109 | 1 | if (get_class($this->memcached) === 'Memcache') { |
||||||||||
110 | // Third parameter is persistance and defaults to TRUE. |
|||||||||||
111 | $this->memcached->addServer( |
|||||||||||
112 | $cacheServer['host'], |
|||||||||||
113 | $cacheServer['port'], |
|||||||||||
114 | true, |
|||||||||||
115 | $cacheServer['weight'] |
|||||||||||
116 | ); |
|||||||||||
117 | } else { |
|||||||||||
118 | 1 | $this->memcached->addServer( |
||||||||||
119 | 1 | $cacheServer['host'], |
||||||||||
120 | 1 | $cacheServer['port'], |
||||||||||
121 | 1 | $cacheServer['weight'] |
||||||||||
122 | ); |
|||||||||||
123 | } |
|||||||||||
124 | } |
|||||||||||
125 | ||||||||||||
126 | 1 | Hook::listen(__CLASS__); |
||||||||||
127 | 1 | } |
||||||||||
128 | ||||||||||||
129 | /** |
|||||||||||
130 | * Fetch from cache |
|||||||||||
131 | * |
|||||||||||
132 | * @param string $id |
|||||||||||
133 | * @return mixed |
|||||||||||
134 | */ |
|||||||||||
135 | 3 | public function get($id) |
||||||||||
136 | { |
|||||||||||
137 | 3 | $data = $this->memcached->get($id); |
||||||||||
138 | ||||||||||||
139 | 3 | return is_array($data) ? $data[0] : $data; |
||||||||||
140 | } |
|||||||||||
141 | ||||||||||||
142 | /** |
|||||||||||
143 | * Set |
|||||||||||
144 | * |
|||||||||||
145 | * @param string $id |
|||||||||||
146 | * @param mixed $data |
|||||||||||
147 | * @param int $ttl |
|||||||||||
148 | * @param boolean $raw |
|||||||||||
149 | * @return boolean |
|||||||||||
150 | */ |
|||||||||||
151 | 3 | public function set($id, $data, $ttl = 60, $raw = false) |
||||||||||
152 | { |
|||||||||||
153 | 3 | if ($raw !== true) { |
||||||||||
154 | 3 | $data = [$data, time(), $ttl]; |
||||||||||
155 | } |
|||||||||||
156 | ||||||||||||
157 | 3 | if (get_class($this->memcached) === 'Memcached') { |
||||||||||
158 | 3 | return $this->memcached->set($id, $data, $ttl); |
||||||||||
159 | } elseif (get_class($this->memcached) === 'Memcache') { |
|||||||||||
160 | return $this->memcached->set($id, $data, 0, $ttl); |
|||||||||||
161 | } |
|||||||||||
162 | ||||||||||||
163 | return false; |
|||||||||||
164 | } |
|||||||||||
165 | ||||||||||||
166 | /** |
|||||||||||
167 | * Delete from Cache |
|||||||||||
168 | * |
|||||||||||
169 | * @param mixed $id |
|||||||||||
170 | * @return boolean |
|||||||||||
171 | */ |
|||||||||||
172 | 1 | public function delete($id) |
||||||||||
173 | { |
|||||||||||
174 | 1 | return $this->memcached->delete($id); |
||||||||||
175 | } |
|||||||||||
176 | ||||||||||||
177 | /** |
|||||||||||
178 | * Increment a raw value |
|||||||||||
179 | * |
|||||||||||
180 | * @param string $id |
|||||||||||
181 | * @param int $offset |
|||||||||||
182 | * @return mixed |
|||||||||||
183 | */ |
|||||||||||
184 | public function increment($id, $offset = 1) |
|||||||||||
185 | { |
|||||||||||
186 | return $this->memcached->increment($id, $offset); |
|||||||||||
187 | } |
|||||||||||
188 | ||||||||||||
189 | /** |
|||||||||||
190 | * Decrement a raw value |
|||||||||||
191 | * |
|||||||||||
192 | * @param string $id |
|||||||||||
193 | * @param int $offset |
|||||||||||
194 | * @return mixed |
|||||||||||
195 | */ |
|||||||||||
196 | public function decrement($id, $offset = 1) |
|||||||||||
197 | { |
|||||||||||
198 | return $this->memcached->decrement($id, $offset); |
|||||||||||
199 | } |
|||||||||||
200 | ||||||||||||
201 | /** |
|||||||||||
202 | * Clean the Cache |
|||||||||||
203 | * |
|||||||||||
204 | * @return boolean |
|||||||||||
205 | */ |
|||||||||||
206 | 1 | public function clean() |
||||||||||
207 | { |
|||||||||||
208 | 1 | return $this->memcached->flush(); |
||||||||||
209 | } |
|||||||||||
210 | ||||||||||||
211 | /** |
|||||||||||
212 | * Cache Info |
|||||||||||
213 | * |
|||||||||||
214 | * @return mixed |
|||||||||||
215 | */ |
|||||||||||
216 | public function cacheInfo() |
|||||||||||
217 | { |
|||||||||||
218 | return $this->memcached->getStats(); |
|||||||||||
219 | } |
|||||||||||
220 | ||||||||||||
221 | /** |
|||||||||||
222 | * Get Cache Metadata |
|||||||||||
223 | * |
|||||||||||
224 | * @param mixed $id |
|||||||||||
225 | * @return mixed |
|||||||||||
226 | */ |
|||||||||||
227 | public function getMetadata($id) |
|||||||||||
228 | { |
|||||||||||
229 | $stored = $this->memcached->get($id); |
|||||||||||
230 | ||||||||||||
231 | if (count($stored) !== 3) { |
|||||||||||
232 | return false; |
|||||||||||
233 | } |
|||||||||||
234 | ||||||||||||
235 | list($data, $time, $ttl) = $stored; |
|||||||||||
236 | ||||||||||||
237 | return [ |
|||||||||||
238 | 'expire' => $time + $ttl, |
|||||||||||
239 | 'mtime' => $time, |
|||||||||||
240 | 'data' => $data, |
|||||||||||
241 | ]; |
|||||||||||
242 | } |
|||||||||||
243 | ||||||||||||
244 | /** |
|||||||||||
245 | * Is supported |
|||||||||||
246 | * |
|||||||||||
247 | * Returns FALSE if memcached is not supported on the system. |
|||||||||||
248 | * If it is, we setup the memcached object & return TRUE |
|||||||||||
249 | * |
|||||||||||
250 | * @return boolean |
|||||||||||
251 | */ |
|||||||||||
252 | 1 | public function isSupported() |
||||||||||
253 | { |
|||||||||||
254 | 1 | return (extension_loaded('memcached') or extension_loaded('memcache')); |
||||||||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Using logical operators such as
or instead of || is generally not recommended.
PHP has two types of connecting operators (logical operators, and boolean operators):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like Let’s take a look at a few examples: // Logical operators have lower precedence:
$f = false or true;
// is executed like this:
($f = false) or true;
// Boolean operators have higher precedence:
$f = false || true;
// is executed like this:
$f = (false || true);
Logical Operators are used for Control-FlowOne case where you explicitly want to use logical operators is for control-flow such as this: $x === 5
or die('$x must be 5.');
// Instead of
if ($x !== 5) {
die('$x must be 5.');
}
Since // The following is currently a parse error.
$x === 5
or throw new RuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current PHP code.
Loading history...
|
||||||||||||
255 | } |
|||||||||||
256 | } |
|||||||||||
257 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.