PageRenderTime 26ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Cache/drivers/Cache_memcached.php

https://bitbucket.org/Naked_Yoshi/sq_kanban
PHP | 303 lines | 127 code | 35 blank | 141 comment | 8 complexity | c88704859e1b8d9be03adfa6b01389f2 MD5 | raw file
  1<?php
  2/**
  3 * CodeIgniter
  4 *
  5 * An open source application development framework for PHP
  6 *
  7 * This content is released under the MIT License (MIT)
  8 *
  9 * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
 10 *
 11 * Permission is hereby granted, free of charge, to any person obtaining a copy
 12 * of this software and associated documentation files (the "Software"), to deal
 13 * in the Software without restriction, including without limitation the rights
 14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 15 * copies of the Software, and to permit persons to whom the Software is
 16 * furnished to do so, subject to the following conditions:
 17 *
 18 * The above copyright notice and this permission notice shall be included in
 19 * all copies or substantial portions of the Software.
 20 *
 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 27 * THE SOFTWARE.
 28 *
 29 * @package	CodeIgniter
 30 * @author	EllisLab Dev Team
 31 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
 32 * @copyright	Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
 33 * @license	http://opensource.org/licenses/MIT	MIT License
 34 * @link	https://codeigniter.com
 35 * @since	Version 2.0
 36 * @filesource
 37 */
 38defined('BASEPATH') OR exit('No direct script access allowed');
 39
 40/**
 41 * CodeIgniter Memcached Caching Class
 42 *
 43 * @package		CodeIgniter
 44 * @subpackage	Libraries
 45 * @category	Core
 46 * @author		EllisLab Dev Team
 47 * @link
 48 */
 49class CI_Cache_memcached extends CI_Driver {
 50
 51	/**
 52	 * Holds the memcached object
 53	 *
 54	 * @var object
 55	 */
 56	protected $_memcached;
 57
 58	/**
 59	 * Memcached configuration
 60	 *
 61	 * @var array
 62	 */
 63	protected $_config = array(
 64		'default' => array(
 65			'host'		=> '127.0.0.1',
 66			'port'		=> 11211,
 67			'weight'	=> 1
 68		)
 69	);
 70
 71	// ------------------------------------------------------------------------
 72
 73	/**
 74	 * Class constructor
 75	 *
 76	 * Setup Memcache(d)
 77	 *
 78	 * @return	void
 79	 */
 80	public function __construct()
 81	{
 82		// Try to load memcached server info from the config file.
 83		$CI =& get_instance();
 84		$defaults = $this->_config['default'];
 85
 86		if ($CI->config->load('memcached', TRUE, TRUE))
 87		{
 88			$this->_config = $CI->config->config['memcached'];
 89		}
 90
 91		if (class_exists('Memcached', FALSE))
 92		{
 93			$this->_memcached = new Memcached();
 94		}
 95		elseif (class_exists('Memcache', FALSE))
 96		{
 97			$this->_memcached = new Memcache();
 98		}
 99		else
100		{
101			log_message('error', 'Cache: Failed to create Memcache(d) object; extension not loaded?');
102			return;
103		}
104
105		foreach ($this->_config as $cache_server)
106		{
107			isset($cache_server['hostname']) OR $cache_server['hostname'] = $defaults['host'];
108			isset($cache_server['port']) OR $cache_server['port'] = $defaults['port'];
109			isset($cache_server['weight']) OR $cache_server['weight'] = $defaults['weight'];
110
111			if ($this->_memcached instanceof Memcache)
112			{
113				// Third parameter is persistence and defaults to TRUE.
114				$this->_memcached->addServer(
115					$cache_server['hostname'],
116					$cache_server['port'],
117					TRUE,
118					$cache_server['weight']
119				);
120			}
121			elseif ($this->_memcached instanceof Memcached)
122			{
123				$this->_memcached->addServer(
124					$cache_server['hostname'],
125					$cache_server['port'],
126					$cache_server['weight']
127				);
128			}
129		}
130	}
131
132	// ------------------------------------------------------------------------
133
134	/**
135	 * Fetch from cache
136	 *
137	 * @param	string	$id	Cache ID
138	 * @return	mixed	Data on success, FALSE on failure
139	 */
140	public function get($id)
141	{
142		$data = $this->_memcached->get($id);
143
144		return is_array($data) ? $data[0] : $data;
145	}
146
147	// ------------------------------------------------------------------------
148
149	/**
150	 * Save
151	 *
152	 * @param	string	$id	Cache ID
153	 * @param	mixed	$data	Data being cached
154	 * @param	int	$ttl	Time to live
155	 * @param	bool	$raw	Whether to store the raw value
156	 * @return	bool	TRUE on success, FALSE on failure
157	 */
158	public function save($id, $data, $ttl = 60, $raw = FALSE)
159	{
160		if ($raw !== TRUE)
161		{
162			$data = array($data, time(), $ttl);
163		}
164
165		if ($this->_memcached instanceof Memcached)
166		{
167			return $this->_memcached->set($id, $data, $ttl);
168		}
169		elseif ($this->_memcached instanceof Memcache)
170		{
171			return $this->_memcached->set($id, $data, 0, $ttl);
172		}
173
174		return FALSE;
175	}
176
177	// ------------------------------------------------------------------------
178
179	/**
180	 * Delete from Cache
181	 *
182	 * @param	mixed	$id	key to be deleted.
183	 * @return	bool	true on success, false on failure
184	 */
185	public function delete($id)
186	{
187		return $this->_memcached->delete($id);
188	}
189
190	// ------------------------------------------------------------------------
191
192	/**
193	 * Increment a raw value
194	 *
195	 * @param	string	$id	Cache ID
196	 * @param	int	$offset	Step/value to add
197	 * @return	mixed	New value on success or FALSE on failure
198	 */
199	public function increment($id, $offset = 1)
200	{
201		return $this->_memcached->increment($id, $offset);
202	}
203
204	// ------------------------------------------------------------------------
205
206	/**
207	 * Decrement a raw value
208	 *
209	 * @param	string	$id	Cache ID
210	 * @param	int	$offset	Step/value to reduce by
211	 * @return	mixed	New value on success or FALSE on failure
212	 */
213	public function decrement($id, $offset = 1)
214	{
215		return $this->_memcached->decrement($id, $offset);
216	}
217
218	// ------------------------------------------------------------------------
219
220	/**
221	 * Clean the Cache
222	 *
223	 * @return	bool	false on failure/true on success
224	 */
225	public function clean()
226	{
227		return $this->_memcached->flush();
228	}
229
230	// ------------------------------------------------------------------------
231
232	/**
233	 * Cache Info
234	 *
235	 * @return	mixed	array on success, false on failure
236	 */
237	public function cache_info()
238	{
239		return $this->_memcached->getStats();
240	}
241
242	// ------------------------------------------------------------------------
243
244	/**
245	 * Get Cache Metadata
246	 *
247	 * @param	mixed	$id	key to get cache metadata on
248	 * @return	mixed	FALSE on failure, array on success.
249	 */
250	public function get_metadata($id)
251	{
252		$stored = $this->_memcached->get($id);
253
254		if (count($stored) !== 3)
255		{
256			return FALSE;
257		}
258
259		list($data, $time, $ttl) = $stored;
260
261		return array(
262			'expire'	=> $time + $ttl,
263			'mtime'		=> $time,
264			'data'		=> $data
265		);
266	}
267
268	// ------------------------------------------------------------------------
269
270	/**
271	 * Is supported
272	 *
273	 * Returns FALSE if memcached is not supported on the system.
274	 * If it is, we setup the memcached object & return TRUE
275	 *
276	 * @return	bool
277	 */
278	public function is_supported()
279	{
280		return (extension_loaded('memcached') OR extension_loaded('memcache'));
281	}
282
283	// ------------------------------------------------------------------------
284
285	/**
286	 * Class destructor
287	 *
288	 * Closes the connection to Memcache(d) if present.
289	 *
290	 * @return	void
291	 */
292	public function __destruct()
293	{
294		if ($this->_memcached instanceof Memcache)
295		{
296			$this->_memcached->close();
297		}
298		elseif ($this->_memcached instanceof Memcached && method_exists($this->_memcached, 'quit'))
299		{
300			$this->_memcached->quit();
301		}
302	}
303}