Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
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 |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc. |
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 | */ |
| 27 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 28 | /** |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 29 | * CodeIgniter Memcached Caching Class |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Libraries |
| 33 | * @category Core |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 35 | * @link |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 36 | */ |
Phil Sturgeon | eb2dcda | 2011-04-02 14:44:58 +0100 | [diff] [blame] | 37 | class CI_Cache_memcached extends CI_Driver { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 38 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 39 | /** |
| 40 | * Holds the memcached object |
| 41 | * |
| 42 | * @var object |
| 43 | */ |
| 44 | protected $_memcached; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 45 | |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 46 | /** |
| 47 | * Memcached configuration |
| 48 | * |
| 49 | * @var array |
| 50 | */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 51 | protected $_memcache_conf = array( |
Timothy Warren | 0688ac9 | 2012-04-20 10:25:04 -0400 | [diff] [blame] | 52 | 'default' => array( |
| 53 | 'default_host' => '127.0.0.1', |
| 54 | 'default_port' => 11211, |
| 55 | 'default_weight' => 1 |
| 56 | ) |
| 57 | ); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 58 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 59 | /** |
| 60 | * Fetch from cache |
| 61 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 62 | * @param mixed unique key id |
| 63 | * @return mixed data on success/false on failure |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 64 | */ |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 65 | public function get($id) |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 66 | { |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 67 | $data = $this->_memcached->get($id); |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 68 | |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 69 | return is_array($data) ? $data[0] : FALSE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // ------------------------------------------------------------------------ |
| 73 | |
| 74 | /** |
| 75 | * Save |
| 76 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 77 | * @param string unique identifier |
| 78 | * @param mixed data being cached |
| 79 | * @param int time to live |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame^] | 80 | * @return bool true on success, false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 81 | */ |
| 82 | public function save($id, $data, $ttl = 60) |
| 83 | { |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 84 | if (get_class($this->_memcached) === 'Memcached') |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 85 | { |
| 86 | return $this->_memcached->set($id, array($data, time(), $ttl), $ttl); |
| 87 | } |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 88 | elseif (get_class($this->_memcached) === 'Memcache') |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 89 | { |
| 90 | return $this->_memcached->set($id, array($data, time(), $ttl), 0, $ttl); |
| 91 | } |
Andrey Andreev | 7d4ea07 | 2011-12-25 19:23:50 +0200 | [diff] [blame] | 92 | |
Mark Huot | ba00e9f | 2011-09-23 08:20:29 -0400 | [diff] [blame] | 93 | return FALSE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // ------------------------------------------------------------------------ |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 97 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 98 | /** |
| 99 | * Delete from Cache |
| 100 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 101 | * @param mixed key to be deleted. |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame^] | 102 | * @return bool true on success, false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 103 | */ |
| 104 | public function delete($id) |
| 105 | { |
| 106 | return $this->_memcached->delete($id); |
| 107 | } |
| 108 | |
| 109 | // ------------------------------------------------------------------------ |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 110 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 111 | /** |
| 112 | * Clean the Cache |
| 113 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 114 | * @return bool false on failure/true on success |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 115 | */ |
| 116 | public function clean() |
| 117 | { |
| 118 | return $this->_memcached->flush(); |
| 119 | } |
| 120 | |
| 121 | // ------------------------------------------------------------------------ |
| 122 | |
| 123 | /** |
| 124 | * Cache Info |
| 125 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 126 | * @return mixed array on success, false on failure |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 127 | */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 128 | public function cache_info() |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 129 | { |
| 130 | return $this->_memcached->getStats(); |
| 131 | } |
| 132 | |
| 133 | // ------------------------------------------------------------------------ |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 134 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 135 | /** |
| 136 | * Get Cache Metadata |
| 137 | * |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 138 | * @param mixed key to get cache metadata on |
| 139 | * @return mixed FALSE on failure, array on success. |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 140 | */ |
| 141 | public function get_metadata($id) |
| 142 | { |
| 143 | $stored = $this->_memcached->get($id); |
| 144 | |
| 145 | if (count($stored) !== 3) |
| 146 | { |
| 147 | return FALSE; |
| 148 | } |
| 149 | |
Greg Aker | 999e747 | 2011-01-29 16:16:58 -0600 | [diff] [blame] | 150 | list($data, $time, $ttl) = $stored; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 151 | |
| 152 | return array( |
| 153 | 'expire' => $time + $ttl, |
| 154 | 'mtime' => $time, |
| 155 | 'data' => $data |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | // ------------------------------------------------------------------------ |
| 160 | |
| 161 | /** |
| 162 | * Setup memcached. |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 163 | * |
| 164 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 165 | */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 166 | protected function _setup_memcached() |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 167 | { |
| 168 | // Try to load memcached server info from the config file. |
| 169 | $CI =& get_instance(); |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 170 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 171 | if ($CI->config->load('memcached', TRUE, TRUE)) |
| 172 | { |
| 173 | if (is_array($CI->config->config['memcached'])) |
| 174 | { |
| 175 | $this->_memcache_conf = NULL; |
| 176 | |
| 177 | foreach ($CI->config->config['memcached'] as $name => $conf) |
| 178 | { |
| 179 | $this->_memcache_conf[$name] = $conf; |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 180 | } |
| 181 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 182 | } |
John Bellone | cbb81c6 | 2011-08-21 09:12:33 -0400 | [diff] [blame] | 183 | |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 184 | if (class_exists('Memcached')) |
| 185 | { |
| 186 | $this->_memcached = new Memcached(); |
| 187 | } |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 188 | elseif (class_exists('Memcache')) |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 189 | { |
| 190 | $this->_memcached = new Memcache(); |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | 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] | 195 | return FALSE; |
| 196 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 197 | |
| 198 | foreach ($this->_memcache_conf as $name => $cache_server) |
| 199 | { |
| 200 | if ( ! array_key_exists('hostname', $cache_server)) |
| 201 | { |
Ryan Neufeld | f1bbb17 | 2012-03-29 08:24:01 -0700 | [diff] [blame] | 202 | $cache_server['hostname'] = $this->_memcache_conf['default']['default_host']; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 203 | } |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 204 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 205 | if ( ! array_key_exists('port', $cache_server)) |
| 206 | { |
Ryan Neufeld | f1bbb17 | 2012-03-29 08:24:01 -0700 | [diff] [blame] | 207 | $cache_server['port'] = $this->_memcache_conf['default']['default_port']; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 208 | } |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 209 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 210 | if ( ! array_key_exists('weight', $cache_server)) |
| 211 | { |
Ryan Neufeld | f1bbb17 | 2012-03-29 08:24:01 -0700 | [diff] [blame] | 212 | $cache_server['weight'] = $this->_memcache_conf['default']['default_weight']; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 213 | } |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 214 | |
| 215 | if (get_class($this->_memcached) == 'Memcache') |
| 216 | { |
| 217 | // Third parameter is persistance and defaults to TRUE. |
| 218 | $this->_memcached->addServer( |
| 219 | $cache_server['hostname'], |
| 220 | $cache_server['port'], |
| 221 | TRUE, |
| 222 | $cache_server['weight'] |
| 223 | ); |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | $this->_memcached->addServer( |
| 228 | $cache_server['hostname'], |
| 229 | $cache_server['port'], |
| 230 | $cache_server['weight'] |
| 231 | ); |
| 232 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 233 | } |
John Bellone | 51758fc | 2011-08-21 09:38:44 -0400 | [diff] [blame] | 234 | |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 235 | return TRUE; |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // ------------------------------------------------------------------------ |
| 239 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 240 | /** |
| 241 | * Is supported |
| 242 | * |
| 243 | * Returns FALSE if memcached is not supported on the system. |
| 244 | * If it is, we setup the memcached object & return TRUE |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 245 | * |
| 246 | * @return bool |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 247 | */ |
| 248 | public function is_supported() |
| 249 | { |
Greg Aker | 151b7a9 | 2011-08-21 12:29:43 -0500 | [diff] [blame] | 250 | if ( ! extension_loaded('memcached') && ! extension_loaded('memcache')) |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 251 | { |
| 252 | log_message('error', 'The Memcached Extension must be loaded to use Memcached Cache.'); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 253 | return FALSE; |
| 254 | } |
John Bellone | 02d7369 | 2011-08-21 09:32:35 -0400 | [diff] [blame] | 255 | |
| 256 | return $this->_setup_memcached(); |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 257 | } |
| 258 | |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 259 | } |
Greg Aker | bde25d9 | 2010-12-21 09:31:21 -0600 | [diff] [blame] | 260 | |
| 261 | /* End of file Cache_memcached.php */ |
Andrey Andreev | b24b033 | 2012-03-26 15:34:39 +0300 | [diff] [blame] | 262 | /* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */ |