session放入緩存(redis)、DB


為什么要把SESSION保存在緩存

  就php來說,語言本身支持的session是以文件的方式保存到磁盤文件中,保存在指定的文件夾中,保存的路徑可以在配置文件中設置或者在程序中使用函數session_save_path()進行設置,但是這么做有弊端,

第一就是保存到文件系統中,效率低,只要有用到session就會從好多個文件中查找指定的sessionid,效率很低。

第二就是當用到多台服務器的時候可能會出現,session丟失問題(其實是保存在了其他服務器上)。

  當然了,保存在緩存中可以解決上面的問題,如果使用php本身的session函數,可以使用session_set_save_handler()函數很方便的對session的處理過程進行重新控制。如果不用php的session系列函數,可以自己編寫個類似的session函數,也是可以的,我現在做的這個項目就是這樣,會根據用戶的mid、登錄時間進行求hash作為sessionId,每次請求的時候都必須加上sessionId才算合法(第一次登錄的時候是不需要的,這個時候會創建sessionId,返回給客戶端),這么做也很方便、簡潔高效的。當然了,我這篇文章主要說的是在php自身的SESSION中”做做手腳”。

SESSION保存在緩存中

  php將緩存保存到redis中,可以使用配置文件,對session的處理和保存做修改,當然了,在程序中使用ini_set()函數去修改也可以,這個很方便測試,我這里就使用這種方式,當然了,要是生產環境還是建議使用配置文件。

<?php
ini_set("session.save_handler", "redis");
ini_set("session.save_path", "tcp://localhost:6379");
session_start();
header("Content-type:text/html;charset=utf-8");
if(isset($_SESSION['view'])){
    $_SESSION['view'] = $_SESSION['view'] + 1;
}else{
    $_SESSION['view'] = 1;
}
echo "【view】{$_SESSION['view']}";

  這里設置session.save_handler方式為redis,session.save_path為redis的地址和端口,設置之后刷新,再回頭查看redis,會發現redis中的生成了sessionId,sessionId和瀏覽器請求的是一樣的,

2015-01-25_1338442015-01-25_133759

  是不是很方便呢,只需要改下配置文件就可以實現redis中保存session,但是我這里要說的是通過程序的方式來處理session保存到redis或者db,下面一起來看看。

通過php提供的接口,自己改寫session的處理函數

這里可以先看看php的這個函數session_set_save_handler,php5.4及之后可以直接實現SessionHandlerInterface接口,代碼會更加簡潔。重寫的時候主要有下面幾個方法

open(string $savePath, string $sessionName); //open類似於構造函數,開始會話的時候會調用,比如使用session_start()函數之后

close(); //類似於類的析構函數,在write函數調用之后調用,session_write_close()之后之后也會執行

read(string $sessionId); //讀取session的時候調用

write(string $sessionId, string $data); //保存數據的時候調用

destroy($sessionId); //銷毀會話的時候(session_destroy()或者session_regenerate_id())會調用

gc($lifeTime); //垃圾清理函數,清理掉過期作廢的數據

  主要就是實現這幾個方法,根據不同的存儲驅動可以自己設置不同的具體方法,我實現了mysql數據庫和redis這兩種保存session的驅動,如果有需要的話可以自己去擴展,擴展很方便很容易。

  下面是我的redis的實現(db和redis差不多,redis代碼少,貼出來):

  我使用了接口的方式,這樣擴展起來更方便,那天想用memcached了,直接添加就行了

<?php
include_once __DIR__."/interfaceSession.php";
/**
 * 以db的方式存儲session
 */
class redisSession implements interfaceSession{
    /**
     * 保存session的數據庫表的信息
     */
    private $_options = array(
        'handler' => null, //數據庫連接句柄
        'host' => null,
        'port' => null,
        'lifeTime' => null,
    );

    /**
     * 構造函數
     * @param $options 設置信息數組
     */
    public function __construct($options=array()){
        if(!class_exists("redis", false)){
            die("必須安裝redis擴展");
        }
        if(!isset($options['lifeTime']) || $options['lifeTime'] <= 0){
            $options['lifeTime'] = ini_get('session.gc_maxlifetime');
        }
        $this->_options = array_merge($this->_options, $options);
    }

    /**
     * 開始使用該驅動的session
     */
    public function begin(){
        if($this->_options['host'] === null ||
           $this->_options['port'] === null ||
           $this->_options['lifeTime'] === null
        ){
            return false;
        }
        //設置session處理函數
        session_set_save_handler(
            array($this, 'open'),
            array($this, 'close'),
            array($this, 'read'),
            array($this, 'write'),
            array($this, 'destroy'),
            array($this, 'gc')
        );
    }
    /**
     * 自動開始回話或者session_start()開始回話后第一個調用的函數
     * 類似於構造函數的作用
     * @param $savePath 默認的保存路徑
     * @param $sessionName 默認的參數名,PHPSESSID
     */
    public function open($savePath, $sessionName){
        if(is_resource($this->_options['handler'])) return true;
        //連接redis
        $redisHandle = new Redis();
        $redisHandle->connect($this->_options['host'], $this->_options['port']);
        if(!$redisHandle){
            return false;
        }

        $this->_options['handler'] = $redisHandle;
        $this->gc(null);
        return true;

    }

    /**
     * 類似於析構函數,在write之后調用或者session_write_close()函數之后調用
     */
    public function close(){
        return $this->_options['handler']->close();
    }

    /**
     * 讀取session信息
     * @param $sessionId 通過該Id唯一確定對應的session數據
     * @return session信息/空串
     */
    public function read($sessionId){
        return $this->_options['handler']->get($sessionId);
    }

    /**
     * 寫入或者修改session數據
     * @param $sessionId 要寫入數據的session對應的id
     * @param $sessionData 要寫入的數據,已經序列化過了
     */
    public function write($sessionId, $sessionData){
        return $this->_options['handler']->setex($sessionId, $this->_options['lifeTime'], $sessionData);
    }

    /**
     * 主動銷毀session會話
     * @param $sessionId 要銷毀的會話的唯一id
     */
    public function destroy($sessionId){
        return $this->_options['handler']->delete($sessionId) >= 1 ? true : false;
    }

    /**
     * 清理繪畫中的過期數據
     * @param 有效期
     */
    public function gc($lifeTime){
        //獲取所有sessionid,讓過期的釋放掉
        $this->_options['handler']->keys("*");
        return true;
    }

}

 

  看看簡單工廠模式

class session {
    /**
     * 驅動程序句柄保存
     */
    private static $_handler = null;

    /**
     * 創建session驅動程序
     */
    public static function getSession($type, $options){
        //單例
        if(isset($handler)){
            return self::$_handler;
        }

        switch ($type) {
            case 'db': //數據庫驅動session類型
                    include_once __DIR__."/driver/dbSession.php";
                    $handler = new dbSession($options);
                break;
            
            case 'redis': //redis驅動session類型
                    include_once __DIR__."/driver/redisSession.php";
                    $handler = new redisSession($options);
                break;
            default:
                    return false;
                break;
        }

        return self::$_handler = $handler;
    }
}

 

  調用也很簡單,

session::getSession('redis',array(
        'host' => "localhost",
        'port' => "6379",
    ))->begin();

session_start();

  數據庫版本的也一樣很簡單就可以配置,需要的話可以在這里下載完整版和demo

 

 

  本文版權歸作者iforever(luluyrt@163.com)所有,未經作者本人同意禁止任何形式的轉載,轉載文章之后必須在文章頁面明顯位置給出作者和原文連接,否則保留追究法律責任的權利。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM