亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

php語言

php實(shí)現(xiàn)Session存儲(chǔ)到Redis

時(shí)間:2024-08-17 21:52:43 php語言 我要投稿
  • 相關(guān)推薦

php實(shí)現(xiàn)Session存儲(chǔ)到Redis

  介紹了php實(shí)現(xiàn)Session存儲(chǔ)到Redis的方法,php Session可以保存到文本或者內(nèi)存、還有數(shù)據(jù)庫(kù),本文講的是存到Redis的方法.

  對(duì)于大訪問量的站點(diǎn)使用默認(rèn)的Session 并不合適,我們可以將其存入數(shù)據(jù)庫(kù)、或者使用Redis KEY-VALUE數(shù)據(jù)存儲(chǔ)方案

  首先新建一個(gè)session表

  CREATE TABLE `sessions` (

  `sid` char(40) NOT NULL,

  `updatetime` int(20) NOT NULL,

  `data` varchar(200) NOT NULL,

  UNIQUE KEY `sid` (`sid`) USING HASH

  ) ENGINE=MEMORY DEFAULT CHARSET=utf8;

  Mysql 的memory引擎采用內(nèi)存表,所有數(shù)據(jù)存儲(chǔ)在內(nèi)存,操作速度快

  <?php

  //引入數(shù)據(jù)庫(kù)文件

  include "db.php";

  class MySessionHandler implements SessionHandlerInterface

  {

  private $savePath;

  private $sessData;

  public $expiretime;  //設(shè)置過期時(shí)間

  public $db;  //數(shù)據(jù)庫(kù)

  public function __construct($hanlder =''){

  $this->db = Database::getInstance();

  //獲取數(shù)據(jù)庫(kù)實(shí)力

  ///var_dump($this->db);

  }

  public function open($savePath, $sessionName)

  {

  return true;

  }

  public function close()

  {

  return true;

  }

  public function read($id)

  {

  $sql ="select * from sessions where sid ='$id'";

  $result = $this->db->execute($sql);

  if(!empty($result)){

  return $this->sessData = $result;

  }

  }

  //函數(shù)的參數(shù) $id -> 當(dāng)前會(huì)話ID

  //數(shù)據(jù)DATA -> 序列化之后的字符串

  public function write($id, $data)

  {

  // echo $id;

  // echo $data;

  $now = time();

  $newExp = $now+$this->expiretime;  //總時(shí)間=當(dāng)前時(shí)間 + 期限時(shí)間

  $sql = "select * from sessions where sid ='$id'";

  $result = $this->db->getOne($sql);

  //var_dump($result);

  if($data==''||isset($data)){

  $data = $this->sessData;

  }

  if($result){

  //如果存在則更新

  $sql ="update sessions set updatetime = '$newExp',data ='$data' where sid = '$id'";

  //echo $sql;

  $update_data =$this->db->execute($sql);

  if($update_data){

  return true;

  }

  }else{

  //不存在則生成生成

  $sql = "insert into sessions(sid,updatetime,data) values('$id','$now','$data')";

  $insert_data = $this->db->execute($sql);

  if($insert_data){

  return true;

  }

  }

  return false;

  }

  public function destroy($id)

  {    //銷毀

  $sql = "delete from sessions where sid="."$id";

  $destory = $this->db->execute($sql);

  if($destory){

  return true;

  }else{

  return false;

  }

  }

  public function gc($sessMaxLifeTime)

  {

  $t = time();

  $sql ="delete from sessions where $t - 'updatetime'>${sessMaxLifeTime}";

  $data = $this->db->execute($this->tosql);

  if($data){

  return true;

  }else{

  return false;

  }

  return true;

  }

  }

  實(shí)例化

  此處 PHP 手冊(cè)可以有兩種方法

  1,實(shí)現(xiàn)了SessionHandlerInterface借口的對(duì)象,自PHP5.4可以使用

  2 ,直接使用  session_set_save_handler

  ?

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  //判斷PHP版本

  if(version_compare(PHP_VERSION,5.4)==1){

  session_set_save_handler($handler, true);

  session_start();

  }else{

  ini_set('session.use_trans_sid',0);

  ini_set('session.use_cookies',1);

  ini_set('session.cookie_path','/');

  ini_set('session.save_handler','user');

  session_module_name('user');

  session_set_save_handler(array($session,"open"),array($session,"close"),array($session,"read"),array($session,"write"),array($session,"destory"),array($session,"gc"));

  session_start();

  }

  $_SESSION['QQ']="QQ";

  echo $_SESSION['QQ'];

  數(shù)據(jù)庫(kù)代碼

  ?

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  19

  20

  21

  22

  23

  24

  25

  26

  27

  28

  29

  30

  31

  32

  33

  <?php

  class Database{

  static $instance;

  static $db;

  static function getInstance(){

  if(self::$instance){

  return self::$instance;

  }else{

  return new Database();

  }

  }

  public function __construct(){

  self::$db = new PDO('mysql:host=localhost;dbname=session', 'root','');

  }

  public function getOne($sql){

  $rs =self::$db->query($sql);

  @$rs->setFetchMode(PDO::FETCH_ASSOC);//返回關(guān)聯(lián)數(shù)組

  $result = $rs -> fetch();

  return $result;

  }

  public function execute($sql){

  $rs = self::$db->exec($sql);

  return $rs;

  }

  }

  //$data = Database::getInstance();

  //var_dump($data);

  使用REDIS 存儲(chǔ)SESSION

  <?php

  class SessionManager{

  private $redis;

  private $sessionSavePath;

  private $sessionName;

  private $sessionExpireTime = 30;

  public function __construct(){

  $this->redis = new Redis();

  $this->redis->connect('127.0.0.1',6379);  //連接redis

  $retval = session_set_save_handler(

  array($this,"open"),

  array($this,"close"),

  array($this,"read"),

  array($this,"write"),

  array($this,"destory"),

  array($this,"gc")

  );

  session_start();

  }

  public function open($path,$name){

  return true;

  }

  public function close(){

  return true;

  }

  public function read($id){

  $value = $this->redis->get($id);

  if($value){

  return $value;

  }else{

  return "";

  }

  }

  public function write($id,$data){

  if($this->redis->set($id,$data)){

  $this->redis->expire($id,$this->sessionExpireTime);

  //設(shè)置過期時(shí)間

  return true;

  }

  return false;

  }

  public function destory($id){

  if($this->redis->delete($id)){

  return true;

  }

  return false;

  }

  public function gc($maxlifetime){

  return true;

  }

  //析構(gòu)函數(shù)

  public function __destruct(){

  session_write_close();

  }

  }

  $re = new SessionManager();

  $_SESSION['name'] = "qq";

  echo $_SESSION['name'];

  以上就是詳細(xì)的介紹了php實(shí)現(xiàn)Session存儲(chǔ)到Redis的方法,希望對(duì)大家的學(xué)習(xí)有所幫助。

【php實(shí)現(xiàn)Session存儲(chǔ)到Redis】相關(guān)文章:

php中Session存儲(chǔ)到Redis的方法03-02

用PHP基于Redis消息隊(duì)列實(shí)現(xiàn)發(fā)布微博的方法03-02

教你如何使用php的session11-16

php語言中session用法12-03

PHP解決session死鎖的解決方法03-02

php中使用redis隊(duì)列操作實(shí)例代碼03-02

PHP實(shí)現(xiàn)多線程的方法03-29

PHP中實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)12-06

php頁(yè)面緩存實(shí)現(xiàn)方法11-27