现有个需求直播功能模块添加点赞,一个用户可以疯狂点赞
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", "点赞内容成功");