php hyperf 读取redis,存储到数据库

背景说明
小白:伟哥,java中的set是无序的,Redis中可以带顺序吗?

伟哥:可以, 不过不叫set了,叫zset。

概述
SortedSet又叫zset,它是Redis提供的特殊数据类型,是一种特殊的set类型,继承了set不可重复的特点,并在set基础上为每个值添加一个分数,用来实现值的有序排列。

在这里插入图片描述

redis中排行榜中的数据
在这里插入图片描述

在这里插入图片描述

    public function execute(string $date)
    {
        $query = ChannelConfig::query();
        $query->where('module_name', 'rank_reward');
        $rewardData = $query->first();
        $rewards = [];
        if( $rewardData ){
            $rewardContents = $rewardData->content;
            foreach ($rewardContents as $key => $value) {
                if( !key_exists($value["game_type"], $rewards) ){
                    $rewards[$value["game_type"]] = [];
                }
                $rewards[$value["game_type"]][$value["rank"]] = $value["reward"];
            }
        }
        echo json_encode($rewards);

        $redisFactory = ApplicationContext::getContainer()->get(RedisFactory::class);
        $redis = $redisFactory->get('rank');

        $day = substr($date,5,2).substr($date,8,2);
        $keys = $redis->keys("RANK:*:$day:0-0");
        foreach ($keys as $key => $value) {
            // echo "key = $value \n";
            $kuCuns = $redis->ZREVRANGE($value, 0, 29, true);
            // echo "data = ".json_encode($kuCuns)."\n";
            $gameType = explode(":",$value)[1];
            $count = count($kuCuns);
            $index = 1;
            foreach ($kuCuns as $keyItem => $valueItem) {
                $mode = new RankLog();
                $mode->date = $date;
                $mode->index = $index;
                $mode->gameType = $gameType;
                $mode->userId = $keyItem;
                $mode->score = $valueItem;
                $mode->reward = key_exists($gameType, $rewards) ? $rewards[$gameType][$index] : "";
                $mode->save();
                $index++;
            }
        }
    }

转自:https://zhuanlan.zhihu.com/p/629389490