blob: 78cab25d48bd50944b31a5bd4c672b9d4c11b8e2 [file] [log] [blame]
Derek Jonesf4a4bd82011-10-20 12:18:42 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Greg Akerbde25d92010-12-21 09:31:21 -06002/**
3 * CodeIgniter
4 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05005 * 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 Akerbde25d92010-12-21 09:31:21 -060018 *
19 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Derek Jones700205a2011-01-28 07:44:28 -060021 * @copyright Copyright (c) 2006 - 2011 EllisLab, Inc.
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Greg Akerbde25d92010-12-21 09:31:21 -060023 * @link http://codeigniter.com
24 * @since Version 2.0
Greg Aker151b7a92011-08-21 12:29:43 -050025 * @filesource
Greg Akerbde25d92010-12-21 09:31:21 -060026 */
27
28// ------------------------------------------------------------------------
29
30/**
Greg Aker151b7a92011-08-21 12:29:43 -050031 * CodeIgniter Memcached Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060032 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Greg Aker151b7a92011-08-21 12:29:43 -050037 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060038 */
39
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010040class CI_Cache_memcached extends CI_Driver {
Greg Akerbde25d92010-12-21 09:31:21 -060041
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 Aker151b7a92011-08-21 12:29:43 -050052 // ------------------------------------------------------------------------
Greg Akerbde25d92010-12-21 09:31:21 -060053
54 /**
55 * Fetch from cache
56 *
57 * @param mixed unique key id
58 * @return mixed data on success/false on failure
Greg Aker151b7a92011-08-21 12:29:43 -050059 */
Greg Akerbde25d92010-12-21 09:31:21 -060060 public function get($id)
Greg Aker151b7a92011-08-21 12:29:43 -050061 {
Greg Akerbde25d92010-12-21 09:31:21 -060062 $data = $this->_memcached->get($id);
Greg Aker151b7a92011-08-21 12:29:43 -050063
Greg Akerbde25d92010-12-21 09:31:21 -060064 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 Huotba00e9f2011-09-23 08:20:29 -040079 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 Akerbde25d92010-12-21 09:31:21 -060089 }
90
91 // ------------------------------------------------------------------------
Greg Aker151b7a92011-08-21 12:29:43 -050092
Greg Akerbde25d92010-12-21 09:31:21 -060093 /**
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 Aker151b7a92011-08-21 12:29:43 -0500105
Greg Akerbde25d92010-12-21 09:31:21 -0600106 /**
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 Aker151b7a92011-08-21 12:29:43 -0500130
Greg Akerbde25d92010-12-21 09:31:21 -0600131 /**
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 Aker999e7472011-01-29 16:16:58 -0600146 list($data, $time, $ttl) = $stored;
Greg Akerbde25d92010-12-21 09:31:21 -0600147
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 Aker151b7a92011-08-21 12:29:43 -0500164
Greg Akerbde25d92010-12-21 09:31:21 -0600165 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 Aker151b7a92011-08-21 12:29:43 -0500174 }
175 }
Greg Akerbde25d92010-12-21 09:31:21 -0600176 }
John Bellonecbb81c62011-08-21 09:12:33 -0400177
Greg Aker151b7a92011-08-21 12:29:43 -0500178 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 Bellone9e8dc0e2011-08-21 08:54:56 -0400189
Greg Aker151b7a92011-08-21 12:29:43 -0500190 return FALSE;
191 }
Greg Akerbde25d92010-12-21 09:31:21 -0600192
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 Aker151b7a92011-08-21 12:29:43 -0500199
Greg Akerbde25d92010-12-21 09:31:21 -0600200 if ( ! array_key_exists('port', $cache_server))
201 {
202 $cache_server['port'] = $this->_default_options['default_port'];
203 }
Greg Aker151b7a92011-08-21 12:29:43 -0500204
Greg Akerbde25d92010-12-21 09:31:21 -0600205 if ( ! array_key_exists('weight', $cache_server))
206 {
207 $cache_server['weight'] = $this->_default_options['default_weight'];
208 }
Greg Aker151b7a92011-08-21 12:29:43 -0500209
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 Akerbde25d92010-12-21 09:31:21 -0600228 }
John Bellone51758fc2011-08-21 09:38:44 -0400229
Greg Aker151b7a92011-08-21 12:29:43 -0500230 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -0600231 }
232
233 // ------------------------------------------------------------------------
234
Greg Akerbde25d92010-12-21 09:31:21 -0600235 /**
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 Aker151b7a92011-08-21 12:29:43 -0500243 if ( ! extension_loaded('memcached') && ! extension_loaded('memcache'))
Greg Akerbde25d92010-12-21 09:31:21 -0600244 {
245 log_message('error', 'The Memcached Extension must be loaded to use Memcached Cache.');
Greg Aker151b7a92011-08-21 12:29:43 -0500246
Greg Akerbde25d92010-12-21 09:31:21 -0600247 return FALSE;
248 }
John Bellone02d73692011-08-21 09:32:35 -0400249
250 return $this->_setup_memcached();
Greg Akerbde25d92010-12-21 09:31:21 -0600251 }
252
253 // ------------------------------------------------------------------------
254
255}
256// End Class
257
258/* End of file Cache_memcached.php */
259/* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */