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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 6 | * |
| 7 | * NOTICE OF LICENSE |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 18 | * |
| 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Andrey Andreev | 80500af | 2013-01-01 08:16:53 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 2.0 |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 25 | * @filesource |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 28 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 29 | /** |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 30 | * CodeIgniter Memcached Caching Class |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Libraries |
| 34 | * @category Core |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 36 | * @link |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 37 | */ |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 38 | class CI_Cache_memcached extends CI_Driver { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 39 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 40 | /** |
| 41 | * Holds the memcached object |
| 42 | * |
| 43 | * @var object |
| 44 | */ |
| 45 | protected $_memcached; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 46 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 47 | /** |
| 48 | * Memcached configuration |
| 49 | * |
| 50 | * @var array |
| 51 | */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 52 | protected $_memcache_conf = array( |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 53 | 'default' => array( |
Andrey Andreev | 6a3d7e5 | 2013-07-18 19:14:05 +0300 | [diff] [blame] | 54 | 'host' => '127.0.0.1', |
| 55 | 'port' => 11211, |
| 56 | 'weight' => 1 |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 57 | ) |
| 58 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 59 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 60 | /** |
| 61 | * Fetch from cache |
| 62 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 63 | * @param string $id Cache ID |
| 64 | * @return mixed Data on success, FALSE on failure |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 65 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 66 | public function get($id) |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 67 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 68 | $data = $this->_memcached->get($id); |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 69 | |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 70 | return is_array($data) ? $data[0] : $data; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // ------------------------------------------------------------------------ |
| 74 | |
| 75 | /** |
| 76 | * Save |
| 77 | * |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 78 | * @param string $id Cache ID |
| 79 | * @param mixed $data Data being cached |
| 80 | * @param int $ttl Time to live |
| 81 | * @param bool $raw Whether to store the raw value |
| 82 | * @return bool TRUE on success, FALSE on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 83 | */ |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 84 | public function save($id, $data, $ttl = 60, $raw = FALSE) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 85 | { |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 86 | if ($raw !== TRUE) |
| 87 | { |
Andrey Andreev | b2a0e70 | 2014-01-18 16:54:51 +0200 | [diff] [blame^] | 88 | $data = array($data, time(), $ttl); |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 91 | if (get_class($this->_memcached) === 'Memcached') |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 92 | { |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 93 | return $this->_memcached->set($id, $data, $ttl); |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 94 | } |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 95 | elseif (get_class($this->_memcached) === 'Memcache') |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 96 | { |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 97 | return $this->_memcached->set($id, $data, 0, $ttl); |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 98 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 99 | |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 100 | return FALSE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // ------------------------------------------------------------------------ |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 104 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 105 | /** |
| 106 | * Delete from Cache |
| 107 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 108 | * @param mixed key to be deleted. |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 109 | * @return bool true on success, false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 110 | */ |
| 111 | public function delete($id) |
| 112 | { |
| 113 | return $this->_memcached->delete($id); |
| 114 | } |
| 115 | |
| 116 | // ------------------------------------------------------------------------ |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 117 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 118 | /** |
Andrey Andreev | 43d7fa7 | 2014-01-09 17:29:45 +0200 | [diff] [blame] | 119 | * Increment a raw value |
| 120 | * |
| 121 | * @param string $id Cache ID |
| 122 | * @param int $offset Step/value to add |
| 123 | * @return mixed New value on success or FALSE on failure |
| 124 | */ |
| 125 | public function increment($id, $offset = 1) |
| 126 | { |
| 127 | return $this->_memcached->increment($id, $offset); |
| 128 | } |
| 129 | |
| 130 | // ------------------------------------------------------------------------ |
| 131 | |
| 132 | /** |
| 133 | * Decrement a raw value |
| 134 | * |
| 135 | * @param string $id Cache ID |
| 136 | * @param int $offset Step/value to reduce by |
| 137 | * @return mixed New value on success or FALSE on failure |
| 138 | */ |
| 139 | public function decrement($id, $offset = 1) |
| 140 | { |
| 141 | return $this->_memcached->decrement($id, $offset); |
| 142 | } |
| 143 | |
| 144 | // ------------------------------------------------------------------------ |
| 145 | |
| 146 | /** |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 147 | * Clean the Cache |
| 148 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 149 | * @return bool false on failure/true on success |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 150 | */ |
| 151 | public function clean() |
| 152 | { |
| 153 | return $this->_memcached->flush(); |
| 154 | } |
| 155 | |
| 156 | // ------------------------------------------------------------------------ |
| 157 | |
| 158 | /** |
| 159 | * Cache Info |
| 160 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 161 | * @return mixed array on success, false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 162 | */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 163 | public function cache_info() |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 164 | { |
| 165 | return $this->_memcached->getStats(); |
| 166 | } |
| 167 | |
| 168 | // ------------------------------------------------------------------------ |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 169 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 170 | /** |
| 171 | * Get Cache Metadata |
| 172 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 173 | * @param mixed key to get cache metadata on |
| 174 | * @return mixed FALSE on failure, array on success. |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 175 | */ |
| 176 | public function get_metadata($id) |
| 177 | { |
| 178 | $stored = $this->_memcached->get($id); |
| 179 | |
| 180 | if (count($stored) !== 3) |
| 181 | { |
| 182 | return FALSE; |
| 183 | } |
| 184 | |
Greg Aker | 999e747 | 2011-01-29 16:16:58 -0600 | [diff] [blame] | 185 | list($data, $time, $ttl) = $stored; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 186 | |
| 187 | return array( |
| 188 | 'expire' => $time + $ttl, |
| 189 | 'mtime' => $time, |
| 190 | 'data' => $data |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | // ------------------------------------------------------------------------ |
| 195 | |
| 196 | /** |
| 197 | * Setup memcached. |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 198 | * |
| 199 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 200 | */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 201 | protected function _setup_memcached() |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 202 | { |
| 203 | // Try to load memcached server info from the config file. |
| 204 | $CI =& get_instance(); |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 205 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 206 | if ($CI->config->load('memcached', TRUE, TRUE)) |
| 207 | { |
| 208 | if (is_array($CI->config->config['memcached'])) |
| 209 | { |
Andrey Andreev | 6a3d7e5 | 2013-07-18 19:14:05 +0300 | [diff] [blame] | 210 | $defaults = $this->_memcache_conf['default']; |
| 211 | $this->_memcache_conf = array(); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 212 | |
| 213 | foreach ($CI->config->config['memcached'] as $name => $conf) |
| 214 | { |
| 215 | $this->_memcache_conf[$name] = $conf; |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 216 | } |
| 217 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 218 | } |
John Bellone | cbb81c6 | 2011-08-21 09:12:33 -0400 | [diff] [blame] | 219 | |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 220 | if (class_exists('Memcached', FALSE)) |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 221 | { |
| 222 | $this->_memcached = new Memcached(); |
| 223 | } |
Andrey Andreev | 49e68de | 2013-02-21 16:30:55 +0200 | [diff] [blame] | 224 | elseif (class_exists('Memcache', FALSE)) |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 225 | { |
| 226 | $this->_memcached = new Memcache(); |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | 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] | 231 | return FALSE; |
| 232 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 233 | |
ThallisPHP | 08ddfb8 | 2013-05-20 11:10:26 -0300 | [diff] [blame] | 234 | foreach ($this->_memcache_conf as $cache_server) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 235 | { |
Andrey Andreev | 6a3d7e5 | 2013-07-18 19:14:05 +0300 | [diff] [blame] | 236 | isset($cache_server['hostname']) OR $cache_server['hostname'] = $defaults['host']; |
| 237 | isset($cache_server['port']) OR $cache_server['port'] = $defaults['host']; |
| 238 | isset($cache_server['weight']) OR $cache_server['weight'] = $defaults['weight']; |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 239 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 240 | if (get_class($this->_memcached) === 'Memcache') |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 241 | { |
| 242 | // Third parameter is persistance and defaults to TRUE. |
| 243 | $this->_memcached->addServer( |
| 244 | $cache_server['hostname'], |
| 245 | $cache_server['port'], |
| 246 | TRUE, |
| 247 | $cache_server['weight'] |
| 248 | ); |
| 249 | } |
| 250 | else |
| 251 | { |
| 252 | $this->_memcached->addServer( |
| 253 | $cache_server['hostname'], |
| 254 | $cache_server['port'], |
| 255 | $cache_server['weight'] |
| 256 | ); |
| 257 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 258 | } |
John Bellone | 51758fc | 2011-08-21 09:38:44 -0400 | [diff] [blame] | 259 | |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 260 | return TRUE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | // ------------------------------------------------------------------------ |
| 264 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 265 | /** |
| 266 | * Is supported |
| 267 | * |
| 268 | * Returns FALSE if memcached is not supported on the system. |
| 269 | * If it is, we setup the memcached object & return TRUE |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 270 | * |
| 271 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 272 | */ |
| 273 | public function is_supported() |
| 274 | { |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 275 | if ( ! extension_loaded('memcached') && ! extension_loaded('memcache')) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 276 | { |
Tyler Brownell | d967f72 | 2013-07-29 19:06:52 -0400 | [diff] [blame] | 277 | 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] | 278 | return FALSE; |
| 279 | } |
John Bellone | 02d7369 | 2011-08-21 09:32:35 -0400 | [diff] [blame] | 280 | |
| 281 | return $this->_setup_memcached(); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 282 | } |
| 283 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 284 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 285 | |
| 286 | /* End of file Cache_memcached.php */ |
Andrey Andreev | 2ecc06c | 2013-07-17 12:31:11 +0300 | [diff] [blame] | 287 | /* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */ |