精简版的Redis封装类,其实大部分情况下,我们也只用到了Redis的字符串读写。下面是代码<?php
class redisHelp {
public $redis;
function __construct()
{
$this->redis = new Redis();
$this->redis->connect('127.0.0.1',6379);
}
function __destruct()
{
// TODO: Implement __destruct() method.
$this->redis->close();
}
public function get($key){
$b = $this->redis->get($key);
if ($this->is_serialized($b)){
return unserialize($b);
}
return $b;
}
public function set($key,$data,$ttl = 86400){
if (is_array($data)){
$b = $this->redis->set($key,serialize($data),$ttl);
}else{
$b = $this->redis->set($key,$data,$ttl);
}
return $b;
}
private 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;
}
}