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