]>
jfr.im git - z_archive/KronOS.git/blob - system/libraries/Cache/drivers/Cache_memcached.php
1 <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2006 - 2012 EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * CodeIgniter Memcached Caching Class
21 * @package CodeIgniter
22 * @subpackage Libraries
24 * @author ExpressionEngine Dev Team
28 class CI_Cache_memcached
extends CI_Driver
{
30 private $_memcached; // Holds the memcached object
32 protected $_memcache_conf = array(
34 'default_host' => '127.0.0.1',
35 'default_port' => 11211,
40 // ------------------------------------------------------------------------
45 * @param mixed unique key id
46 * @return mixed data on success/false on failure
48 public function get($id)
50 $data = $this->_memcached
->get($id);
52 return (is_array($data)) ? $data[0] : FALSE;
55 // ------------------------------------------------------------------------
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
65 public function save($id, $data, $ttl = 60)
67 if (get_class($this->_memcached
) == 'Memcached')
69 return $this->_memcached
->set($id, array($data, time(), $ttl), $ttl);
71 else if (get_class($this->_memcached
) == 'Memcache')
73 return $this->_memcached
->set($id, array($data, time(), $ttl), 0, $ttl);
79 // ------------------------------------------------------------------------
84 * @param mixed key to be deleted.
85 * @return boolean true on success, false on failure
87 public function delete($id)
89 return $this->_memcached
->delete($id);
92 // ------------------------------------------------------------------------
97 * @return boolean false on failure/true on success
99 public function clean()
101 return $this->_memcached
->flush();
104 // ------------------------------------------------------------------------
109 * @param null type not supported in memcached
110 * @return mixed array on success, false on failure
112 public function cache_info($type = NULL)
114 return $this->_memcached
->getStats();
117 // ------------------------------------------------------------------------
122 * @param mixed key to get cache metadata on
123 * @return mixed FALSE on failure, array on success.
125 public function get_metadata($id)
127 $stored = $this->_memcached
->get($id);
129 if (count($stored) !== 3)
134 list($data, $time, $ttl) = $stored;
137 'expire' => $time +
$ttl,
143 // ------------------------------------------------------------------------
148 private function _setup_memcached()
150 // Try to load memcached server info from the config file.
151 $CI =& get_instance();
152 if ($CI->config
->load('memcached', TRUE, TRUE))
154 if (is_array($CI->config
->config
['memcached']))
156 $this->_memcache_conf
= NULL;
158 foreach ($CI->config
->config
['memcached'] as $name => $conf)
160 $this->_memcache_conf
[$name] = $conf;
165 $this->_memcached
= new Memcached();
167 foreach ($this->_memcache_conf
as $name => $cache_server)
169 if ( ! array_key_exists('hostname', $cache_server))
171 $cache_server['hostname'] = $this->_default_options
['default_host'];
174 if ( ! array_key_exists('port', $cache_server))
176 $cache_server['port'] = $this->_default_options
['default_port'];
179 if ( ! array_key_exists('weight', $cache_server))
181 $cache_server['weight'] = $this->_default_options
['default_weight'];
184 $this->_memcached
->addServer(
185 $cache_server['hostname'], $cache_server['port'], $cache_server['weight']
190 // ------------------------------------------------------------------------
196 * Returns FALSE if memcached is not supported on the system.
197 * If it is, we setup the memcached object & return TRUE
199 public function is_supported()
201 if ( ! extension_loaded('memcached'))
203 log_message('error', 'The Memcached Extension must be loaded to use Memcached Cache.');
208 $this->_setup_memcached();
212 // ------------------------------------------------------------------------
217 /* End of file Cache_memcached.php */
218 /* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */