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