blob: 95bdcb35042bc310ec6612d60e813d8c863c17fd [file] [log] [blame]
Greg Akerbde25d92010-12-21 09:31:21 -06001<?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 Jones700205a2011-01-28 07:44:28 -06009 * @copyright Copyright (c) 2006 - 2011 EllisLab, Inc.
Greg Akerbde25d92010-12-21 09:31:21 -060010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 2.0
Greg Aker151b7a92011-08-21 12:29:43 -050013 * @filesource
Greg Akerbde25d92010-12-21 09:31:21 -060014 */
15
16// ------------------------------------------------------------------------
17
18/**
Greg Aker151b7a92011-08-21 12:29:43 -050019 * CodeIgniter Memcached Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060020 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Core
24 * @author ExpressionEngine Dev Team
Greg Aker151b7a92011-08-21 12:29:43 -050025 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060026 */
27
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010028class CI_Cache_memcached extends CI_Driver {
Greg Akerbde25d92010-12-21 09:31:21 -060029
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 Aker151b7a92011-08-21 12:29:43 -050040 // ------------------------------------------------------------------------
Greg Akerbde25d92010-12-21 09:31:21 -060041
42 /**
43 * Fetch from cache
44 *
45 * @param mixed unique key id
46 * @return mixed data on success/false on failure
Greg Aker151b7a92011-08-21 12:29:43 -050047 */
Greg Akerbde25d92010-12-21 09:31:21 -060048 public function get($id)
Greg Aker151b7a92011-08-21 12:29:43 -050049 {
Greg Akerbde25d92010-12-21 09:31:21 -060050 $data = $this->_memcached->get($id);
Greg Aker151b7a92011-08-21 12:29:43 -050051
Greg Akerbde25d92010-12-21 09:31:21 -060052 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 Huotba00e9f2011-09-23 08:20:29 -040067 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 Akerbde25d92010-12-21 09:31:21 -060077 }
78
79 // ------------------------------------------------------------------------
Greg Aker151b7a92011-08-21 12:29:43 -050080
Greg Akerbde25d92010-12-21 09:31:21 -060081 /**
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 Aker151b7a92011-08-21 12:29:43 -050093
Greg Akerbde25d92010-12-21 09:31:21 -060094 /**
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 Aker151b7a92011-08-21 12:29:43 -0500118
Greg Akerbde25d92010-12-21 09:31:21 -0600119 /**
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 Aker999e7472011-01-29 16:16:58 -0600134 list($data, $time, $ttl) = $stored;
Greg Akerbde25d92010-12-21 09:31:21 -0600135
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 Aker151b7a92011-08-21 12:29:43 -0500152
Greg Akerbde25d92010-12-21 09:31:21 -0600153 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 Aker151b7a92011-08-21 12:29:43 -0500162 }
163 }
Greg Akerbde25d92010-12-21 09:31:21 -0600164 }
John Bellonecbb81c62011-08-21 09:12:33 -0400165
Greg Aker151b7a92011-08-21 12:29:43 -0500166 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 Bellone9e8dc0e2011-08-21 08:54:56 -0400177
Greg Aker151b7a92011-08-21 12:29:43 -0500178 return FALSE;
179 }
Greg Akerbde25d92010-12-21 09:31:21 -0600180
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 Aker151b7a92011-08-21 12:29:43 -0500187
Greg Akerbde25d92010-12-21 09:31:21 -0600188 if ( ! array_key_exists('port', $cache_server))
189 {
190 $cache_server['port'] = $this->_default_options['default_port'];
191 }
Greg Aker151b7a92011-08-21 12:29:43 -0500192
Greg Akerbde25d92010-12-21 09:31:21 -0600193 if ( ! array_key_exists('weight', $cache_server))
194 {
195 $cache_server['weight'] = $this->_default_options['default_weight'];
196 }
Greg Aker151b7a92011-08-21 12:29:43 -0500197
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 Akerbde25d92010-12-21 09:31:21 -0600216 }
John Bellone51758fc2011-08-21 09:38:44 -0400217
Greg Aker151b7a92011-08-21 12:29:43 -0500218 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -0600219 }
220
221 // ------------------------------------------------------------------------
222
Greg Akerbde25d92010-12-21 09:31:21 -0600223 /**
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 Aker151b7a92011-08-21 12:29:43 -0500231 if ( ! extension_loaded('memcached') && ! extension_loaded('memcache'))
Greg Akerbde25d92010-12-21 09:31:21 -0600232 {
233 log_message('error', 'The Memcached Extension must be loaded to use Memcached Cache.');
Greg Aker151b7a92011-08-21 12:29:43 -0500234
Greg Akerbde25d92010-12-21 09:31:21 -0600235 return FALSE;
236 }
John Bellone02d73692011-08-21 09:32:35 -0400237
238 return $this->_setup_memcached();
Greg Akerbde25d92010-12-21 09:31:21 -0600239 }
240
241 // ------------------------------------------------------------------------
242
243}
244// End Class
245
246/* End of file Cache_memcached.php */
247/* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */