o2system /
cache
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the O2System Framework package. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Steeve Andrian Salim |
||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 10 | */ |
||
| 11 | |||
| 12 | // ------------------------------------------------------------------------ |
||
| 13 | |||
| 14 | namespace O2System\Cache\Adapters\Memcache; |
||
| 15 | |||
| 16 | // ------------------------------------------------------------------------ |
||
| 17 | |||
| 18 | use O2System\Cache\Abstracts\AbstractAdapter; |
||
| 19 | use O2System\Cache\DataStructures\Config; |
||
| 20 | use O2System\Spl\Exceptions\Logic\BadFunctionCall\BadMethodCallException; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Class Adapter |
||
| 24 | * |
||
| 25 | * @package O2System\Cache\Adapters\File |
||
| 26 | */ |
||
| 27 | abstract class Adapter extends AbstractAdapter |
||
| 28 | {
|
||
| 29 | /** |
||
| 30 | * Adapter::$platform |
||
| 31 | * |
||
| 32 | * Adapter Platform Name |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $platform = 'Memcache'; |
||
| 37 | |||
| 38 | // ------------------------------------------------------------------------ |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Adapter::$memchace |
||
| 42 | * |
||
| 43 | * Memcache Instance |
||
| 44 | * |
||
| 45 | * @var \Memcache |
||
| 46 | */ |
||
| 47 | protected $memcache; |
||
| 48 | |||
| 49 | // ------------------------------------------------------------------------ |
||
| 50 | |||
| 51 | /** |
||
| 52 | * AbstractAdapter::__construct |
||
| 53 | * |
||
| 54 | * @param Config|NULL $config |
||
| 55 | * |
||
| 56 | * @return Adapter |
||
| 57 | */ |
||
| 58 | public function __construct(Config $config = null) |
||
| 59 | {
|
||
| 60 | if (isset($config)) {
|
||
| 61 | if ($this->isSupported()) {
|
||
| 62 | $this->connect($config->getArrayCopy()); |
||
| 63 | |||
| 64 | if ($config->offsetExists('prefixKey')) {
|
||
| 65 | $this->setPrefixKey($config->prefixKey); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 66 | } |
||
| 67 | } |
||
| 68 | } elseif ($this->isSupported()) {
|
||
| 69 | $this->connect([]); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | // ------------------------------------------------------------------------ |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Adapter::isSupported |
||
| 77 | * |
||
| 78 | * Checks if this adapter is supported on this system. |
||
| 79 | * |
||
| 80 | * @return bool Returns FALSE if not supported. |
||
| 81 | */ |
||
| 82 | public function isSupported() |
||
| 83 | {
|
||
| 84 | return (bool)(extension_loaded('memcache') && class_exists('Memcache', false));
|
||
| 85 | } |
||
| 86 | |||
| 87 | // ------------------------------------------------------------------------ |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Adapter::connect |
||
| 91 | * |
||
| 92 | * @param array $config Cache adapter connection configuration. |
||
| 93 | * |
||
| 94 | * @return void |
||
| 95 | */ |
||
| 96 | public function connect(array $config) |
||
| 97 | {
|
||
| 98 | $this->memcache = new \Memcache(); |
||
| 99 | |||
| 100 | if (empty($config)) {
|
||
| 101 | $this->config = [ |
||
| 102 | 'host' => '127.0.0.1', |
||
| 103 | 'port' => 11211, |
||
| 104 | 'persistent' => true, |
||
| 105 | 'timeout' => 1, |
||
| 106 | 'retryInterval' => 15, |
||
| 107 | 'weight' => 1, |
||
| 108 | ]; |
||
| 109 | |||
| 110 | $this->memcache->addserver($this->config[ 'host' ], $this->config[ 'port' ]); |
||
| 111 | } elseif (isset($config[ 'servers' ])) {
|
||
| 112 | foreach ($config[ 'servers' ] as $server) {
|
||
| 113 | $this->config[ $server[ 'host' ] ] = array_merge( |
||
| 114 | [ |
||
| 115 | 'host' => '127.0.0.1', |
||
| 116 | 'port' => 11211, |
||
| 117 | 'persistent' => true, |
||
| 118 | 'timeout' => 1, |
||
| 119 | 'weight' => 1, |
||
| 120 | ], |
||
| 121 | $server |
||
| 122 | ); |
||
| 123 | |||
| 124 | if (array_key_exists('status', $server)) {
|
||
| 125 | if ($server[ 'status' ] === false) {
|
||
| 126 | $this->config[ $server[ 'host' ] ][ 'retryInterval' ] = -1; |
||
| 127 | |||
| 128 | $this->memcache->addserver( |
||
| 129 | $this->config[ $server[ 'host' ] ][ 'host' ], |
||
| 130 | $this->config[ $server[ 'host' ] ][ 'port' ], |
||
| 131 | $this->config[ $server[ 'host' ] ][ 'persistent' ], |
||
| 132 | $this->config[ $server[ 'host' ] ][ 'weight' ], |
||
| 133 | $this->config[ $server[ 'host' ] ][ 'timeout' ], |
||
| 134 | $this->config[ $server[ 'host' ] ][ 'retryInterval' ], |
||
| 135 | false |
||
| 136 | ); |
||
| 137 | |||
| 138 | continue; |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | $this->memcache->addserver( |
||
| 143 | $this->config[ $server[ 'host' ] ][ 'host' ], |
||
| 144 | $this->config[ $server[ 'host' ] ][ 'port' ], |
||
| 145 | $this->config[ $server[ 'host' ] ][ 'persistent' ], |
||
| 146 | $this->config[ $server[ 'host' ] ][ 'weight' ], |
||
| 147 | $this->config[ $server[ 'host' ] ][ 'timeout' ] |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | } else {
|
||
| 151 | $this->config = array_merge( |
||
| 152 | [ |
||
| 153 | 'host' => '127.0.0.1', |
||
| 154 | 'port' => 11211, |
||
| 155 | 'persistent' => true, |
||
| 156 | 'timeout' => 1, |
||
| 157 | 'weight' => 1, |
||
| 158 | ], |
||
| 159 | $config |
||
| 160 | ); |
||
| 161 | |||
| 162 | if (isset($this->config[ 'status' ])) {
|
||
| 163 | if ($this->config[ 'status' ] === false) {
|
||
| 164 | $this->memcache->addserver( |
||
| 165 | $this->config[ 'host' ], |
||
| 166 | $this->config[ 'port' ], |
||
| 167 | $this->config[ 'persistent' ], |
||
| 168 | $this->config[ 'weight' ], |
||
| 169 | $this->config[ 'timeout' ], |
||
| 170 | -1, |
||
| 171 | false |
||
| 172 | ); |
||
| 173 | } else {
|
||
| 174 | $this->memcache->addserver( |
||
| 175 | $this->config[ 'host' ], |
||
| 176 | $this->config[ 'port' ], |
||
| 177 | $this->config[ 'persistent' ], |
||
| 178 | $this->config[ 'weight' ], |
||
| 179 | $this->config[ 'timeout' ] |
||
| 180 | ); |
||
| 181 | } |
||
| 182 | } else {
|
||
| 183 | $this->memcache->addserver( |
||
| 184 | $this->config[ 'host' ], |
||
| 185 | $this->config[ 'port' ], |
||
| 186 | $this->config[ 'persistent' ], |
||
| 187 | $this->config[ 'weight' ], |
||
| 188 | $this->config[ 'timeout' ] |
||
| 189 | ); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | // ------------------------------------------------------------------------ |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Adapter::isConnected |
||
| 198 | * |
||
| 199 | * Checks if this adapter has a successful connection. |
||
| 200 | * |
||
| 201 | * @return bool Returns FALSE if not supported. |
||
| 202 | */ |
||
| 203 | public function isConnected() |
||
| 204 | {
|
||
| 205 | return (bool)($this->memcache instanceof \Memcache); |
||
| 206 | } |
||
| 207 | |||
| 208 | // ------------------------------------------------------------------------ |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Adapter::__call |
||
| 212 | * |
||
| 213 | * @param string $method Memcache Adapter Class / Memcache Class / Memcache ItemPool Class method name. |
||
| 214 | * @param array $arguments Method arguments. |
||
| 215 | * |
||
| 216 | * @return mixed |
||
| 217 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadMethodCallException |
||
| 218 | */ |
||
| 219 | public function __call($method, array $arguments = []) |
||
| 220 | {
|
||
| 221 | if (method_exists($this, $method)) {
|
||
| 222 | return call_user_func_array([&$this, $method], $arguments); |
||
| 223 | } elseif ($this->memcache instanceof \Memcache AND method_exists($this->memcache, $method)) {
|
||
| 224 | return call_user_func_array([&$this->memcache, $method], $arguments); |
||
| 225 | } |
||
| 226 | |||
| 227 | throw new BadMethodCallException('E_BAD_METHOD_CALL_CACHE_EXCEPTION', 0, [__CLASS__ . '::' . $method]);
|
||
| 228 | } |
||
| 229 | |||
| 230 | // ------------------------------------------------------------------------ |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Adapter::increment |
||
| 234 | * |
||
| 235 | * Increment a raw value offset. |
||
| 236 | * |
||
| 237 | * @param string $key Cache item key. |
||
| 238 | * @param int $step Increment step to add. |
||
| 239 | * |
||
| 240 | * @return mixed New value on success or FALSE on failure. |
||
| 241 | */ |
||
| 242 | public function increment($key, $step = 1) |
||
| 243 | {
|
||
| 244 | return $this->memcache->increment($this->prefixKey . $key, $step); |
||
| 245 | } |
||
| 246 | |||
| 247 | // ------------------------------------------------------------------------ |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Adapter::decrement |
||
| 251 | * |
||
| 252 | * Decrement a raw value offset. |
||
| 253 | * |
||
| 254 | * @param string $key Cache item key. |
||
| 255 | * @param int $step Decrement step to add. |
||
| 256 | * |
||
| 257 | * @return mixed New value on success or FALSE on failure. |
||
| 258 | */ |
||
| 259 | public function decrement($key, $step = 1) |
||
| 260 | {
|
||
| 261 | return $this->memcache->decrement($this->prefixKey . $key, $step); |
||
| 262 | } |
||
| 263 | |||
| 264 | // ------------------------------------------------------------------------ |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Adapter::getInfo |
||
| 268 | * |
||
| 269 | * Gets item pool adapter info. |
||
| 270 | * |
||
| 271 | * @return mixed |
||
| 272 | */ |
||
| 273 | public function getInfo() |
||
| 274 | {
|
||
| 275 | return [ |
||
| 276 | 'version' => $this->memcache->getVersion(), |
||
| 277 | ]; |
||
| 278 | } |
||
| 279 | |||
| 280 | // ------------------------------------------------------------------------ |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Adapter::getInfo |
||
| 284 | * |
||
| 285 | * Gets item pool adapter stats. |
||
| 286 | * |
||
| 287 | * @return mixed |
||
| 288 | */ |
||
| 289 | public function getStats() |
||
| 290 | {
|
||
| 291 | return $this->memcache->getExtendedStats(); |
||
| 292 | } |
||
| 293 | } |