最近阅读tp5的底层类的实现,

看到了大神的Redis 类的实现, 觉

得非常的简洁明了,而且统一了所有的get,set,在更新一下,非常值得参考

/**
     * 读取缓存
     * @access public
     * @param string $name 缓存变量名
     * @param mixed  $default 默认值
     * @return mixed
     */
    public function get($name, $default = false)
    {
        //拼接key , 并获取缓存
        $value = $this->handler->get($this->getCacheKey($name));
        if (is_null($value)) {
            return $default;
        }
        $jsonData = json_decode($value, true);
        // 检测是否为JSON数据 true 返回JSON解析数组, false返回源数据 
        return (null === $jsonData) ? $value : $jsonData;
    }

    /**
     * 写入缓存
     * @access public
     * @param string    $name 缓存变量名
     * @param mixed     $value  存储数据
     * @param integer   $expire  有效时间(秒)
     * @return boolean
     */
    public function set($name, $value, $expire = null)
    {
        if (is_null($expire)) {
            $expire = $this->options['expire'];
        }
        if ($this->tag && !$this->has($name)) {
            $first = true;
        }
        //增加KEY的前缀
        $key = $this->getCacheKey($name);
        //对数组/对象数据进行缓存处理,保证数据完整性 
        $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
        if (is_int($expire) && $expire) {
            $result = $this->handler->setex($key, $expire, $value);
        } else {
            $result = $this->handler->set($key, $value);
        }
        isset($first) && $this->setTagItem($key);
        return $result;
    }

 

 

 

16.11.9号更新:

后来发现,由于redis存储数据方式,如果存储的数据中有"x:x"时,似乎会导致读取错误,存储时需注意

 

 

 

今天遇到了redis保存多维数组出错的情况,得知redis是只能模拟简单数组,没有多维数组的数据结构.

解决方案是序列化一下保存的数据

我的解决方案是在原有的hset跟hget的基础上新增了两个方法 setArr跟getArr 调用 hset hget 用来保存多维数组的情况

这两个方法是在存之前,取之后都进行序列化操作.

既不影响原来的简单操作

又实现了保存多维数组的作用

 

例子如下

 

<?php 
namespace Vendor\Redis;
class DefaultRedis{
private $redis;
public function __construct($host='127.0.0.1',$port =3198,$auth = '#yr9XjB%b6k'){
$this->redis = new \Redis();
$this->redis->pconnect($host,$port);
$this->redis->auth($auth);
}


/**
* [set key-value设置 会覆盖]
* @ckhero
* @DateTime 2016-06-17
* @param [type] $name [缓存名字(传进来的参数可为数组)]
* @param [type] $value [缓存值]
* @param integer $expire [缓存有效期]
*/
public function set($name,$value,$expire=0){
if(is_array($name)){ //判断是否维数组
foreach($name as $key=>$val){
if($expire==0){
if(!$this->redis->set($val,$value[$key])){ //缓存失败则会中断
return false;
}
}else{
if(!$this->redis->setex($val,$expire,$value[$key])){
return false;
}
}
}
return true;
}else{
if($expire==0){
return $this->redis->set($name,$value);
}else{
return $res = $this->redis->setex($name,$expire,$value);
}
}
}
/**
* [setnx 设置值不覆盖]
* @ckhero
* @DateTime 2016-06-17
* @param [type] $name [缓存名字]
* @param [type] $value [缓存的值]
* @return [type] [返回 成功(true)/失败(false)]
*/
public function setnx($name,$value){
return $this->redis->setnx($name,$value);
}
/set/getarr/
/**
* [setArr 将存入的数据进行序列化]
* @ckhero
* @DateTime 2016-10-27
* @param [type] $key [description]
* @param [type] $data [description]
*/
public function setArr($key,$data){
$this->set($key,serialize($data));
}
public function getArr($key){
return unserialize($this->get($key));
}

/**
* [get 获取缓存]
* @ckhero
* @DateTime 2016-06-17
* @param [type] $name [缓存名字(可为数组)]
* @return [type] [返回值]
*/
public function get($name){
if(!is_array($name)){
return $this->redis->get($name);
}else{
return $this->redis->getMultiple($name);
}
}
/**
* [exists 验证指定的键是否存在]
* @ckhero
* @DateTime 2016-06-17
* @param [type] $name [缓存名字]
* @return [type] [返回 存在(true)/不存在(false)]
*/
public function exists($name){
return $this->redis->exists($name);
}

 

/**
* [delete 删除]
* @ckhero
* @DateTime 2016-06-17
* @param [type] $name [缓存名字]
* @return [type] [返回 成功(true)/失败(false)]
*/
public function delete($name){
return $this->redis->delete($name);
}



public function hset($key,$val,$field){
if(!is_array($val)){
return $this->redis->hset($key,$field,$val);
}else{
return $this->redis->hMset($key,$val);
}
}

public function hget($key,$field=false){
if(!$field){
return $this->redis->hGetAll($key);
}else{
return $this->redis->hGet($key,$name);
}
}

 


/**
* list
* 操作
*/

/**
* [lpush 左/右插入]
* @ckhero
* @DateTime 2016-07-29
* @param [type] $key [description]
* @param [type] $val [description]
* @param string $type [l从左边插入,R从右边插入]
* @return [type] [description]
*/
public function lset($key,$val,$type = 'L',$index=-1){
if(empty($key)){
return false;
}
if(!is_array($val)){ //不是数组先转为数组
$arr[] = $val;
}else{
$arr = $val;
}
foreach($arr as $k=>$v){
if($type=='L'){
$res[] = $this->redis->lPush($key,$v); //左插入
}else if($type=='R'){
$res[] = $this->redis->rPush($key,$v); //右插入
}elseif($type=='S'){
$res[] = $this->redis->lSet($key,$index,$v); //定点插入
}else{
return false;
}
}
return $res;
}

/**
* [lsize 返回该列表长度 ]
* @ckhero
* @DateTime 2016-07-29
* @param [type] $key [description]
* @return [type] [description]
*/
public function lsize($key){
return $this->redis->lSize($key);
}
/**
* [lget 获取list的值]
* @ckhero
* @DateTime 2016-08-11
* @param [type] $key [description]
* @param [type] $type [description]
* @param integer $start [description]
* @param integer $end [description]
* @return [type] [description]
*/
public function lget($key,$type,$start=0,$end=-1){
if(empty($key)){
return false;
}
if($type == 'g'){
//return $this->reids->lget($key,1);
}else if($type=='R'){
return $this->redis->lRange($key,$start,$end);
} 
}
 

/**
* [pop 返回一个值 并且删除该值]
* @ckhero
* @DateTime 2016-08-11
* @param [type] $key [description]
* @param string $type [description]
* @return [type] [description]
*/
public function pop($key,$type = "L"){

if(!empty($key)){

if($type =='l'){
return $this->redis->lPop($key);
}elseif($type=='R'){
return $this->redis->rPop($key);
}
}

 

return false;
}

 


public function ltrim($key,$start,$end){
return $this->redis->lTrim($key,$start,$end);
}

/**
* [expire 设置过期时间]
* @ckhero
* @DateTime 2016-08-16
* @param [type] $data [二维数组安然然也array( array('键值'=>过期时间))]
* @return [type] [description]
*/
public function expire($key,$val){
if(!is_array($key)){
$data[$key] = $val;
}else{
$data = $key;
}
foreach($data as $k=>$expire_time){
$this->redis->expire($k,$expire_time);
}
return true;
}

/**
* [databaseSelect 表选择]
* @ckhero
* @DateTime 2016-08-18
* @param string $name [表名字]
* @return [type] [description]
*/
public function databaseSelect($name='default'){
switch($name){

case 'pt_log':
$database = 1;
break; 

case 'zwid':
$database = 2;
break; 

case 'one_coin':
$database = 3;
break;

case "wheel" :
$database = 4;
break; 

case "home" :
$database = 5;
break; 

case "analysis" :
$database = 6;
break; 

case "order" :
$database = 7;
break;

case "product_score" :
$database = 8;
break;

 

case "LeTianBang" :
$database = 9;
break; 

default:
$database = 0;
break;
}
return $this->redis->select($database);
}
/**
* [del 删除缓存]
* @ckhero
* @DateTime 2016-08-18
* @param [type] $key [缓存的key名]
* @return [type] [true/false]
*/
public function del($key){ 

return $this->redis->del($key);
}

 

/**
* [keys 模糊匹配]
* @ckhero
* @DateTime 2016-08-29
* @param [type] $key [description]
* @return [type] [description]
*/
public function keys($key){
return $this->redis->keys($key);
}

/**
* zset
*/

/**
* [zset 有序集合设置]
* @ckhero
* @DateTime 2016-09-01
* @param [type] $key [键名]
* @param [type] $score [分数、权重]
* @param [type] $member [键值]
* @return [type] [description]
*/
public function zset($key,$score,$val){

 

return $this->redis->zAdd($key,$score,$val);
}

/**
* [zdel 删除有序集合中某个值]
* @ckhero
* @DateTime 2016-09-01
* @param [type] $key [键名]
* @param [type] $val [删除的键值]
* @return [type] [description]
*/
public function zdel($key,$val){ 

return $this->redis->zRem($key,$val);
}
 

/**
* [zget 获取集合中的数据]
* @ckhero
* @DateTime 2016-09-01
* @param [type] $key [键名]
* @param integer $start [开始索引]
* @param integer $end [结束索引]
* @param string $order ['排序方式';sec(从小打大);desc(从大到小)]
* @param boolean $withscroes [true返回分数值;默认false 不返回]
* @return [type] [description]
*/
public function zget($key,$start=0,$end='-1',$order = 'sec',$withscroes = false){ 

if($order =='sec'){ 

return $this->redis->zRange($key,$start,$end,$withscroes);
}else{
return $this->redis->zRevRange($key,$start,$end,$withscroes);
}
}

 

/**
* [exists 判断商品是否存在]
* @ckhero
* @DateTime 2016-09-02
* @param [type] $key [键名]
* @return [type] [1为存在,0为不存在]
*/

public function existsRedis($key){ 

return $this->redis->exists($key);
}
}

 

?>