對于大訪問量的站點使用默認的Session 并不合適,我們可以將其存入數(shù)據(jù)庫、或者使用Redis KEY-VALUE數(shù)據(jù)存儲方案
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供貴陽企業(yè)網(wǎng)站建設,專注與網(wǎng)站建設、成都網(wǎng)站制作、H5開發(fā)、小程序制作等業(yè)務。10年已為貴陽眾多企業(yè)、政府機構(gòu)等服務。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。
首先新建一個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ù)存儲在內(nèi)存,操作速度快
復制代碼
?php
//引入數(shù)據(jù)庫文件
include "db.php";
class MySessionHandler implements SessionHandlerInterface
{
private $savePath;
private $sessData;
public $expiretime; //設置過期時間
public $db; //數(shù)據(jù)庫
public function __construct($hanlder =''){
$this-db = Database::getInstance();
//獲取數(shù)據(jù)庫實力
///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 - 當前會話ID
//數(shù)據(jù)DATA - 序列化之后的字符串
public function write($id, $data)
{
// echo $id;
// echo $data;
$now = time();
$newExp = $now+$this-expiretime; //總時間=當前時間 + 期限時間
$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;
}
}
復制代碼
實例化
此處 PHP 手冊可以有兩種方法
1,實現(xiàn)了SessionHandlerInterface借口的對象,自PHP5.4可以使用
2 ,直接使用 session_set_save_handler
復制代碼
//判斷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ù)庫代碼 db.php
復制代碼
?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);//返回關聯(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 存儲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);
//設置過期時間
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'];
?php
$redis?=?new?Redis();
//*********無序集合**********//
//添加set
$redis-
sadd('set1' , 'ab');
$redis-sadd('set1' , 'cd');
$redis-sadd('set1' , 'ef');
//返回集合所有成員
var_dump($redis-smembers('set1'));
//返回集合隨機元素
var_dump($redis-srandmember('set1'));
//*********有序集合**********//
//添加zset
$redis-zadd('zset1' , 1 , 'ab');
$redis-zadd('zset1' , 2 , 'cd');
$redis-zadd('zset1' , 3 , 'ef');
//返回指定區(qū)間的而元素
$redis-zrange('zset1' , 0 ,1); //0和1之間的元素
//更多請查手冊
redis key 的長度主要會影響空間占用,時間上,差距可以忽略
所以這個不用擔心了