Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 8 | * |
Master Yoda | da60e9b | 2016-12-31 08:46:18 -0800 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2017, British Columbia Institute of Technology |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Master Yoda | da60e9b | 2016-12-31 08:46:18 -0800 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 2.0 |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 36 | * @filesource |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 39 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 40 | /** |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 41 | * CodeIgniter Memcached Caching Class |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Core |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 47 | * @link |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 48 | */ |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 49 | class CI_Cache_memcached extends CI_Driver { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 50 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 51 | /** |
| 52 | * Holds the memcached object |
| 53 | * |
| 54 | * @var object |
| 55 | */ |
| 56 | protected $_memcached; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 57 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 58 | /** |
| 59 | * Memcached configuration |
| 60 | * |
| 61 | * @var array |
| 62 | */ |
Andrey Andreev | 9c948aa | 2016-03-12 12:33:07 +0200 | [diff] [blame] | 63 | protected $_config = array( |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 64 | 'default' => array( |
Andrey Andreev | 6a3d7e5 | 2013-07-18 19:14:05 +0300 | [diff] [blame] | 65 | 'host' => '127.0.0.1', |
| 66 | 'port' => 11211, |
| 67 | 'weight' => 1 |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 68 | ) |
| 69 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 70 | |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 71 | // ------------------------------------------------------------------------ |
| 72 | |
| 73 | /** |
| 74 | * Class constructor |
| 75 | * |
| 76 | * Setup Memcache(d) |
| 77 | * |
| 78 | * @return void |
| 79 | */ |
| 80 | public function __construct() |
| 81 | { |
| 82 | // Try to load memcached server info from the config file. |
| 83 | $CI =& get_instance(); |
Andrey Andreev | 9c948aa | 2016-03-12 12:33:07 +0200 | [diff] [blame] | 84 | $defaults = $this->_config['default']; |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 85 | |
| 86 | if ($CI->config->load('memcached', TRUE, TRUE)) |
| 87 | { |
Andrey Andreev | 9c948aa | 2016-03-12 12:33:07 +0200 | [diff] [blame] | 88 | $this->_config = $CI->config->config['memcached']; |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (class_exists('Memcached', FALSE)) |
| 92 | { |
| 93 | $this->_memcached = new Memcached(); |
| 94 | } |
| 95 | elseif (class_exists('Memcache', FALSE)) |
| 96 | { |
| 97 | $this->_memcached = new Memcache(); |
| 98 | } |
| 99 | else |
| 100 | { |
Andrey Andreev | 24a4a6a | 2015-08-31 15:11:47 +0300 | [diff] [blame] | 101 | log_message('error', 'Cache: Failed to create Memcache(d) object; extension not loaded?'); |
Andrey Andreev | 1be8987 | 2016-03-11 18:12:57 +0200 | [diff] [blame] | 102 | return; |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 103 | } |
| 104 | |
Andrey Andreev | 9c948aa | 2016-03-12 12:33:07 +0200 | [diff] [blame] | 105 | foreach ($this->_config as $cache_server) |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 106 | { |
| 107 | isset($cache_server['hostname']) OR $cache_server['hostname'] = $defaults['host']; |
| 108 | isset($cache_server['port']) OR $cache_server['port'] = $defaults['port']; |
| 109 | isset($cache_server['weight']) OR $cache_server['weight'] = $defaults['weight']; |
| 110 | |
Andrey Andreev | c13b4a6 | 2016-03-12 12:30:21 +0200 | [diff] [blame] | 111 | if ($this->_memcached instanceof Memcache) |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 112 | { |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 113 | // Third parameter is persistence and defaults to TRUE. |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 114 | $this->_memcached->addServer( |
| 115 | $cache_server['hostname'], |
| 116 | $cache_server['port'], |
| 117 | TRUE, |
| 118 | $cache_server['weight'] |
| 119 | ); |
| 120 | } |
Andrey Andreev | c13b4a6 | 2016-03-12 12:30:21 +0200 | [diff] [blame] | 121 | elseif ($this->_memcached instanceof Memcached) |
Andrey Andreev | 9aade1c | 2015-06-22 13:19:45 +0300 | [diff] [blame] | 122 | { |
| 123 | $this->_memcached->addServer( |
| 124 | $cache_server['hostname'], |
| 125 | $cache_server['port'], |
| 126 | $cache_server['weight'] |
| 127 | ); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // ------------------------------------------------------------------------ |
| 133 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 134 | /** |
| 135 | * Fetch from cache |
| 136 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 137 | * @param string $id Cache ID |
| 138 | * @return mixed Data on success, FALSE on failure |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 139 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 140 | public function get($id) |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 141 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 142 | $data = $this->_memcached->get($id); |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 143 | |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 144 | return is_array($data) ? $data[0] : $data; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // ------------------------------------------------------------------------ |
| 148 | |
| 149 | /** |
| 150 | * Save |
| 151 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 152 | * @param string $id Cache ID |
| 153 | * @param mixed $data Data being cached |
| 154 | * @param int $ttl Time to live |
| 155 | * @param bool $raw Whether to store the raw value |
| 156 | * @return bool TRUE on success, FALSE on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 157 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 158 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 159 | { |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 160 | if ($raw !== TRUE) |
| 161 | { |
Andrey Andreev | b2a0e70 | 2014-01-18 16:54:51 +0200 | [diff] [blame] | 162 | $data = array($data, time(), $ttl); |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Andrey Andreev | c13b4a6 | 2016-03-12 12:30:21 +0200 | [diff] [blame] | 165 | if ($this->_memcached instanceof Memcached) |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 166 | { |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 167 | return $this->_memcached->set($id, $data, $ttl); |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 168 | } |
Andrey Andreev | c13b4a6 | 2016-03-12 12:30:21 +0200 | [diff] [blame] | 169 | elseif ($this->_memcached instanceof Memcache) |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 170 | { |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 171 | return $this->_memcached->set($id, $data, 0, $ttl); |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 172 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 173 | |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 174 | return FALSE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // ------------------------------------------------------------------------ |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 178 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 179 | /** |
| 180 | * Delete from Cache |
| 181 | * |
Andrey Andreev | 364ba6f | 2016-03-12 12:18:36 +0200 | [diff] [blame] | 182 | * @param mixed $id key to be deleted. |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 183 | * @return bool true on success, false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 184 | */ |
| 185 | public function delete($id) |
| 186 | { |
| 187 | return $this->_memcached->delete($id); |
| 188 | } |
| 189 | |
| 190 | // ------------------------------------------------------------------------ |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 191 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 192 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 193 | * Increment a raw value |
| 194 | * |
| 195 | * @param string $id Cache ID |
| 196 | * @param int $offset Step/value to add |
| 197 | * @return mixed New value on success or FALSE on failure |
| 198 | */ |
| 199 | public function increment($id, $offset = 1) |
| 200 | { |
Andrey Andreev | 47df088 | 2018-01-05 17:29:31 +0200 | [diff] [blame^] | 201 | if (($result = $this->_memcached->increment($id, $offset)) === FALSE) |
| 202 | { |
| 203 | return $this->_memcached->add($id, $offset) ? $offset : FALSE; |
| 204 | } |
| 205 | |
| 206 | return $result; |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // ------------------------------------------------------------------------ |
| 210 | |
| 211 | /** |
| 212 | * Decrement a raw value |
| 213 | * |
| 214 | * @param string $id Cache ID |
| 215 | * @param int $offset Step/value to reduce by |
| 216 | * @return mixed New value on success or FALSE on failure |
| 217 | */ |
| 218 | public function decrement($id, $offset = 1) |
| 219 | { |
Andrey Andreev | 47df088 | 2018-01-05 17:29:31 +0200 | [diff] [blame^] | 220 | if (($result = $this->_memcached->decrement($id, $offset)) === FALSE) |
| 221 | { |
| 222 | return $this->_memcached->add($id, 0) ? 0 : FALSE; |
| 223 | } |
| 224 | |
| 225 | return $result; |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | // ------------------------------------------------------------------------ |
| 229 | |
| 230 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 231 | * Clean the Cache |
| 232 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 233 | * @return bool false on failure/true on success |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 234 | */ |
| 235 | public function clean() |
| 236 | { |
| 237 | return $this->_memcached->flush(); |
| 238 | } |
| 239 | |
| 240 | // ------------------------------------------------------------------------ |
| 241 | |
| 242 | /** |
| 243 | * Cache Info |
| 244 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 245 | * @return mixed array on success, false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 246 | */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 247 | public function cache_info() |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 248 | { |
| 249 | return $this->_memcached->getStats(); |
| 250 | } |
| 251 | |
| 252 | // ------------------------------------------------------------------------ |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 253 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 254 | /** |
| 255 | * Get Cache Metadata |
| 256 | * |
Andrey Andreev | 364ba6f | 2016-03-12 12:18:36 +0200 | [diff] [blame] | 257 | * @param mixed $id key to get cache metadata on |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 258 | * @return mixed FALSE on failure, array on success. |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 259 | */ |
| 260 | public function get_metadata($id) |
| 261 | { |
| 262 | $stored = $this->_memcached->get($id); |
| 263 | |
| 264 | if (count($stored) !== 3) |
| 265 | { |
| 266 | return FALSE; |
| 267 | } |
| 268 | |
Greg Aker | 999e747 | 2011-01-29 16:16:58 -0600 | [diff] [blame] | 269 | list($data, $time, $ttl) = $stored; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 270 | |
| 271 | return array( |
| 272 | 'expire' => $time + $ttl, |
| 273 | 'mtime' => $time, |
| 274 | 'data' => $data |
| 275 | ); |
| 276 | } |
| 277 | |
| 278 | // ------------------------------------------------------------------------ |
| 279 | |
| 280 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 281 | * Is supported |
| 282 | * |
| 283 | * Returns FALSE if memcached is not supported on the system. |
| 284 | * If it is, we setup the memcached object & return TRUE |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 285 | * |
| 286 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 287 | */ |
| 288 | public function is_supported() |
| 289 | { |
Andrey Andreev | 24a4a6a | 2015-08-31 15:11:47 +0300 | [diff] [blame] | 290 | return (extension_loaded('memcached') OR extension_loaded('memcache')); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 291 | } |
Andrey Andreev | 522c3f3 | 2016-03-12 12:13:43 +0200 | [diff] [blame] | 292 | |
| 293 | // ------------------------------------------------------------------------ |
| 294 | |
| 295 | /** |
| 296 | * Class destructor |
| 297 | * |
| 298 | * Closes the connection to Memcache(d) if present. |
| 299 | * |
| 300 | * @return void |
| 301 | */ |
| 302 | public function __destruct() |
| 303 | { |
| 304 | if ($this->_memcached instanceof Memcache) |
| 305 | { |
| 306 | $this->_memcached->close(); |
| 307 | } |
Andrey Andreev | f8312cf | 2016-04-01 21:11:23 +0300 | [diff] [blame] | 308 | elseif ($this->_memcached instanceof Memcached && method_exists($this->_memcached, 'quit')) |
Andrey Andreev | 522c3f3 | 2016-03-12 12:13:43 +0200 | [diff] [blame] | 309 | { |
| 310 | $this->_memcached->quit(); |
| 311 | } |
| 312 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 313 | } |