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