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

发布时间:2023年12月18日

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://blog.csdn.net/wwj256/article/details/135065961
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。