init($param); } public function init($param = array()) { if($param['host'] && $param['port']) { $this->config = $param; } else { $CI =& get_instance(); if ($CI->config->load('redis')){ $host = $CI->config->item('host'); $port = $CI->config->item('port'); if($host && $port){ $this->config['host'] = $host; $this->config['port'] = $port; }else{ return false; } }else{ return false; } } $this->redis = new Redis(); $this->redis->connect($this->config['host'], $this->config['port']); } /** * 设置值 * @param string $key KEY名称 * @param string|array $value 获取得到的数据 * @param int $timeOut 时间 */ public function save($key, $value, $timeOut = 0) { $value = json_encode($value); $retRes = $this->redis->set($key, $value); if ($timeOut > 0) $this->redis->setTimeout($key, $timeOut); return $retRes; } /** * 通过KEY获取数据 * @param string $key KEY名称 */ public function get($key) { $result = $this->redis->get($key); return json_decode($result, TRUE); } /** * 获取一组Key * @param $key * @return mixed */ public function keys($key) { return $this->redis->keys($key); } /** * 删除一条数据 * @param string $key KEY名称 */ public function delete($key) { return $this->redis->delete($key); } /** * 清空数据 */ public function flushAll() { return $this->redis->flushAll(); } /** * 数据入队列 * @param string $key KEY名称 * @param string|array $value 获取得到的数据 * @param bool $right 是否从右边开始入 */ public function push($key, $value ,$right = true) { $value = json_encode($value); return $right ? $this->redis->rPush($key, $value) : $this->redis->lPush($key, $value); } /** * 数据出队列 * @param string $key KEY名称 * @param bool $left 是否从左边开始出数据 */ public function pop($key , $left = true) { $val = $left ? $this->redis->lPop($key) : $this->redis->rPop($key); return json_decode($val); } /** * 数据自增 * @param string $key KEY名称 */ public function increment($key) { return $this->redis->incr($key); } /** * 数据自减 * @param string $key KEY名称 */ public function decrement($key) { return $this->redis->decr($key); } /** * key是否存在,存在返回ture * @param string $key KEY名称 */ public function exists($key) { return $this->redis->exists($key); } /** * 数值+$num * @param $key * @param $num */ public function calculate($key,$num){ $result = $this->redis->get($key); $data = json_decode($result, TRUE); $this->redis->set($key,doubleval(($data?$data:0))+doubleval($num)); } public function hget($key,$filed) { return $this->redis->hGet($key,$filed); } public function hset($key,$filed,$count,$time_out=0){ $res = $this->redis->hSet($key,$filed,$count); if($time_out>0){ $this->redis->setTimeout($key,$time_out); } return $res; } /** * 返回redis对象 * redis有非常多的操作方法,我们只封装了一部分 * 拿着这个对象就可以直接调用redis自身方法 */ public function redis() { return $this->redis; } // public function setRedisFLK($shop_id,$order_id,$status,$order_info=array()){ // $this->redis->select(4); // $this->redis->hSet('FLK_order_status_'.$shop_id, $order_id, $status); // if(count($order_info)){ // $order_info_data = json_encode($order_info); // $this->redis->hSet('FLK_order_info_'.$shop_id, $order_id, $order_info_data); // } // } // // public function getRedisFLK($shop_id,$order_id){ // $this->redis->select(4); // $res['status'] = $this->redis->hGet('FLK_order_status_'.$shop_id,$order_id); // $res['order_info'] = $this->redis->hGet('FLK_order_info_'.$shop_id,$order_id); // return $res; // } // // // /** // * 增加品牌的评论数 // * @param $brand_id // */ // public function setBrandOrderComment($brand_id){ // $brand_id = "brand_id:".$brand_id; // $this->redis->select(4); // return $this->redis->hIncrBy('FLK_brand_order_comment',$brand_id,1); // } // //获取品牌的评论数 // public function getBrandOrderComment($brand_id){ // $brand_id = "brand_id:".$brand_id; // $this->redis->select(4); // $count = $this->redis->hGet('FLK_brand_order_comment',$brand_id); // return intval($count); // } // //设置品牌的好评 // public function setBrandOrderGoodComment($brand_id){ // $brand_id = "brand_id:".$brand_id; // $this->redis->select(4); // $this->redis->hIncrBy('FLK_brand_order_good_comment',$brand_id,1); // } // //获取品牌的好评 // public function getBrandOrderGoodComment($brand_id){ // $brand_id = "brand_id:".$brand_id; // $this->redis->select(4); // $count = $this->redis->hGet('FLK_brand_order_good_comment',$brand_id); // return intval($count); // } // // // // //设置服务的好评 // public function setItemGoodComment($item_id){ // $brand_id = "item_id:".$item_id; // $this->redis->select(4); // $this->redis->hIncrBy('FLK_item_good_comment',$brand_id,1); // } // //获取服务的好评 // public function getItemGoodComment($item_id){ // $brand_id = "item_id:".$item_id; // $this->redis->select(4); // $count = $this->redis->hGet('FLK_item_good_comment',$brand_id); // return intval($count); // } // //设置服务的评论 // public function setItemComment($item_id){ // $brand_id = "item_id:".$item_id; // $this->redis->select(4); // return $this->redis->hIncrBy('FLK_item_comment',$brand_id,1); // } // //获取服务的评论 // public function getItemComment($item_id){ // $brand_id = "item_id:".$item_id; // $this->redis->select(4); // $count = $this->redis->hGet('FLK_item_comment',$brand_id); // return intval($count); // } // // /** // * 设置服务好评总数 // * @param $item_id // * @param $count // */ // public function setItemTotalGoodComment($item_id,$count){ // $brand_id = "item_id:".$item_id; // $this->redis->select(4); // $this->redis->hSet('FLK_item_good_comment',$brand_id,$count); // } // /** // * 设置服务评论总数 // * @param $item_id // * @param $count // */ // public function setItemTotalComment($item_id,$count){ // $brand_id = "item_id:".$item_id; // $this->redis->select(4); // $this->redis->hSet('FLK_item_comment',$brand_id,$count); // } } ?>