| 12694 |
anikendra |
1 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
2 |
/**
|
|
|
3 |
* CodeIgniter
|
|
|
4 |
*
|
|
|
5 |
* An open source application development framework for PHP 4.3.2 or newer
|
|
|
6 |
*
|
|
|
7 |
* @package CodeIgniter
|
|
|
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
|
|
|
12 |
* @since Version 2.0
|
|
|
13 |
* @filesource
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
// ------------------------------------------------------------------------
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* CodeIgniter Memcached Caching Class
|
|
|
20 |
*
|
|
|
21 |
* @package CodeIgniter
|
|
|
22 |
* @subpackage Libraries
|
|
|
23 |
* @category Core
|
|
|
24 |
* @author ExpressionEngine Dev Team
|
|
|
25 |
* @link
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
class CI_Cache_memcached extends CI_Driver {
|
|
|
29 |
|
|
|
30 |
private $_memcached; // Holds the memcached object
|
|
|
31 |
|
|
|
32 |
protected $_memcache_conf = array(
|
|
|
33 |
'default' => array(
|
|
|
34 |
'default_host' => '127.0.0.1',
|
|
|
35 |
'default_port' => 11211,
|
|
|
36 |
'default_weight' => 1
|
|
|
37 |
)
|
|
|
38 |
);
|
|
|
39 |
|
|
|
40 |
// ------------------------------------------------------------------------
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Fetch from cache
|
|
|
44 |
*
|
|
|
45 |
* @param mixed unique key id
|
|
|
46 |
* @return mixed data on success/false on failure
|
|
|
47 |
*/
|
|
|
48 |
public function get($id)
|
|
|
49 |
{
|
|
|
50 |
$data = $this->_memcached->get($id);
|
|
|
51 |
|
|
|
52 |
return (is_array($data)) ? $data[0] : FALSE;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
// ------------------------------------------------------------------------
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Save
|
|
|
59 |
*
|
|
|
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
|
|
|
64 |
*/
|
|
|
65 |
public function save($id, $data, $ttl = 60)
|
|
|
66 |
{
|
|
|
67 |
if (get_class($this->_memcached) == 'Memcached')
|
|
|
68 |
{
|
|
|
69 |
return $this->_memcached->set($id, array($data, time(), $ttl), $ttl);
|
|
|
70 |
}
|
|
|
71 |
else if (get_class($this->_memcached) == 'Memcache')
|
|
|
72 |
{
|
|
|
73 |
return $this->_memcached->set($id, array($data, time(), $ttl), 0, $ttl);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
return FALSE;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
// ------------------------------------------------------------------------
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Delete from Cache
|
|
|
83 |
*
|
|
|
84 |
* @param mixed key to be deleted.
|
|
|
85 |
* @return boolean true on success, false on failure
|
|
|
86 |
*/
|
|
|
87 |
public function delete($id)
|
|
|
88 |
{
|
|
|
89 |
return $this->_memcached->delete($id);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
// ------------------------------------------------------------------------
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Clean the Cache
|
|
|
96 |
*
|
|
|
97 |
* @return boolean false on failure/true on success
|
|
|
98 |
*/
|
|
|
99 |
public function clean()
|
|
|
100 |
{
|
|
|
101 |
return $this->_memcached->flush();
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
// ------------------------------------------------------------------------
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Cache Info
|
|
|
108 |
*
|
|
|
109 |
* @param null type not supported in memcached
|
|
|
110 |
* @return mixed array on success, false on failure
|
|
|
111 |
*/
|
|
|
112 |
public function cache_info($type = NULL)
|
|
|
113 |
{
|
|
|
114 |
return $this->_memcached->getStats();
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
// ------------------------------------------------------------------------
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Get Cache Metadata
|
|
|
121 |
*
|
|
|
122 |
* @param mixed key to get cache metadata on
|
|
|
123 |
* @return mixed FALSE on failure, array on success.
|
|
|
124 |
*/
|
|
|
125 |
public function get_metadata($id)
|
|
|
126 |
{
|
|
|
127 |
$stored = $this->_memcached->get($id);
|
|
|
128 |
|
|
|
129 |
if (count($stored) !== 3)
|
|
|
130 |
{
|
|
|
131 |
return FALSE;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
list($data, $time, $ttl) = $stored;
|
|
|
135 |
|
|
|
136 |
return array(
|
|
|
137 |
'expire' => $time + $ttl,
|
|
|
138 |
'mtime' => $time,
|
|
|
139 |
'data' => $data
|
|
|
140 |
);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
// ------------------------------------------------------------------------
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Setup memcached.
|
|
|
147 |
*/
|
|
|
148 |
private function _setup_memcached()
|
|
|
149 |
{
|
|
|
150 |
// Try to load memcached server info from the config file.
|
|
|
151 |
$CI =& get_instance();
|
|
|
152 |
if ($CI->config->load('memcached', TRUE, TRUE))
|
|
|
153 |
{
|
|
|
154 |
if (is_array($CI->config->config['memcached']))
|
|
|
155 |
{
|
|
|
156 |
$this->_memcache_conf = NULL;
|
|
|
157 |
|
|
|
158 |
foreach ($CI->config->config['memcached'] as $name => $conf)
|
|
|
159 |
{
|
|
|
160 |
$this->_memcache_conf[$name] = $conf;
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
$this->_memcached = new Memcached();
|
|
|
166 |
|
|
|
167 |
foreach ($this->_memcache_conf as $name => $cache_server)
|
|
|
168 |
{
|
|
|
169 |
if ( ! array_key_exists('hostname', $cache_server))
|
|
|
170 |
{
|
|
|
171 |
$cache_server['hostname'] = $this->_default_options['default_host'];
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
if ( ! array_key_exists('port', $cache_server))
|
|
|
175 |
{
|
|
|
176 |
$cache_server['port'] = $this->_default_options['default_port'];
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
if ( ! array_key_exists('weight', $cache_server))
|
|
|
180 |
{
|
|
|
181 |
$cache_server['weight'] = $this->_default_options['default_weight'];
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
$this->_memcached->addServer(
|
|
|
185 |
$cache_server['hostname'], $cache_server['port'], $cache_server['weight']
|
|
|
186 |
);
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
// ------------------------------------------------------------------------
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Is supported
|
|
|
195 |
*
|
|
|
196 |
* Returns FALSE if memcached is not supported on the system.
|
|
|
197 |
* If it is, we setup the memcached object & return TRUE
|
|
|
198 |
*/
|
|
|
199 |
public function is_supported()
|
|
|
200 |
{
|
|
|
201 |
if ( ! extension_loaded('memcached'))
|
|
|
202 |
{
|
|
|
203 |
log_message('error', 'The Memcached Extension must be loaded to use Memcached Cache.');
|
|
|
204 |
|
|
|
205 |
return FALSE;
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
$this->_setup_memcached();
|
|
|
209 |
return TRUE;
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
// ------------------------------------------------------------------------
|
|
|
213 |
|
|
|
214 |
}
|
|
|
215 |
// End Class
|
|
|
216 |
|
|
|
217 |
/* End of file Cache_memcached.php */
|
|
|
218 |
/* Location: ./system/libraries/Cache/drivers/Cache_memcached.php */
|