繁体   English   中英

在Redis中将php会话存储为json,读取会话

[英]Storing php sessions in redis as json, read the session

我将我的php会话作为json字符串存储在redis中,以便与node.js共享它们,就像在这里完成的那样:

http://simplapi.wordpress.com/2012/04/13/php-and-node-js-session-share-redi/

我不喜欢这些功能将会话存储在磁盘和Redis中。 我只想让他们穿着Redis。 作为json。

我以为改变它会很容易,我想是的,但是我不知道如何。

我不能使用session_encode(); 在特定变量上。 我需要序列化反序列化吗? 读取功能必须返回哪种格式?

public function read($id) {

    $id = $this->_prefix . $id;

    try {

        $r = $this->__rc
            ->multi()
            ->get($id)
            ->expire($id, $this->ttl)
            ->exec();

    } catch (\RedisException $e) { return false; }


    $tmp      = $_SESSION;
    $_SESSION = json_decode( $r[0], true );

    if( isset( $_SESSION ) && ! empty( $_SESSION ) && $_SESSION != null ){

        $new_data = session_encode();
        $_SESSION = $tmp;
        return $new_data;

    } 

    else{ 

        return ''; 

    }

}

public function write($id, $data) {

    $tmp = $_SESSION;
    session_decode($data);
    $new_data = $_SESSION;
    $_SESSION = $tmp;

    $id = $this->_prefix . $id;

    try{

        $this->__rc
            ->multi()
            ->set( $id, json_encode($new_data) )
            ->expire($id, $this->ttl)
            ->exec();

    } catch (\RedisException $e) { return false; }

    return true;

}

为了仅将会话保存在Redis中而不保存在磁盘上,您需要更改将会话存储在php.ini中的设置,例如session_save路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM