class redis {
public function __construct() {
parent::__construct();
$this->redis = new \redis();
$result = $this->redis->connect('127.0.0.1', 6379);
if($result){
return true;
}else{
return false;
}
}
Public function setRedis($key,$data,$time){
if(is_array($data)){
$data = serialize($data);
}
$result = $this->redis->set($key,$data);
$this->redis->expire($key,$time);
if($result){
return true;
}else{
return false;
}
}
public function getRedis($key){
$data = $this->redis->get($key);
if($this->is_serialized($data)){
$data = unserialize($data);
}
if($data){
return $data;
}else{
return false;
}
}
public function deletRedis($key){
$result = $this->redis->delete($key);
if($result){
return true ;
}else{
return false;
}
}
//判断是否为系列化数组
public function is_serialized($data) {
$data = trim( $data );
if ( 'N;' == $data )
return true;
if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
return false;
switch ( $badions[1] ) {
case 'a' :
case 'O' :
case 's' :
if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
return true;
break;
case 'b' :
case 'i' :
case 'd' :
if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
return true;
break;
}
return false;
}
public function flushAll() {
return $this->redis->flushAll();
}
}
public function __construct() {
parent::__construct();
$this->redis = new \redis();
$result = $this->redis->connect('127.0.0.1', 6379);
if($result){
return true;
}else{
return false;
}
}
Public function setRedis($key,$data,$time){
if(is_array($data)){
$data = serialize($data);
}
$result = $this->redis->set($key,$data);
$this->redis->expire($key,$time);
if($result){
return true;
}else{
return false;
}
}
public function getRedis($key){
$data = $this->redis->get($key);
if($this->is_serialized($data)){
$data = unserialize($data);
}
if($data){
return $data;
}else{
return false;
}
}
public function deletRedis($key){
$result = $this->redis->delete($key);
if($result){
return true ;
}else{
return false;
}
}
//判断是否为系列化数组
public function is_serialized($data) {
$data = trim( $data );
if ( 'N;' == $data )
return true;
if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
return false;
switch ( $badions[1] ) {
case 'a' :
case 'O' :
case 's' :
if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
return true;
break;
case 'b' :
case 'i' :
case 'd' :
if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
return true;
break;
}
return false;
}
public function flushAll() {
return $this->redis->flushAll();
}
}