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