获取代码:
/**
* 获取排行榜
*/
public function getHotImage($num=6)
{
//获取redis排行榜有序集合
$hotImageIds = $this->getHotImageIds($num);
$ids = implode(',',$hotImageIds);
$redisKey = CommonConst::MODULE_ID . __METHOD__ . md5($ids);
if ($this->publicRedis->exists($redisKey)) {
return json_decode($this->publicRedis->get($redisKey), true);
}
$pic_list = $this->publicDao->getListByIds($ids);
if($pic_list['data']){
$this->publicRedis->setex($redisKey, 10 * 24 * 3600, json_encode($pic_list));
}
return $pic_list;
}
/**
* 获取排行榜ID
*/
public function getHotImageIds($num=6)
{
//获取redis排序集合
$hot_redisKey = CommonConst::MODULE__ID .'_'. __METHOD__;
$hotImageIds = array();
if ($this->publicRedis->exists($hot_redisKey)) {
return $this->publicRedis->zRevRange($hot_redisKey,0,-1);
}
$images = $this->publicDao->getList(array('status'=>1),array('view_num'=>'desc'),array(0,$num));
if($images){
foreach($images as $val){
$this->publicRedis->zAdd($hot_redisKey, $val['view_num'], $val['id']);
}
$hotImageIds = $this->publicRedis->zRevRange($hot_redisKey,0,-1);
}
return $hotImageIds;
}
更新点击量
/**
* 增加点击数
*/
public function addHits($image_id,$now_hits,$hits=1){
$ret = $this->publicDao->update(array('id'=>$image_id),array('view_num'=>$now_hits+$hits));
if($ret){
$hot_redisKey = CommonConst::MODULE_ID . '_Bn_ImagesService::getHotImageIds';
//获取现有点击数
$score = $this->publicRedis->zScore($hot_redisKey,$image_id);
if($score){
//如果有,incr增加1
$this->publicRedis->zIncrBy($hot_redisKey,$hits,$image_id);
}else{
//查询排行列表是否有小于等于当前播放量的数据
$less_member = $this->publicRedis->zRangeByScore($hot_redisKey,0,$now_hits,array('limit' => array(0, 1)));
if($less_member){
//删除小于当前播放量的元素
$this->publicRedis->zDelete($hot_redisKey,$less_member[0]);
//将当前元素增加至集合
$this->publicRedis->zAdd($hot_redisKey, $now_hits+$hits,$image_id);
}
}
}
return $ret;
}