blob: 1df149c2dc380544cdb61b59a3d340dcb2faf1b9 [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
Timothy Warren0688ac92012-04-20 10:25:04 -040039 /**
40 * Holds the memcached object
41 *
42 * @var object
43 */
44 protected $_memcached;
Greg Akerbde25d92010-12-21 09:31:21 -060045
Timothy Warren0688ac92012-04-20 10:25:04 -040046 /**
47 * Memcached configuration
48 *
49 * @var array
50 */
Andrey Andreevb24b0332012-03-26 15:34:39 +030051 protected $_memcache_conf = array(
Timothy Warren0688ac92012-04-20 10:25:04 -040052 'default' => array(
53 'default_host' => '127.0.0.1',
54 'default_port' => 11211,
55 'default_weight' => 1
56 )
57 );
Greg Akerbde25d92010-12-21 09:31:21 -060058
Greg Akerbde25d92010-12-21 09:31:21 -060059 /**
60 * Fetch from cache
61 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030062 * @param mixed unique key id
63 * @return mixed data on success/false on failure
Greg Aker151b7a92011-08-21 12:29:43 -050064 */
Greg Akerbde25d92010-12-21 09:31:21 -060065 public function get($id)
Greg Aker151b7a92011-08-21 12:29:43 -050066 {
Greg Akerbde25d92010-12-21 09:31:21 -060067 $data = $this->_memcached->get($id);
Greg Aker151b7a92011-08-21 12:29:43 -050068
Andrey Andreevb24b0332012-03-26 15:34:39 +030069 return is_array($data) ? $data[0] : FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -060070 }
71
72 // ------------------------------------------------------------------------
73
74 /**
75 * Save
76 *
Andrey Andreevb24b0332012-03-26 15:34:39 +030077 * @param string unique identifier
78 * @param mixed data being cached
79 * @param int time to live
Andrey Andreev56454792012-05-17 14:32:19 +030080 * @return bool true on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -060081 */
82 public function save($id, $data, $ttl = 60)
83 {
Andrey Andreevb24b0332012-03-26 15:34:39 +030084 if (get_class($this->_memcached) === 'Memcached')
Mark Huotba00e9f2011-09-23 08:20:29 -040085 {
86 return $this->_memcached->set($id, array($data, time(), $ttl), $ttl);
87 }
Andrey Andreevb24b0332012-03-26 15:34:39 +030088 elseif (get_class($this->_memcached) === 'Memcache')
Mark Huotba00e9f2011-09-23 08:20:29 -040089 {
90 return $this->_memcached->set($id, array($data, time(), $ttl), 0, $ttl);
91 }
Andrey Andreev7d4ea072011-12-25 19:23:50 +020092
Mark Huotba00e9f2011-09-23 08:20:29 -040093 return FALSE;
Greg Akerbde25d92010-12-21 09:31:21 -060094 }
95
96 // ------------------------------------------------------------------------
Greg Aker151b7a92011-08-21 12:29:43 -050097
Greg Akerbde25d92010-12-21 09:31:21 -060098 /**
99 * Delete from Cache
100 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300101 * @param mixed key to be deleted.
Andrey Andreev56454792012-05-17 14:32:19 +0300102 * @return bool true on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600103 */
104 public function delete($id)
105 {
106 return $this->_memcached->delete($id);
107 }
108
109 // ------------------------------------------------------------------------
Greg Aker151b7a92011-08-21 12:29:43 -0500110
Greg Akerbde25d92010-12-21 09:31:21 -0600111 /**
112 * Clean the Cache
113 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300114 * @return bool false on failure/true on success
Greg Akerbde25d92010-12-21 09:31:21 -0600115 */
116 public function clean()
117 {
118 return $this->_memcached->flush();
119 }
120
121 // ------------------------------------------------------------------------
122
123 /**
124 * Cache Info
125 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300126 * @return mixed array on success, false on failure
Greg Akerbde25d92010-12-21 09:31:21 -0600127 */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300128 public function cache_info()
Greg Akerbde25d92010-12-21 09:31:21 -0600129 {
130 return $this->_memcached->getStats();
131 }
132
133 // ------------------------------------------------------------------------
Greg Aker151b7a92011-08-21 12:29:43 -0500134
Greg Akerbde25d92010-12-21 09:31:21 -0600135 /**
136 * Get Cache Metadata
137 *
Andrey Andreevb24b0332012-03-26 15:34:39 +0300138 * @param mixed key to get cache metadata on
139 * @return mixed FALSE on failure, array on success.
Greg Akerbde25d92010-12-21 09:31:21 -0600140 */
141 public function get_metadata($id)
142 {
143 $stored = $this->_memcached->get($id);
144
145 if (count($stored) !== 3)
146 {
147 return FALSE;
148 }
149
Greg Aker999e7472011-01-29 16:16:58 -0600150 list($data, $time, $ttl) = $stored;
Greg Akerbde25d92010-12-21 09:31:21 -0600151
152 return array(
153 'expire' => $time + $ttl,
154 'mtime' => $time,
155 'data' => $data
156 );
157 }
158
159 // ------------------------------------------------------------------------
160
161 /**
162 * Setup memcached.
Andrey Andreevb24b0332012-03-26 15:34:39 +0300163 *
164 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600165 */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300166 protected function _setup_memcached()
Greg Akerbde25d92010-12-21 09:31:21 -0600167 {
168 // Try to load memcached server info from the config file.
169 $CI =& get_instance();
Greg Aker151b7a92011-08-21 12:29:43 -0500170
Greg Akerbde25d92010-12-21 09:31:21 -0600171 if ($CI->config->load('memcached', TRUE, TRUE))
172 {
173 if (is_array($CI->config->config['memcached']))
174 {
175 $this->_memcache_conf = NULL;
176
177 foreach ($CI->config->config['memcached'] as $name => $conf)
178 {
179 $this->_memcache_conf[$name] = $conf;
Greg Aker151b7a92011-08-21 12:29:43 -0500180 }
181 }
Greg Akerbde25d92010-12-21 09:31:21 -0600182 }
John Bellonecbb81c62011-08-21 09:12:33 -0400183
Greg Aker151b7a92011-08-21 12:29:43 -0500184 if (class_exists('Memcached'))
185 {
186 $this->_memcached = new Memcached();
187 }
Andrey Andreevb24b0332012-03-26 15:34:39 +0300188 elseif (class_exists('Memcache'))
Greg Aker151b7a92011-08-21 12:29:43 -0500189 {
190 $this->_memcached = new Memcache();
191 }
192 else
193 {
194 log_message('error', 'Failed to create object for Memcached Cache; extension not loaded?');
Greg Aker151b7a92011-08-21 12:29:43 -0500195 return FALSE;
196 }
Greg Akerbde25d92010-12-21 09:31:21 -0600197
198 foreach ($this->_memcache_conf as $name => $cache_server)
199 {
200 if ( ! array_key_exists('hostname', $cache_server))
201 {
Ryan Neufeldf1bbb172012-03-29 08:24:01 -0700202 $cache_server['hostname'] = $this->_memcache_conf['default']['default_host'];
Greg Akerbde25d92010-12-21 09:31:21 -0600203 }
Greg Aker151b7a92011-08-21 12:29:43 -0500204
Greg Akerbde25d92010-12-21 09:31:21 -0600205 if ( ! array_key_exists('port', $cache_server))
206 {
Ryan Neufeldf1bbb172012-03-29 08:24:01 -0700207 $cache_server['port'] = $this->_memcache_conf['default']['default_port'];
Greg Akerbde25d92010-12-21 09:31:21 -0600208 }
Greg Aker151b7a92011-08-21 12:29:43 -0500209
Greg Akerbde25d92010-12-21 09:31:21 -0600210 if ( ! array_key_exists('weight', $cache_server))
211 {
Ryan Neufeldf1bbb172012-03-29 08:24:01 -0700212 $cache_server['weight'] = $this->_memcache_conf['default']['default_weight'];
Greg Akerbde25d92010-12-21 09:31:21 -0600213 }
Greg Aker151b7a92011-08-21 12:29:43 -0500214
215 if (get_class($this->_memcached) == 'Memcache')
216 {
217 // Third parameter is persistance and defaults to TRUE.
218 $this->_memcached->addServer(
219 $cache_server['hostname'],
220 $cache_server['port'],
221 TRUE,
222 $cache_server['weight']
223 );
224 }
225 else
226 {
227 $this->_memcached->addServer(
228 $cache_server['hostname'],
229 $cache_server['port'],
230 $cache_server['weight']
231 );
232 }
Greg Akerbde25d92010-12-21 09:31:21 -0600233 }
John Bellone51758fc2011-08-21 09:38:44 -0400234
Greg Aker151b7a92011-08-21 12:29:43 -0500235 return TRUE;
Greg Akerbde25d92010-12-21 09:31:21 -0600236 }
237
238 // ------------------------------------------------------------------------
239
Greg Akerbde25d92010-12-21 09:31:21 -0600240 /**
241 * Is supported
242 *
243 * Returns FALSE if memcached is not supported on the system.
244 * If it is, we setup the memcached object & return TRUE
Andrey Andreevb24b0332012-03-26 15:34:39 +0300245 *
246 * @return bool
Greg Akerbde25d92010-12-21 09:31:21 -0600247 */
248 public function is_supported()
249 {
Greg Aker151b7a92011-08-21 12:29:43 -0500250 if ( ! extension_loaded('memcached') && ! extension_loaded('memcache'))
Greg Akerbde25d92010-12-21 09:31:21 -0600251 {
252 log_message('error', 'The Memcached Extension must be loaded to use Memcached Cache.');
Greg Akerbde25d92010-12-21 09:31:21 -0600253 return FALSE;
254 }
John Bellone02d73692011-08-21 09:32:35 -0400255
256 return $this->_setup_memcached();
Greg Akerbde25d92010-12-21 09:31:21 -0600257 }
258
Greg Akerbde25d92010-12-21 09:31:21 -0600259}
Greg Akerbde25d92010-12-21 09:31:21 -0600260
261/* End of file Cache_memcached.php */
Andrey Andreevb24b0332012-03-26 15:34:39 +0300262/* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */