blob: 1028c8fd522f31a95bb1d27e79c5e67cb8d2ae15 [file] [log] [blame]
Andrey Andreev7d4ea072011-12-25 19:23:50 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2006 - 2012 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
Greg Akerbde25d92010-12-21 09:31:21 -060028/**
Greg Aker151b7a92011-08-21 12:29:43 -050029 * CodeIgniter Memcached Caching Class
Greg Akerbde25d92010-12-21 09:31:21 -060030 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Core
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Greg Aker151b7a92011-08-21 12:29:43 -050035 * @link
Greg Akerbde25d92010-12-21 09:31:21 -060036 */
Phil Sturgeoneb2dcda2011-04-02 14:44:58 +010037class CI_Cache_memcached extends CI_Driver {
Greg Akerbde25d92010-12-21 09:31:21 -060038
Andrey Andreevb24b0332012-03-26 15:34:39 +030039 protected $_memcached; // Holds the memcached object
Greg Akerbde25d92010-12-21 09:31:21 -060040
Andrey Andreevb24b0332012-03-26 15:34:39 +030041 protected $_memcache_conf = array(
Greg Akerbde25d92010-12-21 09:31:21 -060042 'default' => array(
43 'default_host' => '127.0.0.1',
44 'default_port' => 11211,
45 'default_weight' => 1
46 )
47 );
48
Greg Akerbde25d92010-12-21 09:31:21 -060049 /**
50 * Fetch from cache
51 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030052 * @param mixed unique key id
53 * @return mixed data on success/false on failure
Greg Aker151b7a92011-08-21 12:29:43 -050054 */
Greg Akerbde25d92010-12-21 09:31:21 -060055 public function get($id)
Greg Aker151b7a92011-08-21 12:29:43 -050056 {
Greg Akerbde25d92010-12-21 09:31:21 -060057 $data = $this->_memcached->get($id);
Greg Aker151b7a92011-08-21 12:29:43 -050058
Andrey Andreevb24b0332012-03-26 15:34:39 +030059 return is_array($data) ? $data[0] : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -060060 }
61
62 // ------------------------------------------------------------------------
63
64 /**
65 * Save
66 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030067 * @param string unique identifier
68 * @param mixed data being cached
69 * @param int time to live
70 * @return bool true on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060071 */
72 public function save($id, $data, $ttl = 60)
73 {
Andrey Andreevb24b0332012-03-26 15:34:39 +030074 if (get_class($this->_memcached) === 'Memcached')
Mark Huotba00e9f2011-09-23 08:20:29 -040075 {
76 return $this->_memcached->set($id, array($data, time(), $ttl), $ttl);
77 }
Andrey Andreevb24b0332012-03-26 15:34:39 +030078 elseif (get_class($this->_memcached) === 'Memcache')
Mark Huotba00e9f2011-09-23 08:20:29 -040079 {
80 return $this->_memcached->set($id, array($data, time(), $ttl), 0, $ttl);
81 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020082
Mark Huotba00e9f2011-09-23 08:20:29 -040083 return FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -060084 }
85
86 // ------------------------------------------------------------------------
Greg Aker151b7a92011-08-21 12:29:43 -050087
Greg Akerbde25d92010-12-21 09:31:21 -060088 /**
89 * Delete from Cache
90 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030091 * @param mixed key to be deleted.
92 * @return bool true on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060093 */
94 public function delete($id)
95 {
96 return $this->_memcached->delete($id);
97 }
98
99 // ------------------------------------------------------------------------
Greg Aker151b7a92011-08-21 12:29:43 -0500100
Greg Akerbde25d92010-12-21 09:31:21 -0600101 /**
102 * Clean the Cache
103 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300104 * @return bool false on failure/true on success
Greg Akerbde25d92010-12-21 09:31:21 -0600105 */
106 public function clean()
107 {
108 return $this->_memcached->flush();
109 }
110
111 // ------------------------------------------------------------------------
112
113 /**
114 * Cache Info
115 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300116 * @return mixed array on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600117 */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300118 public function cache_info()
Greg Akerbde25d92010-12-21 09:31:21 -0600119 {
120 return $this->_memcached->getStats();
121 }
122
123 // ------------------------------------------------------------------------
Greg Aker151b7a92011-08-21 12:29:43 -0500124
Greg Akerbde25d92010-12-21 09:31:21 -0600125 /**
126 * Get Cache Metadata
127 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300128 * @param mixed key to get cache metadata on
129 * @return mixed FALSE on failure, array on success.
Greg Akerbde25d92010-12-21 09:31:21 -0600130 */
131 public function get_metadata($id)
132 {
133 $stored = $this->_memcached->get($id);
134
135 if (count($stored) !== 3)
136 {
137 return FALSE;
138 }
139
Greg Aker999e7472011-01-29 16:16:58 -0600140 list($data, $time, $ttl) = $stored;
Greg Akerbde25d92010-12-21 09:31:21 -0600141
142 return array(
143 'expire' => $time + $ttl,
144 'mtime' => $time,
145 'data' => $data
146 );
147 }
148
149 // ------------------------------------------------------------------------
150
151 /**
152 * Setup memcached.
Andrey Andreevb24b0332012-03-26 15:34:39 +0300153 *
154 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600155 */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300156 protected function _setup_memcached()
Greg Akerbde25d92010-12-21 09:31:21 -0600157 {
158 // Try to load memcached server info from the config file.
159 $CI =& get_instance();
Greg Aker151b7a92011-08-21 12:29:43 -0500160
Greg Akerbde25d92010-12-21 09:31:21 -0600161 if ($CI->config->load('memcached', TRUE, TRUE))
162 {
163 if (is_array($CI->config->config['memcached']))
164 {
165 $this->_memcache_conf = NULL;
166
167 foreach ($CI->config->config['memcached'] as $name => $conf)
168 {
169 $this->_memcache_conf[$name] = $conf;
Greg Aker151b7a92011-08-21 12:29:43 -0500170 }
171 }
Greg Akerbde25d92010-12-21 09:31:21 -0600172 }
John Bellonecbb81c62011-08-21 09:12:33 -0400173
Greg Aker151b7a92011-08-21 12:29:43 -0500174 if (class_exists('Memcached'))
175 {
176 $this->_memcached = new Memcached();
177 }
Andrey Andreevb24b0332012-03-26 15:34:39 +0300178 elseif (class_exists('Memcache'))
Greg Aker151b7a92011-08-21 12:29:43 -0500179 {
180 $this->_memcached = new Memcache();
181 }
182 else
183 {
184 log_message('error', 'Failed to create object for Memcached Cache; extension not loaded?');
Greg Aker151b7a92011-08-21 12:29:43 -0500185 return FALSE;
186 }
Greg Akerbde25d92010-12-21 09:31:21 -0600187
188 foreach ($this->_memcache_conf as $name => $cache_server)
189 {
190 if ( ! array_key_exists('hostname', $cache_server))
191 {
192 $cache_server['hostname'] = $this->_default_options['default_host'];
193 }
Greg Aker151b7a92011-08-21 12:29:43 -0500194
Greg Akerbde25d92010-12-21 09:31:21 -0600195 if ( ! array_key_exists('port', $cache_server))
196 {
197 $cache_server['port'] = $this->_default_options['default_port'];
198 }
Greg Aker151b7a92011-08-21 12:29:43 -0500199
Greg Akerbde25d92010-12-21 09:31:21 -0600200 if ( ! array_key_exists('weight', $cache_server))
201 {
202 $cache_server['weight'] = $this->_default_options['default_weight'];
203 }
Greg Aker151b7a92011-08-21 12:29:43 -0500204
205 if (get_class($this->_memcached) == 'Memcache')
206 {
207 // Third parameter is persistance and defaults to TRUE.
208 $this->_memcached->addServer(
209 $cache_server['hostname'],
210 $cache_server['port'],
211 TRUE,
212 $cache_server['weight']
213 );
214 }
215 else
216 {
217 $this->_memcached->addServer(
218 $cache_server['hostname'],
219 $cache_server['port'],
220 $cache_server['weight']
221 );
222 }
Greg Akerbde25d92010-12-21 09:31:21 -0600223 }
John Bellone51758fc2011-08-21 09:38:44 -0400224
Greg Aker151b7a92011-08-21 12:29:43 -0500225 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -0600226 }
227
228 // ------------------------------------------------------------------------
229
Greg Akerbde25d92010-12-21 09:31:21 -0600230 /**
231 * Is supported
232 *
233 * Returns FALSE if memcached is not supported on the system.
234 * If it is, we setup the memcached object & return TRUE
Andrey Andreevb24b0332012-03-26 15:34:39 +0300235 *
236 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600237 */
238 public function is_supported()
239 {
Greg Aker151b7a92011-08-21 12:29:43 -0500240 if ( ! extension_loaded('memcached') && ! extension_loaded('memcache'))
Greg Akerbde25d92010-12-21 09:31:21 -0600241 {
242 log_message('error', 'The Memcached Extension must be loaded to use Memcached Cache.');
Greg Akerbde25d92010-12-21 09:31:21 -0600243 return FALSE;
244 }
John Bellone02d73692011-08-21 09:32:35 -0400245
246 return $this->_setup_memcached();
Greg Akerbde25d92010-12-21 09:31:21 -0600247 }
248
Greg Akerbde25d92010-12-21 09:31:21 -0600249}
Greg Akerbde25d92010-12-21 09:31:21 -0600250
251/* End of file Cache_memcached.php */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300252/* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */