php之session最優將資訊寫入memcache中管理_PHP教程

來源:互聯網
上載者:User

php之session最優將資訊寫入memcache中管理


前面也講到了用memcache儲存資料資訊緩衝的方法和好處,這樣能夠減少訪問資料庫的次數,減少訪問量大時對資料庫的壓力

將session儲存到memcache中管理需要瞭解memcache、session的使用和session_set_save_handler()

同樣先編寫一個公用的類,當然採用靜態成員方法

memcache 指令用telnet操作

同樣現在根目錄下建立需要用到的檔案

其中memsession.class.php 是公用的memcache儲存類檔案,one.php、two.php和three.php是測試檔案,items.php 是輸出資料數組的

session.class.php中:

首先定義串連memcache用到的變數並初始化

connect("localhost",11211) or die("could not connect");    MemSession::start($memcache);


注意的是 NS 為常量,定義下標

再初始化方法

    //初始化方法    private static function init($handler){    self::$handler=$handler;    self::$lifetime=ini_get('session.gc_maxlifetime');    self::$time=time();    }


開啟session,並定義調用本類中的open、close等方法

    //開啟session    public static function start(Memcache $memcache){    //首先將屬性初始化    self::init($memcache);  //調用handler,以後調用handler時都是用memcache    session_set_save_handler(    array(__CLASS__,'open'),//調用本類的open方法    array(__CLASS__,'close'),    array(__CLASS__,'read'),    array(__CLASS__,'write'),    array(__CLASS__,'destroy'),    array(__CLASS__,'gc')    );    //調用session_start()    session_start();    }

接下來就是定義上面調用的這些方法

open() 和 close() 只要返回真就可以,但 open() 的參數為 路徑(path) 和 名稱(name)

    public static function open($path, $name){    return true;    }    public static function close(){    return true;    }

read() 只需要有PHPSESSID參數即可

但要判斷傳入的out 參數是否有值,有值就返回out的資料

    public static function read($PHPSESSID){    $out=self::$handler->get(self::session_key($PHPSESSID));  //得到該下標輸出的資料    if($out===false || $out ==null){    return '';  //out得到資料沒有,返回空    }    return $out;  //返回得到的資料    }

write() :

返回自身的id,資料,和生命時間長度

    public static function write($PHPSESSID, $data){    //判斷是否有資料    $method=$data ? 'set' : 'relpace';    return self::$handler->$method(self::session_key($PHPSESSID), $data, MEMCACHE_COMPRESSED, self::$lifetime);    }

destroy() 和 gc() :

destroy()調用自身的delete方法

    public static function destroy($PHPSESSID){    return self::$handler->delete(self::session_key($PHPSESSID));  //調用delete方法    }    public static function gc($lifetime){    return true;    }

接下來需要定義一個傳入PHPSESSID的方法

    private static function session_key($PHPSESSID){    $session_key=self::NS.$PHPSESSID; //索引值為自身和傳進來的phpsessid    return $session_key;    }

結果顯示

如果成功,在telnet中顯示

表示session資料資訊儲存到memcache成功


http://www.bkjia.com/PHPjc/971083.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/971083.htmlTechArticlephp之session最優將資訊寫入memcache中管理 前面也講到了用memcache儲存資料資訊緩衝的方法和好處,這樣能夠減少訪問資料庫的次數,減少訪問...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.