国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > PHP > 正文

thinkphp框架下session存入mongo

2020-03-22 20:20:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
  • 最近做系統(tǒng)的時(shí)候處理到session入數(shù)據(jù)庫(kù)問(wèn)題 , 由于是在thinkphp框架下, 查看了下框架session相關(guān)代碼, 發(fā)現(xiàn)原框架默認(rèn)支持mysql數(shù)據(jù)庫(kù) ,

    于是對(duì)原Session驅(qū)動(dòng)做了相應(yīng)修改 , 使其支持mongo數(shù)據(jù)庫(kù)存儲(chǔ)session

    修改的相關(guān)文件是ThinkSessionDriverDbDb.html' target='_blank'>class.php , 本人使用的是TP3.2,2版本 , 修改后的Db.class.php代碼如下:


    <?phpnamespace ThinkSessionDriver;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;    }}

    PHP編程

    鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

  • 發(fā)表評(píng)論 共有條評(píng)論
    用戶(hù)名: 密碼:
    驗(yàn)證碼: 匿名發(fā)表
    主站蜘蛛池模板: 二连浩特市| 磐安县| 平乡县| 包头市| 万州区| 尚义县| 滨州市| 香港 | 外汇| 宁都县| 托克托县| 德钦县| 揭阳市| 南陵县| 门头沟区| 吉安县| 临澧县| 饶河县| 手机| 建昌县| 小金县| 故城县| 镶黄旗| 木兰县| 韶关市| 江达县| 都昌县| 雅安市| 民丰县| 元江| 克拉玛依市| 顺昌县| 团风县| 元阳县| 太白县| 获嘉县| 格尔木市| 香河县| 锡林浩特市| 库伦旗| 合山市|