blob: fae00234769451b2a260c53a6d4404ca8a8c72cc [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Greg Akerbde25d92010-12-21 09:31:21 -06002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Jonesf4a4bd82011-10-20 12:18:42 -05006 *
7 * NOTICE OF LICENSE
Andrey Andreev7d4ea072011-12-25 19:23:50 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev7d4ea072011-12-25 19:23:50 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Andrey Andreevc5536aa2012-11-01 17:33:58 +020021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Greg Akerbde25d92010-12-21 09:31:21 -060028
Greg Akerbde25d92010-12-21 09:31:21 -060029/**
Greg Aker151b7a92011-08-21 12:29:43 -050030 * CodeIgniter Memcached Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060031 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Greg Aker151b7a92011-08-21 12:29:43 -050036 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060037 */
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010038class CI_Cache_memcached extends CI_Driver {
Greg Akerbde25d92010-12-21 09:31:21 -060039
Timothy Warren0688ac92012-04-20 10:25:04 -040040 /**
41 * Holds the memcached object
42 *
43 * @var object
44 */
45 protected $_memcached;
Greg Akerbde25d92010-12-21 09:31:21 -060046
Timothy Warren0688ac92012-04-20 10:25:04 -040047 /**
48 * Memcached configuration
49 *
50 * @var array
51 */
Andrey Andreevb24b0332012-03-26 15:34:39 +030052 protected $_memcache_conf = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040053 'default' => array(
54 'default_host' => '127.0.0.1',
55 'default_port' => 11211,
56 'default_weight' => 1
57 )
58 );
Greg Akerbde25d92010-12-21 09:31:21 -060059
Greg Akerbde25d92010-12-21 09:31:21 -060060 /**
61 * Fetch from cache
62 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030063 * @param mixed unique key id
64 * @return mixed data on success/false on failure
Greg Aker151b7a92011-08-21 12:29:43 -050065 */
Greg Akerbde25d92010-12-21 09:31:21 -060066 public function get($id)
Greg Aker151b7a92011-08-21 12:29:43 -050067 {
Greg Akerbde25d92010-12-21 09:31:21 -060068 $data = $this->_memcached->get($id);
Greg Aker151b7a92011-08-21 12:29:43 -050069
Andrey Andreevb24b0332012-03-26 15:34:39 +030070 return is_array($data) ? $data[0] : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -060071 }
72
73 // ------------------------------------------------------------------------
74
75 /**
76 * Save
77 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030078 * @param string unique identifier
79 * @param mixed data being cached
80 * @param int time to live
Andrey Andreev56454792012-05-17 14:32:19 +030081 * @return bool true on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060082 */
83 public function save($id, $data, $ttl = 60)
84 {
Andrey Andreevb24b0332012-03-26 15:34:39 +030085 if (get_class($this->_memcached) === 'Memcached')
Mark Huotba00e9f2011-09-23 08:20:29 -040086 {
87 return $this->_memcached->set($id, array($data, time(), $ttl), $ttl);
88 }
Andrey Andreevb24b0332012-03-26 15:34:39 +030089 elseif (get_class($this->_memcached) === 'Memcache')
Mark Huotba00e9f2011-09-23 08:20:29 -040090 {
91 return $this->_memcached->set($id, array($data, time(), $ttl), 0, $ttl);
92 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020093
Mark Huotba00e9f2011-09-23 08:20:29 -040094 return FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -060095 }
96
97 // ------------------------------------------------------------------------
Greg Aker151b7a92011-08-21 12:29:43 -050098
Greg Akerbde25d92010-12-21 09:31:21 -060099 /**
100 * Delete from Cache
101 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300102 * @param mixed key to be deleted.
Andrey Andreev56454792012-05-17 14:32:19 +0300103 * @return bool true on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600104 */
105 public function delete($id)
106 {
107 return $this->_memcached->delete($id);
108 }
109
110 // ------------------------------------------------------------------------
Greg Aker151b7a92011-08-21 12:29:43 -0500111
Greg Akerbde25d92010-12-21 09:31:21 -0600112 /**
113 * Clean the Cache
114 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300115 * @return bool false on failure/true on success
Greg Akerbde25d92010-12-21 09:31:21 -0600116 */
117 public function clean()
118 {
119 return $this->_memcached->flush();
120 }
121
122 // ------------------------------------------------------------------------
123
124 /**
125 * Cache Info
126 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300127 * @return mixed array on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600128 */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300129 public function cache_info()
Greg Akerbde25d92010-12-21 09:31:21 -0600130 {
131 return $this->_memcached->getStats();
132 }
133
134 // ------------------------------------------------------------------------
Greg Aker151b7a92011-08-21 12:29:43 -0500135
Greg Akerbde25d92010-12-21 09:31:21 -0600136 /**
137 * Get Cache Metadata
138 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300139 * @param mixed key to get cache metadata on
140 * @return mixed FALSE on failure, array on success.
Greg Akerbde25d92010-12-21 09:31:21 -0600141 */
142 public function get_metadata($id)
143 {
144 $stored = $this->_memcached->get($id);
145
146 if (count($stored) !== 3)
147 {
148 return FALSE;
149 }
150
Greg Aker999e7472011-01-29 16:16:58 -0600151 list($data, $time, $ttl) = $stored;
Greg Akerbde25d92010-12-21 09:31:21 -0600152
153 return array(
154 'expire' => $time + $ttl,
155 'mtime' => $time,
156 'data' => $data
157 );
158 }
159
160 // ------------------------------------------------------------------------
161
162 /**
163 * Setup memcached.
Andrey Andreevb24b0332012-03-26 15:34:39 +0300164 *
165 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600166 */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300167 protected function _setup_memcached()
Greg Akerbde25d92010-12-21 09:31:21 -0600168 {
169 // Try to load memcached server info from the config file.
170 $CI =& get_instance();
Greg Aker151b7a92011-08-21 12:29:43 -0500171
Greg Akerbde25d92010-12-21 09:31:21 -0600172 if ($CI->config->load('memcached', TRUE, TRUE))
173 {
174 if (is_array($CI->config->config['memcached']))
175 {
176 $this->_memcache_conf = NULL;
177
178 foreach ($CI->config->config['memcached'] as $name => $conf)
179 {
180 $this->_memcache_conf[$name] = $conf;
Greg Aker151b7a92011-08-21 12:29:43 -0500181 }
182 }
Greg Akerbde25d92010-12-21 09:31:21 -0600183 }
John Bellonecbb81c62011-08-21 09:12:33 -0400184
Greg Aker151b7a92011-08-21 12:29:43 -0500185 if (class_exists('Memcached'))
186 {
187 $this->_memcached = new Memcached();
188 }
Andrey Andreevb24b0332012-03-26 15:34:39 +0300189 elseif (class_exists('Memcache'))
Greg Aker151b7a92011-08-21 12:29:43 -0500190 {
191 $this->_memcached = new Memcache();
192 }
193 else
194 {
195 log_message('error', 'Failed to create object for Memcached Cache; extension not loaded?');
Greg Aker151b7a92011-08-21 12:29:43 -0500196 return FALSE;
197 }
Greg Akerbde25d92010-12-21 09:31:21 -0600198
199 foreach ($this->_memcache_conf as $name => $cache_server)
200 {
201 if ( ! array_key_exists('hostname', $cache_server))
202 {
Ryan Neufeldf1bbb172012-03-29 08:24:01 -0700203 $cache_server['hostname'] = $this->_memcache_conf['default']['default_host'];
Greg Akerbde25d92010-12-21 09:31:21 -0600204 }
Greg Aker151b7a92011-08-21 12:29:43 -0500205
Greg Akerbde25d92010-12-21 09:31:21 -0600206 if ( ! array_key_exists('port', $cache_server))
207 {
Ryan Neufeldf1bbb172012-03-29 08:24:01 -0700208 $cache_server['port'] = $this->_memcache_conf['default']['default_port'];
Greg Akerbde25d92010-12-21 09:31:21 -0600209 }
Greg Aker151b7a92011-08-21 12:29:43 -0500210
Greg Akerbde25d92010-12-21 09:31:21 -0600211 if ( ! array_key_exists('weight', $cache_server))
212 {
Ryan Neufeldf1bbb172012-03-29 08:24:01 -0700213 $cache_server['weight'] = $this->_memcache_conf['default']['default_weight'];
Greg Akerbde25d92010-12-21 09:31:21 -0600214 }
Greg Aker151b7a92011-08-21 12:29:43 -0500215
Alex Bilbied261b1e2012-06-02 11:12:16 +0100216 if (get_class($this->_memcached) === 'Memcache')
Greg Aker151b7a92011-08-21 12:29:43 -0500217 {
218 // Third parameter is persistance and defaults to TRUE.
219 $this->_memcached->addServer(
220 $cache_server['hostname'],
221 $cache_server['port'],
222 TRUE,
223 $cache_server['weight']
224 );
225 }
226 else
227 {
228 $this->_memcached->addServer(
229 $cache_server['hostname'],
230 $cache_server['port'],
231 $cache_server['weight']
232 );
233 }
Greg Akerbde25d92010-12-21 09:31:21 -0600234 }
John Bellone51758fc2011-08-21 09:38:44 -0400235
Greg Aker151b7a92011-08-21 12:29:43 -0500236 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -0600237 }
238
239 // ------------------------------------------------------------------------
240
Greg Akerbde25d92010-12-21 09:31:21 -0600241 /**
242 * Is supported
243 *
244 * Returns FALSE if memcached is not supported on the system.
245 * If it is, we setup the memcached object & return TRUE
Andrey Andreevb24b0332012-03-26 15:34:39 +0300246 *
247 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600248 */
249 public function is_supported()
250 {
Greg Aker151b7a92011-08-21 12:29:43 -0500251 if ( ! extension_loaded('memcached') && ! extension_loaded('memcache'))
Greg Akerbde25d92010-12-21 09:31:21 -0600252 {
253 log_message('error', 'The Memcached Extension must be loaded to use Memcached Cache.');
Greg Akerbde25d92010-12-21 09:31:21 -0600254 return FALSE;
255 }
John Bellone02d73692011-08-21 09:32:35 -0400256
257 return $this->_setup_memcached();
Greg Akerbde25d92010-12-21 09:31:21 -0600258 }
259
Greg Akerbde25d92010-12-21 09:31:21 -0600260}
Greg Akerbde25d92010-12-21 09:31:21 -0600261
262/* End of file Cache_memcached.php */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300263/* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */