blob: c6ad5651166051a5979f4131db550a143f955bde [file] [log] [blame]
Andrey Andreevc9eface2014-09-02 15:19:01 +03001<?php
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 5.2.4 or newer
6 *
7 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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.
18 *
19 * @package CodeIgniter
20 * @author Andrey Andreev
21 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
23 * @link http://codeigniter.com
24 * @since Version 3.0
25 * @filesource
26 */
27defined('BASEPATH') OR exit('No direct script access allowed');
28
29/**
30 * CodeIgniter Session Memcached Driver
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Sessions
35 * @author Andrey Andreev
36 * @link http://codeigniter.com/user_guide/libraries/sessions.html
37 */
38class CI_Session_memcached_driver extends CI_Session_driver implements SessionHandlerInterface {
39
40 /**
41 * Save path
42 *
43 * @var string
44 */
45 protected $_save_path;
46
47 /**
48 * Memcached instance
49 *
50 * @var Memcached
51 */
52 protected $_memcached;
53
54 /**
55 * Key prefix
56 *
57 * @var string
58 */
59 protected $_key_prefix = 'ci_session:';
60
61 /**
62 * Lock key
63 *
64 * @var string
65 */
66 protected $_lock_key;
67
68 // ------------------------------------------------------------------------
69
70 /**
71 * Class constructor
72 *
73 * @param array $params Configuration parameters
74 * @return void
75 */
76 public function __construct(&$params)
77 {
78 parent::__construct($params);
79
80 if (empty($this->_save_path))
81 {
82 log_message('error', 'Session: No Memcached save path configured.');
83 }
84
85 if ($this->_match_ip === TRUE)
86 {
87 $this->_key_prefix .= $_SERVER['REMOTE_ADDR'].':';
88 }
89 }
90
91 // ------------------------------------------------------------------------
92
93 public function open($save_path, $name)
94 {
95 $this->_memcached = new Memcached();
96 $server_list = array();
97 foreach ($this->_memcached->getServerList() as $server)
98 {
99 $server_list[] = $server['host'].':'.$server['port'];
100 }
101
102 if ( ! preg_match_all('#,?([^,:]+)\:(\d{1,5})(?:\:(\d+))?#', $this->_save_path, $matches, PREG_SET_ORDER))
103 {
104 $this->_memcached = NULL;
105 log_message('error', 'Session: Invalid Memcached save path format: '.$this->_save_path);
106 return FALSE;
107 }
108
109 foreach ($matches as $match)
110 {
111 // If Memcached already has this server (or if the port is invalid), skip it
112 if (in_array($match[1].':'.$match[2], $server_list, TRUE))
113 {
114 log_message('debug', 'Session: Memcached server pool already has '.$match[1].':'.$match[2]);
115 continue;
116 }
117
118 if ( ! $this->_memcached->addServer($match[1], $match[2], isset($match[3]) ? $match[3] : 0))
119 {
120 log_message('error', 'Could not add '.$match[1].':'.$match[2].' to Memcached server pool.');
121 }
122 else
123 {
124 $server_list[] = $server['host'].':'.$server['port'];
125 }
126 }
127
128 if (empty($server_list))
129 {
130 log_message('error', 'Session: Memcached server pool is empty.');
131 return FALSE;
132 }
133
134 return TRUE;
135 }
136
137 // ------------------------------------------------------------------------
138
139 public function read($session_id)
140 {
141 if (isset($this->_memcached) && $this->_get_lock($session_id))
142 {
143 $session_data = (string) $this->_memcached->get($this->_key_prefix.$session_id);
144 $this->_fingerprint = md5($session_data);
145 return $session_data;
146 }
147
148 return FALSE;
149 }
150
151 public function write($session_id, $session_data)
152 {
153 if (isset($this->_memcached, $this->_lock_key))
154 {
155 $this->_memcached->replace($this->_lock_key, time(), 5);
156 if ($this->_fingerprint !== ($fingerprint = md5($session_data)))
157 {
158 if ($this->_memcached->set($this->_key_prefix.$session_id, $session_data, $this->_expiration))
159 {
160 $this->_fingerprint = $fingerprint;
161 return TRUE;
162 }
163
164 return FALSE;
165 }
166
167 return $this->_memcached->touch($this->_key_prefix.$session_id, $this->_expiration);
168 }
169
170 return FALSE;
171 }
172
173 // ------------------------------------------------------------------------
174
175 public function close()
176 {
177 if (isset($this->_memcached))
178 {
179 isset($this->_lock_key) && $this->_memcached->delete($this->_lock_key);
180 if ( ! $this->_memcached->quit())
181 {
182 return FALSE;
183 }
184
185 $this->_memcached = NULL;
186 return TRUE;
187 }
188
189 return FALSE;
190 }
191
192 // ------------------------------------------------------------------------
193
194 public function destroy($session_id)
195 {
196 if (isset($this->_memcached, $this->_lock_key))
197 {
198 $this->_memcached->delete($this->_key_prefix.$session_id);
199 return ($this->_cookie_destroy() && $this->close());
200 }
201
202 return $this->close();
203 }
204
205 // ------------------------------------------------------------------------
206
207 public function gc($maxlifetime)
208 {
209 return TRUE;
210 }
211
212 // ------------------------------------------------------------------------
213
214 protected function _get_lock($session_id)
215 {
216 if (isset($this->_lock_key))
217 {
218 return $this->_memcached->replace($this->_lock_key, time(), 5);
219 }
220
221 $lock_key = $this->_key_prefix.$session_id.':lock';
222 if ( ! ($ts = $this->_memcached->get($lock_key)))
223 {
224 if ( ! $this->_memcached->set($lock_key, TRUE, 5))
225 {
226 log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id);
227 return FALSE;
228 }
229
230 $this->_lock_key = $lock_key;
231 $this->_lock = TRUE;
232 return TRUE;
233 }
234
235 // Another process has the lock, we'll try to wait for it to free itself ...
236 $attempt = 0;
237 while ($attempt++ < 5)
238 {
239 usleep(((time() - $ts) * 1000000) - 20000);
240 if (($ts = $this->_memcached->get($lock_key)) < time())
241 {
242 continue;
243 }
244
245 if ( ! $this->_memcached->set($lock_key, time(), 5))
246 {
247 log_message('error', 'Session: Error while trying to obtain lock for '.$this->_key_prefix.$session_id);
248 return FALSE;
249 }
250
251 $this->_lock_key = $lock_key;
252 break;
253 }
254
255 if ($attempt === 5)
256 {
257 log_message('error', 'Session: Unable to obtain lock for '.$this->_key_prefix.$session_id.' after 5 attempts, aborting.');
258 return FALSE;
259 }
260
261 $this->_lock = TRUE;
262 return TRUE;
263 }
264
265 // ------------------------------------------------------------------------
266
267 protected function _release_lock()
268 {
269 if (isset($this->_memcached, $this->_lock_key) && $this->_lock)
270 {
271 if ( ! $this->_memcached->delete($this->_lock_key) && $this->_memcached->getResultCode() !== Memcached::RES_NOTFOUND)
272 {
273 log_message('error', 'Session: Error while trying to free lock for '.$this->_key_prefix.$session_id);
274 return FALSE;
275 }
276
277 $this->_lock_key = NULL;
278 $this->_lock = FALSE;
279 }
280
281 return TRUE;
282 }
283
284}
285
286/* End of file Session_memcached_driver.php */
287/* Location: ./system/libraries/Session/drivers/Session_memcached_driver.php */