mysql+内存表+session_用mysql内存表来代替phpsession的类_PHP

代码如下:

}else{

$sql = 'delete from '.$this->storage_name.' while time+'.$this->expire_time.'}

$this->execute($sql);

}

public function refresh($key=""){

if($this->mysql_version>4){

$sql = 'update '.$this->storage_name.' set time=CURRENT_TIMESTAMP() where id="'.$key.'"';

}else{

$sql = 'update '.$this->storage_name.' set time=unix_timestamp() where id="'.$key.'"';

}

$return = $this->execute($sql);

if(!$return){

$this->initTable();

$return = $this->execute($sql,true);

}

return $return;

}

public function create($key="",$value=""){

if($key=="") $key = $this->id;

if($value != "") $value = mysql_real_escape_string(serialize($value),$this->conn);

if(strlen($value)>$this->max_session_data_length) throw new Exception(__FILE__."->".__LINE__.": Session data is long than max allow length(".$this->max_session_data_length.")!");

if($this->mysql_version>4){

$sql = 'replace into '.$this->storage_name.' set value=\''.$value.'\',id="'.$key.'",time=CURRENT_TIMESTAMP()';

}else{

$sql = 'replace into '.$this->storage_name.' set value=\''.$value.'\',id="'.$key.'",time=unix_timestamp()';

}

$return = $this->execute($sql);

if(!$return){

$this->initTable();

$return = $this->execute($sql,true);

}

return $return;

}

public function set($key="",$value=""){

if($key=="") $key = $this->id;

if($value != "") $value = mysql_real_escape_string(serialize($value),$this->conn);

$sql = 'update '.$this->storage_name.' set value=\''.$value.'\' where id="'.$key.'"';

if(strlen($value)>$this->max_session_data_length)

{

if($this->mysql_version>4){

throw new Exception(__FILE__."->".__LINE__.": Session data is long than max allow length(".$this->max_session_data_length.")!");

}

$sql = 'replace into '.$this->storage_name_slow.' set value=\''.$value.'\',id="'.$key.'",time=unix_timestamp()';

$this->execute($sql,true);

$sql = 'update '.$this->storage_name.' set value=\''.$this->data_too_long_instead_value.'\' where id="'.$key.'"';

}

$return = $this->execute($sql);

if(!$return){

$this->initTable();

$return = $this->execute($sql,true);

}

return $return;

}

private function initTable(){

if($this->mysql_version>4){

$sql = "

CREATE TABLE if not exists `".$this->storage_name."` (

`id` char(32) NOT NULL default 'ERR',

`value` VARBINARY(".$this->max_session_data_length.") NULL,

`time` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

PRIMARY KEY (`id`),

KEY `time` (`time`)

) ENGINE=MEMORY;

";

}else{

$sqlSlow = "

CREATE TABLE if not exists `".$this->storage_name."_slow` (

`id` char(32) NOT NULL default 'ERR',

`value` text NULL,

`time` int(10) not null default '0',

PRIMARY KEY (`id`),

KEY `time` (`time`)

) ENGINE=MyISAM;

";

$this->execute($sqlSlow,true);

$sql = "

CREATE TABLE if not exists `".$this->storage_name."` (

`id` char(32) NOT NULL default 'ERR',

`value` VARCHAR(255) NULL,

`time` int(10) not null default '0',

PRIMARY KEY (`id`),

KEY `time` (`time`)

) ENGINE=MEMORY;

";

}

return $this->execute($sql,true);

}

private function execute($sql,$die=false)

{

if($die)

{

mysql_query($sql,$this->conn) or die("exe Sql error:

".mysql_error()."

".$sql."");

}

else

{

mysql_query($sql,$this->conn);

if(mysql_error()){

return false;

}else{

return true;

}

}

}

private function getOne($sql,$die=false){

$rs = $this->query($sql,$die);

if($rs && ($one = mysql_fetch_row($rs)) ){

return $one[0];

}else{

return false;

}

}

private function query($sql,$die=false){

if($die)

$rs = mysql_query($sql,$this->conn) or die("query Sql error:

".mysql_error()."

".$sql."");

else

$rs = mysql_query($sql,$this->conn);

return $rs;

}

}

$lnk = mysql_connect('localhost', 'root', '123456')

or die ('Not connected : ' . mysql_error());

// make foo the current db

mysql_select_db('test', $lnk) or die ('Can\'t use foo : ' . mysql_error());

$S = new session($lnk);

if(!$S->last){

$S->last = time();

}

echo "First visit at ".$S->last."

";

if(!$S->lastv){

$S->lastv = 0;

}

$S->lastv++;

echo "lastv=".$S->lastv."

";

echo "test=".$S->test."

";

if(isset($_GET['max'])){

$S->boom = str_repeat("OK",255);

}

if(isset($_GET['boom'])){

$S->boom = $_GET['boom'];

}

echo "boom=".$S->boom."

";

?>

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值