我的项目用的是codeigniter 3,想用redis来存储session 结合nodejs和mysql用于实时通知系统
我在我的服务器上安装了 redis 并用 nodejs 测试它工作正常
最佳答案
我不知道你是否还在寻找答案(也会帮助其他人)但这里有一个很好的链接(https://simplapi.wordpress.com/2012/04/13/php-and-node-js-session-share-redi/) 这解释了将 session 数据序列化和反序列化为 json。我已经在 Codeigniter 的库 Session_redis_driver.php 中进行了修改以进行编码和解码,因此我可以在 Nodejs 中使用它。如果有人可以验证此代码并指出任何陷阱或未处理的异常,我会很高兴。
修改“read”方法
public function read($session_id) { if (isset($this->_redis) && $this->_get_lock($session_id)) { // Needed by write() to detect session_regenerate_id() calls $this->_session_id = $session_id; $session_data = json_decode($this->_redis->get($this->_key_prefix.$session_id),TRUE); is_array($session_data) ? $this->_key_exists = TRUE : $session_data = ''; $this->_fingerprint = md5(json_encode($session_data)); $tmp = $_SESSION; $_SESSION = $session_data; $new_data = session_encode(); $_SESSION = $tmp; return $new_data; } return $this->_fail(); }
到目前为止,一切正常。
在写入方法中进行了以下更改。
if (isset($this->_lock_key)) { $this->_redis->setTimeout($this->_lock_key, 300); $tmp = $_SESSION; session_decode($session_data); $new_data = $_SESSION; $_SESSION = $tmp; if ($this->_fingerprint !== ($fingerprint = md5(json_encode($new_data))) OR $this->_key_exists === FALSE) { if ($this->_redis->set($this->_key_prefix.$session_id, json_encode($new_data), $this->_config['expiration'])) { $this->_fingerprint = $fingerprint; $this->_key_exists = TRUE; return $this->_success; } return $this->_fail(); } return ($this->_redis->setTimeout($this->_key_prefix.$session_id, $this->_config['expiration'])) ? $this->_success : $this->_fail(); }
关于codeigniter - 我可以使用 codeigniter 和 redis 来存储 session 吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33396492/