为什么我会丢失 memcached 中的会话数据?
我一直在编写 Codeigniter Session 库的扩展。
完整的“包”可以在 github 上找到。
我遇到的问题是,它似乎将数据保存到内存缓存中,但是当它运行以下方法时(CI 经常更新会话数据 - 默认情况下 5 分钟),它似乎“丢失”了数据。我很确定它会发生在添加新项目然后删除旧项目的情况。
function sess_update() {
// We only update the session every five minutes by default
if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now) {
log_message('info','not enough time before update');
return;
}
log_message('info','defo need to update session');
// Save the old session id so we know which record to
// update in the database if we need it
$old_sessid = $this->userdata['session_id'];
$new_sessid = '';
while (strlen($new_sessid) < 32) {
$new_sessid .= mt_rand(0, mt_getrandmax());
}
// To make the session ID even more secure we'll combine it with the user's IP
$new_sessid .= $this->CI->input->ip_address();
// Turn it into a hash
$new_sessid = md5(uniqid($new_sessid, TRUE));
log_message('info','session id generated');
// Update the session data in the session data array
$this->userdata['session_id'] = $new_sessid;
$this->userdata['last_activity'] = $this->now;
// _set_cookie() will handle this for us if we aren't using database sessions
// by pushing all userdata to the cookie.
$cookie_data = NULL;
$cookie_data = array();
foreach (array('session_id', 'ip_address', 'user_agent', 'last_activity') as $val) {
$cookie_data[$val] = $this->userdata[$val];
}
switch ($this->session_storage) {
case 'database':
// Update the session ID and last_activity field in the DB if needed
// set cookie explicitly to only have our session data
$this->CI->db->query($this->CI->db->update_string($this->sess_table_name, array('last_activity' => $this->now, 'session_id' => $new_sessid), array('session_id' => $old_sessid)));
break;
case 'memcached':
// Add item with new session_id and data to memcached
// then delete old memcache item
$this->memcache->add('user_session_data' . $new_sessid, $this->userdata, false, $this->sess_expiration);
log_message('info', 'new session added');
$this->memcache->delete('user_session_data' . $old_sessid, 0);
log_message('info', 'old session deleted');
break;
}
// Write the cookie
$this->_set_cookie($cookie_data);
}
有人知道我哪里出错了吗?
I've been putting together an extension to the Codeigniter Session library.
The complete "package" can be found on github.
The issue I'm having is that it seems to be saving the data into memcache, but when it runs the following method (CI updates the session data every so often - 5 minutes by default) it seems to "lose" the data. I'm pretty sure it's happening where it adds a new item and then deletes the old one.
function sess_update() {
// We only update the session every five minutes by default
if (($this->userdata['last_activity'] + $this->sess_time_to_update) >= $this->now) {
log_message('info','not enough time before update');
return;
}
log_message('info','defo need to update session');
// Save the old session id so we know which record to
// update in the database if we need it
$old_sessid = $this->userdata['session_id'];
$new_sessid = '';
while (strlen($new_sessid) < 32) {
$new_sessid .= mt_rand(0, mt_getrandmax());
}
// To make the session ID even more secure we'll combine it with the user's IP
$new_sessid .= $this->CI->input->ip_address();
// Turn it into a hash
$new_sessid = md5(uniqid($new_sessid, TRUE));
log_message('info','session id generated');
// Update the session data in the session data array
$this->userdata['session_id'] = $new_sessid;
$this->userdata['last_activity'] = $this->now;
// _set_cookie() will handle this for us if we aren't using database sessions
// by pushing all userdata to the cookie.
$cookie_data = NULL;
$cookie_data = array();
foreach (array('session_id', 'ip_address', 'user_agent', 'last_activity') as $val) {
$cookie_data[$val] = $this->userdata[$val];
}
switch ($this->session_storage) {
case 'database':
// Update the session ID and last_activity field in the DB if needed
// set cookie explicitly to only have our session data
$this->CI->db->query($this->CI->db->update_string($this->sess_table_name, array('last_activity' => $this->now, 'session_id' => $new_sessid), array('session_id' => $old_sessid)));
break;
case 'memcached':
// Add item with new session_id and data to memcached
// then delete old memcache item
$this->memcache->add('user_session_data' . $new_sessid, $this->userdata, false, $this->sess_expiration);
log_message('info', 'new session added');
$this->memcache->delete('user_session_data' . $old_sessid, 0);
log_message('info', 'old session deleted');
break;
}
// Write the cookie
$this->_set_cookie($cookie_data);
}
Does anyone have any ideas where I'm going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想对会话使用 memcache,最好的方法是直接在 php.ini 中设置它并利用本机功能:
If you want to use memcache for sessions, the best way to do it would be to set it directly in php.ini and take advantage of native functionality: