thinkphp框架下session 存入mongo


最近做系统的时候处理到session入数据库问题 , 由于是在thinkphp框架下, 查看了下框架session相关代码, 发现原框架默认支持mysql数据库 , 

于是对原Session驱动做了相应修改 , 使其支持mongo数据库存储session

修改的相关文件是\Think\Session\Driver\Db\Db.class.php , 本人使用的是TP3.2,2版本 , 修改后的Db.class.php代码如下:


<?php
namespace Think\Session\Driver;

class Db
{
    const MONGO_EXPIRY = 3;
    private $host = '';
    private $port = '';
    private $username = '';
    private $password = '';
    private $dbname = '';
    private $dbsession = '';
    private $__mongo_collection = NULL;
    private $__current_session = NULL;

    /**
     * Default constructor set default parameter
     * @access public
     */
    public function __construct()
    {
        $this->host = C('DB_HOST');
        $this->port = C('DB_PORT');
        $this->username = C('DB_USER');
        $this->password = C('DB_PWD');
        $this->dbname = C('DB_NAME');
        $this->dbsession = C('SESSION_TABLE');
        $this->__connect();
        session_set_save_handler(
            array(&$this, 'open'),
            array(&$this, 'close'),
            array(&$this, 'read'),
            array(&$this, 'write'),
            array(&$this, 'destroy'),
            array(&$this, 'gc')
        );
    }

    /**
     * connectes the mongo database and create collection
     * @access private
     */
    private function __connect()
    {
        $connection_string = sprintf('mongodb://%s:%s', $this->host, $this->port);
        if ($this->username != null && $this->password != null) {
            $connection_string = "mongodb://{$this->username}:{$this->password}@{$this->host}:{$this->port}";
            $connection_string = sprintf('mongodb://%s:%s@%s:%s', $this->username, $this->password, $this->username, $this->password);
        }
        $object_mongo = new \Mongo($connection_string);
        $object_mongo = $object_mongo->{$this->dbname};
        $this->__mongo_collection = $object_mongo->{$this->dbsession};
    }


    /**
     *
     * check for collection object
     * @access public
     * @param string $session_path
     * @param string $session_name
     * @return boolean
     */
    public function open($session_path, $session_name)
    {
        $result = false;
        if ($this->__mongo_collection != NULL) {
            $result = false;
        }
        return $result;
    }

    /**
     *
     * doing noting
     * @access public
     * @return boolean
     */
    public function close()
    {
        return true;
    }

    /**
     *
     * Reading session data based on id
     * @access public
     * @param string $session_id
     * @return mixed
     */
    public function read($session_id)
    {
        $result = NULL;
        $expiry = time();
        $query['_id'] = $session_id;
        $query['expiry'] = array('$gte' => $expiry);
        $result = $this->__mongo_collection->findone($query);
        if ($result) {
            $this->__current_session = $result;
        }
        return $result['data'];
    }

    /**
     *
     * Writing session data
     * @access public
     * @param string $session_id
     * @param mixed $data
     * @return boolean
     */
    public function write($session_id, $data)
    {
        $result = true;
        $expiry = $this->__getExpriry();
        $session_data = array();
        if (empty($this->__current_session)) {
            $session_id = $session_id;
            $session_data['_id'] = $session_id;
            $session_data['data'] = $data;
            $session_data['expiry'] = $expiry;
        } else {
            $session_data = (array)$this->__current_session;
            $session_data['data'] = $data;
            $session_data['expiry'] = $expiry;

        }
        $query['_id'] = $session_id;
        $record = $this->__mongo_collection->findOne($query);
        if ($record == null) {
            $this->__mongo_collection->insert($session_data);
        } else {
            $record['data'] = $data;
            $record['expiry'] = $expiry;
            $this->__mongo_collection->save($record);
        }
        return true;
    }

    /**
     *
     * remove session data
     * @access public
     * @param string $session_id
     * @return boolean
     */
    public function destroy($session_id)
    {
        $query['_id'] = $session_id;
        $this->__mongo_collection->remove($query, true);
        return true;
    }

    /**
     *
     * Garbage collection
     * @access public
     * @return boolean
     */
    public function gc()
    {
        $query = array();
        $query['expiry'] = array(':lt' => time());
        $this->__mongo_collection->remove($query, array('justOne' => false));
        return true;

    }

    /**
     * get expiry
     * @access private
     * @return int
     */
    private function __getExpriry()
    {
        return time() + self::MONGO_EXPIRY;
    }
}



你可能感兴趣的:(thinkphp框架下session 存入mongo)