直播点赞高QPS的优化-redis

现有个需求直播功能模块添加点赞,一个用户可以疯狂点赞

redis处理高QPS点赞操作,逻辑优化处理

原有逻辑,添加点赞,更新点赞次数,每次都要更新mysql,这样会频繁的操作数据库,

优化逻辑, 将点赞数据,点赞数据的id ,和点赞人多,全部存在redis中,等30分钟后将redis中的数据同步一次到数据库中

数据持久化

redis的数据定期写回mysql中

/**
     * 定时任务将直播点赞数同步到mysql中
     * */
    @Scheduled(cron = "0 0/30 * * * ?")
    public void updateContentSortOrder999() {
        long startTime1 = System.currentTimeMillis(); //获取结束时间
        liveRoomService.updateLiveRoomTopCountRedisToMysql();
        long endTime1 = System.currentTimeMillis(); //获取结束时间
        System.out.println("-----------------定时任务将直播点赞数同步到mysql中-----------------,程序运行时间: "+ (endTime1 - startTime1) + "ms");
    }
@Override
    public void updateLiveRoomTopCountRedisToMysql(){
        String liveRoomRedisKey = RedisKeyConstants.CONTENT_REDIS_FOR_LIVEROOM_DETIAL;
        Set<String> liveRoomKeys = redisService.getPrefixkeys(liveRoomRedisKey);
        System.out.println("直播间点赞量--定时存入到mysql中>>>>>>size:" + liveRoomKeys.size());
        if(null!=liveRoomKeys&&liveRoomKeys.size()>0) {
            for (String key : liveRoomKeys) {
                JSONObject json = JSON.parseObject((String) redisService.get(key));

                Integer contentId = json.getIntValue("contentId");
                Integer topCount = json.getIntValue("topCount");

                LiveRoom liveRoom = findLiveRoom(contentId);
                Integer oldTopCount = liveRoom.getTopCount();
                Integer oldShowTopCount = liveRoom.getShowTopCount();
                if (liveRoom != null) {
                    if (liveRoom.getTopCount() != null) {
                        liveRoom.setTopCount(oldTopCount + topCount);
                    } else {
                        liveRoom.setTopCount(topCount);
                    }

                    if (liveRoom.getShowTopCount() != null) {
                        liveRoom.setShowTopCount(oldShowTopCount + topCount);
                    } else {
                        liveRoom.setShowTopCount(topCount);
                    }
                    this.updateLiveRoom(liveRoom);
                    redundantFieldService.upDateTopCountAndPlayCount(contentId, 4, liveRoom.getShowTopCount(), liveRoom.getShowPlayCount());

//清除当前记录
                redisService.remove(key);
                }

            }
        }

    }

添加点赞逻辑:

LiveRoom liveRoom = this.liveRoomService.findLiveRoom(contentId);
                if (liveRoom != null) {
                    contentExistFlag = true;

                    Integer topCount = 0;
                    Object redisResult = redisService.get(liveRoomRedisKey);
                    if (redisResult != null) {

                        /**
                         {
                         "contentType":4,
                         "contentId":11,
                         "topCount":2
                         }
                         * */

                        result = JSONObject.parseObject((String) redisResult);
                        topCount = result.getInteger("topCount");
                    }else{
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("contentType",contentType);
                        jsonObject.put("contentId",contentId);
                        jsonObject.put("topCount",1);
                        topCount = 1;
                        redisService.set(liveRoomRedisKey,jsonObject.toString());
                    }

                   /* if (liveRoom.getTopCount() != null) {
                        liveRoom.setTopCount(liveRoom.getTopCount() + 1);
                    } else {
                        liveRoom.setTopCount(1);
                    }

                    if (liveRoom.getShowTopCount() != null) {
                        liveRoom.setShowTopCount(liveRoom.getShowTopCount() + 1);
                    } else {
                        liveRoom.setShowTopCount(1);
                    }
                    this.liveRoomService.updateLiveRoom(liveRoom);
                    redundantFieldService.upDateTopCountAndPlayCount(contentId, 4, liveRoom.getShowTopCount(), liveRoom.getShowPlayCount());
                    */
                    dataJson.put("showTopCount", liveRoom.getShowTopCount() != null ? liveRoom.getShowTopCount()+topCount : 0+topCount);
                    dataJson.put("topCount", liveRoom.getTopCount() != null ? liveRoom.getTopCount()+topCount : 0+topCount);
                    dataJson.put("suc", 1);
                    dataJson.put("message", "点赞内容成功");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值