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 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2015, 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 |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2015, 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 |
| 34 | * @link http://codeigniter.com |
| 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 | 25df7a9 | 2014-07-31 13:42:07 +0300 | [diff] [blame] | 63 | protected $_memcache_conf = 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 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 71 | /** |
| 72 | * Fetch from cache |
| 73 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 74 | * @param string $id Cache ID |
| 75 | * @return mixed Data on success, FALSE on failure |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 76 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 77 | public function get($id) |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 78 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 79 | $data = $this->_memcached->get($id); |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 80 | |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 81 | return is_array($data) ? $data[0] : $data; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // ------------------------------------------------------------------------ |
| 85 | |
| 86 | /** |
| 87 | * Save |
| 88 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 89 | * @param string $id Cache ID |
| 90 | * @param mixed $data Data being cached |
| 91 | * @param int $ttl Time to live |
| 92 | * @param bool $raw Whether to store the raw value |
| 93 | * @return bool TRUE on success, FALSE on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 94 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 95 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 96 | { |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 97 | if ($raw !== TRUE) |
| 98 | { |
Andrey Andreev | b2a0e70 | 2014-01-18 16:54:51 +0200 | [diff] [blame] | 99 | $data = array($data, time(), $ttl); |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 102 | if (get_class($this->_memcached) === 'Memcached') |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 103 | { |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 104 | return $this->_memcached->set($id, $data, $ttl); |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 105 | } |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 106 | elseif (get_class($this->_memcached) === 'Memcache') |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 107 | { |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 108 | return $this->_memcached->set($id, $data, 0, $ttl); |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 109 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 110 | |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 111 | return FALSE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // ------------------------------------------------------------------------ |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 115 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 116 | /** |
| 117 | * Delete from Cache |
| 118 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 119 | * @param mixed key to be deleted. |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 120 | * @return bool true on success, false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 121 | */ |
| 122 | public function delete($id) |
| 123 | { |
| 124 | return $this->_memcached->delete($id); |
| 125 | } |
| 126 | |
| 127 | // ------------------------------------------------------------------------ |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 128 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 129 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 130 | * Increment a raw value |
| 131 | * |
| 132 | * @param string $id Cache ID |
| 133 | * @param int $offset Step/value to add |
| 134 | * @return mixed New value on success or FALSE on failure |
| 135 | */ |
| 136 | public function increment($id, $offset = 1) |
| 137 | { |
| 138 | return $this->_memcached->increment($id, $offset); |
| 139 | } |
| 140 | |
| 141 | // ------------------------------------------------------------------------ |
| 142 | |
| 143 | /** |
| 144 | * Decrement a raw value |
| 145 | * |
| 146 | * @param string $id Cache ID |
| 147 | * @param int $offset Step/value to reduce by |
| 148 | * @return mixed New value on success or FALSE on failure |
| 149 | */ |
| 150 | public function decrement($id, $offset = 1) |
| 151 | { |
| 152 | return $this->_memcached->decrement($id, $offset); |
| 153 | } |
| 154 | |
| 155 | // ------------------------------------------------------------------------ |
| 156 | |
| 157 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 158 | * Clean the Cache |
| 159 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 160 | * @return bool false on failure/true on success |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 161 | */ |
| 162 | public function clean() |
| 163 | { |
| 164 | return $this->_memcached->flush(); |
| 165 | } |
| 166 | |
| 167 | // ------------------------------------------------------------------------ |
| 168 | |
| 169 | /** |
| 170 | * Cache Info |
| 171 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 172 | * @return mixed array on success, false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 173 | */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 174 | public function cache_info() |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 175 | { |
| 176 | return $this->_memcached->getStats(); |
| 177 | } |
| 178 | |
| 179 | // ------------------------------------------------------------------------ |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 180 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 181 | /** |
| 182 | * Get Cache Metadata |
| 183 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 184 | * @param mixed key to get cache metadata on |
| 185 | * @return mixed FALSE on failure, array on success. |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 186 | */ |
| 187 | public function get_metadata($id) |
| 188 | { |
| 189 | $stored = $this->_memcached->get($id); |
| 190 | |
| 191 | if (count($stored) !== 3) |
| 192 | { |
| 193 | return FALSE; |
| 194 | } |
| 195 | |
Greg Aker | 999e747 | 2011-01-29 16:16:58 -0600 | [diff] [blame] | 196 | list($data, $time, $ttl) = $stored; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 197 | |
| 198 | return array( |
| 199 | 'expire' => $time + $ttl, |
| 200 | 'mtime' => $time, |
| 201 | 'data' => $data |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | // ------------------------------------------------------------------------ |
| 206 | |
| 207 | /** |
| 208 | * Setup memcached. |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 209 | * |
| 210 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 211 | */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 212 | protected function _setup_memcached() |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 213 | { |
| 214 | // Try to load memcached server info from the config file. |
| 215 | $CI =& get_instance(); |
Andrey Andreev | 25df7a9 | 2014-07-31 13:42:07 +0300 | [diff] [blame] | 216 | $defaults = $this->_memcache_conf['default']; |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 217 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 218 | if ($CI->config->load('memcached', TRUE, TRUE)) |
| 219 | { |
| 220 | if (is_array($CI->config->config['memcached'])) |
| 221 | { |
Andrey Andreev | 6a3d7e5 | 2013-07-18 19:14:05 +0300 | [diff] [blame] | 222 | $this->_memcache_conf = array(); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 223 | |
| 224 | foreach ($CI->config->config['memcached'] as $name => $conf) |
| 225 | { |
| 226 | $this->_memcache_conf[$name] = $conf; |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 227 | } |
| 228 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 229 | } |
John Bellone | cbb81c6 | 2011-08-21 09:12:33 -0400 | [diff] [blame] | 230 | |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 231 | if (class_exists('Memcached', FALSE)) |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 232 | { |
| 233 | $this->_memcached = new Memcached(); |
| 234 | } |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 235 | elseif (class_exists('Memcache', FALSE)) |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 236 | { |
| 237 | $this->_memcached = new Memcache(); |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | log_message('error', 'Failed to create object for Memcached Cache; extension not loaded?'); |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 242 | return FALSE; |
| 243 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 244 | |
ThallisPHP | 08ddfb8 | 2013-05-20 11:10:26 -0300 | [diff] [blame] | 245 | foreach ($this->_memcache_conf as $cache_server) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 246 | { |
Andrey Andreev | 6a3d7e5 | 2013-07-18 19:14:05 +0300 | [diff] [blame] | 247 | isset($cache_server['hostname']) OR $cache_server['hostname'] = $defaults['host']; |
Andrey Andreev | 21f2e06 | 2015-01-04 19:30:31 +0200 | [diff] [blame] | 248 | isset($cache_server['port']) OR $cache_server['port'] = $defaults['port']; |
Andrey Andreev | 6a3d7e5 | 2013-07-18 19:14:05 +0300 | [diff] [blame] | 249 | isset($cache_server['weight']) OR $cache_server['weight'] = $defaults['weight']; |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 250 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 251 | if (get_class($this->_memcached) === 'Memcache') |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 252 | { |
| 253 | // Third parameter is persistance and defaults to TRUE. |
| 254 | $this->_memcached->addServer( |
| 255 | $cache_server['hostname'], |
| 256 | $cache_server['port'], |
| 257 | TRUE, |
| 258 | $cache_server['weight'] |
| 259 | ); |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | $this->_memcached->addServer( |
| 264 | $cache_server['hostname'], |
| 265 | $cache_server['port'], |
| 266 | $cache_server['weight'] |
| 267 | ); |
| 268 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 269 | } |
John Bellone | 51758fc | 2011-08-21 09:38:44 -0400 | [diff] [blame] | 270 | |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 271 | return TRUE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // ------------------------------------------------------------------------ |
| 275 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 276 | /** |
| 277 | * Is supported |
| 278 | * |
| 279 | * Returns FALSE if memcached is not supported on the system. |
| 280 | * If it is, we setup the memcached object & return TRUE |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 281 | * |
| 282 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 283 | */ |
| 284 | public function is_supported() |
| 285 | { |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 286 | if ( ! extension_loaded('memcached') && ! extension_loaded('memcache')) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 287 | { |
Tyler Brownell | d967f72 | 2013-07-29 19:06:52 -0400 | [diff] [blame] | 288 | log_message('debug', 'The Memcached Extension must be loaded to use Memcached Cache.'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 289 | return FALSE; |
| 290 | } |
John Bellone | 02d7369 | 2011-08-21 09:32:35 -0400 | [diff] [blame] | 291 | |
| 292 | return $this->_setup_memcached(); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 293 | } |
| 294 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 295 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 296 | |
| 297 | /* End of file Cache_memcached.php */ |
Andrey Andreev | 2ecc06c | 2013-07-17 12:31:11 +0300 | [diff] [blame] | 298 | /* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */ |