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 |
|
|
|
|
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 $key |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
6 |
|
public function get($key) |
136
|
|
|
{ |
137
|
6 |
|
$value = $this->memcached->get($key); |
138
|
|
|
|
139
|
6 |
|
return is_array($value) ? $value[0] : $value; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set |
144
|
|
|
* |
145
|
|
|
* @param string $key |
146
|
|
|
* @param mixed $value |
147
|
|
|
* @param int $ttl |
148
|
|
|
* @param boolean $raw |
149
|
|
|
* @return boolean |
150
|
|
|
*/ |
151
|
6 |
|
public function set($key, $value, $ttl = 60, $raw = false) |
152
|
|
|
{ |
153
|
6 |
|
if ($raw !== true) { |
154
|
6 |
|
$value = [$value, time(), $ttl]; |
155
|
|
|
} |
156
|
|
|
|
157
|
6 |
|
if (get_class($this->memcached) === 'Memcached') { |
158
|
6 |
|
return $this->memcached->set($key, $value, $ttl); |
159
|
|
|
} elseif (get_class($this->memcached) === 'Memcache') { |
160
|
|
|
return $this->memcached->set($key, $value, 0, $ttl); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Delete from Cache |
168
|
|
|
* |
169
|
|
|
* @param mixed $key |
170
|
|
|
* @return boolean |
171
|
|
|
*/ |
172
|
3 |
|
public function delete($key) |
173
|
|
|
{ |
174
|
3 |
|
return $this->memcached->delete($key); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Clean the Cache |
179
|
|
|
* |
180
|
|
|
* @return boolean |
181
|
|
|
*/ |
182
|
1 |
|
public function clear() |
183
|
|
|
{ |
184
|
1 |
|
return $this->memcached->flush(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Is supported |
189
|
|
|
* |
190
|
|
|
* Returns FALSE if memcached is not supported on the system. |
191
|
|
|
* If it is, we setup the memcached object & return TRUE |
192
|
|
|
* |
193
|
|
|
* @return boolean |
194
|
|
|
*/ |
195
|
1 |
|
public function isSupported() |
196
|
|
|
{ |
197
|
1 |
|
return (extension_loaded('memcached') or extension_loaded('memcache')); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
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.