redis秩序化集合によるリアルタイムの読書ランキングの更新


取得コード:
    /**
     *      
     */
    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;
    }