| | |
| | | |
| | | private static final int PAGE_SIZE = 20; |
| | | |
| | | private static final String CACHEKEY_MSG_QUEUE = "msg_chatroom_msg_"; // 实时消息队列 |
| | | private static final String CACHEKEY_MESSAGE_A_COUNT = "msg_chatroom_message_count_"; // A类消息总数 |
| | | private static final String CACHEKEY_MESSAGE_HISTORY = "msg_chatroom_msg_history_"; //历史消息 |
| | | private static final String CACHEKEY_MSG_QUEUE = "msg_chatroom_msg_"; // 实时消息队列 |
| | | private static final String CACHEKEY_MESSAGE_A_COUNT = "msg_chatroom_message_count_"; // A类消息总数 |
| | | private static final String CACHEKEY_MESSAGE_HISTORY = "msg_chatroom_msg_history_"; //历史消息 |
| | | |
| | | private static final String CACHEKEY_ONLINE_COUNT = "msg_chatroom_online_count_"; // 在线人数 |
| | | private static final String CACHEKEY_PRAISE_COUNT = "msg_chatroom_praise_count_"; // 点赞数 |
| | | private static final String CACHEKEY_LOTTERY_COUNT = "msg_chatroom_lottery_count_"; // 签到数 |
| | | private static final String CACHEKEY_VIEW_COUNT = "msg_chatroom_view_count_"; // 浏览数 |
| | | private static final String CACHEKEY_LOTTERY_RESULT_COUNT = "msg_chatroom_lottery_result_count_"; //中奖结果 |
| | | private static final String CACHEKEY_REWARD_COUNT = "msg_chatroom_reward_count_"; //打赏次数 |
| | | private static final String CACHEKEY_CONSULT_COUNT = "msg_chatroom_consult_count_"; //咨询次数 |
| | | private static final String CACHEKEY_REWARD_AMOUNT = "msg_chatroom_reward_amount_"; //打赏金额 |
| | | private static final String CACHEKEY_ONLINE_COUNT = "msg_chatroom_online_count_"; // 在线人数 |
| | | private static final String CACHEKEY_PRAISE_COUNT = "msg_chatroom_praise_count_"; // 点赞数 |
| | | private static final String CACHEKEY_LOTTERY_COUNT = "msg_chatroom_lottery_count_"; // 签到数 |
| | | private static final String CACHEKEY_VIEW_COUNT = "msg_chatroom_view_count_"; // 浏览数 |
| | | private static final String CACHEKEY_LOTTERY_RESULT_COUNT = "msg_chatroom_lottery_result_count_"; //中奖结果 |
| | | private static final String CACHEKEY_REWARD_COUNT = "msg_chatroom_reward_count_"; //打赏次数 |
| | | private static final String CACHEKEY_CONSULT_COUNT = "msg_chatroom_consult_count_"; //咨询次数 |
| | | private static final String CACHEKEY_REWARD_AMOUNT = "msg_chatroom_reward_amount_"; //打赏金额 |
| | | |
| | | /** |
| | | * 发送消息 |
| | | * 发送消息 |
| | | */ |
| | | |
| | | @Override |
| | |
| | | mes.setImgPath(imgPath); |
| | | mes.setCreateTime(new Date()); |
| | | |
| | | //缓存消息 |
| | | //缓存消息 |
| | | this.msgCache(chatroomId, mes); |
| | | this.msgTypeCountCache(chatroomId, type); |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * 聊天消息剪切, |
| | | * 聊天消息剪切, |
| | | * |
| | | * @param chatroomId |
| | | */ |
| | |
| | | } |
| | | index ++; |
| | | } |
| | | //保留20条 |
| | | //保留20条 |
| | | redis.lstTrim(key, mesList.size() - index <= 20?mesList.size()-20:index, -1l); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 缓存消息 |
| | | * 缓存消息 |
| | | * |
| | | * @param chatroomId |
| | | * @param mes |
| | |
| | | private void msgCache(String chatroomId, MsgChatroomMsg mes){ |
| | | String key = CACHEKEY_MSG_QUEUE + chatroomId; |
| | | |
| | | //先获取10秒内的数据 |
| | | //先获取10秒内的数据 |
| | | // this.msgListTrim(key, chatroomId); |
| | | // List<MsgChatroomMsg> mesList = redis.lstRange(key, 0l, -1l); |
| | | |
| | | //获取PAGE_SIZE条以内的消息 |
| | | //获取PAGE_SIZE条以内的消息 |
| | | List<MsgChatroomMsg> mesList= redis.lstRange(key, 0l, PAGE_SIZE); |
| | | |
| | | //最近20消息过滤重复的点赞,进入,退出 |
| | | //最近20消息过滤重复的点赞,进入,退出 |
| | | if((MsgChatroomMsg.TYPE_PRAISE == mes.getType() || MsgChatroomMsg.TYPE_ENTER == mes.getType() || MsgChatroomMsg.TYPE_EXIT == mes.getType()) |
| | | && mesList.contains(mes)){ |
| | | return; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 计数器 |
| | | * 计数器 |
| | | * |
| | | * @param chatroomId |
| | | * @param type |
| | | */ |
| | | private void msgTypeCountCache(String chatroomId, Integer type){ |
| | | if(type == MsgChatroomMsg.TYPE_ENTER){//在线人数加一 |
| | | if(type == MsgChatroomMsg.TYPE_ENTER){//在线人数加一 |
| | | redis.incr(CACHEKEY_ONLINE_COUNT + chatroomId); |
| | | redis.expire(CACHEKEY_ONLINE_COUNT + chatroomId, 360, TimeUnit.DAYS); |
| | | } |
| | |
| | | || type == MsgChatroomMsg.TYPE_REWARD || type == MsgChatroomMsg.TYPE_CONSULT || type == MsgChatroomMsg.TYPE_LOTTERY_RESULT){ |
| | | redis.incr(this.getKey(chatroomId, type)); |
| | | redis.expire(this.getKey(chatroomId, type), 360, TimeUnit.DAYS); |
| | | }else if(type == MsgChatroomMsg.TYPE_EXIT){//在线人数减一 |
| | | }else if(type == MsgChatroomMsg.TYPE_EXIT){//在线人数减一 |
| | | String key = CACHEKEY_ONLINE_COUNT + chatroomId; |
| | | Long value = redis.getIncrValue(key); |
| | | if(value.compareTo(0l)>0){ |
| | | redis.decr(key); |
| | | } |
| | | redis.expire(key, 360, TimeUnit.DAYS); |
| | | }else if(type == MsgChatroomMsg.TYPE_NORMAL || type == MsgChatroomMsg.TYPE_LOTTERY_RESULT){//A类消息计数器 |
| | | }else if(type == MsgChatroomMsg.TYPE_NORMAL || type == MsgChatroomMsg.TYPE_LOTTERY_RESULT){//A类消息计数器 |
| | | redis.incr(CACHEKEY_MESSAGE_A_COUNT + chatroomId); |
| | | redis.expire(CACHEKEY_MESSAGE_A_COUNT + chatroomId, 360, TimeUnit.DAYS); |
| | | } |
| | | } |
| | | |
| | | //获取缓存key值 |
| | | //获取缓存key值 |
| | | private String getKey(String chatroomId, Integer type){ |
| | | String key = ""; |
| | | switch(type){ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 轮询 |
| | | * 轮询 |
| | | * |
| | | * @param chatroomId |
| | | * @throws Exception |
| | |
| | | public Result latestMsg(String chatroomId) { |
| | | List<MsgChatroomMsg> mesList = redis.lstRange(CACHEKEY_MSG_QUEUE + chatroomId, 0l, -1l); |
| | | |
| | | //奖金总数 |
| | | //奖金总数 |
| | | Long rewardAmount = redis.getIncrValue(CACHEKEY_REWARD_AMOUNT + chatroomId); |
| | | |
| | | Map<String, Object> data = new HashMap<String, Object>(); |
| | | //统计数据 |
| | | //统计数据 |
| | | Map<String,Long> statistics = this.reStatistics(chatroomId); |
| | | Integer maxPage = this.getMaxPage(chatroomId, String.valueOf(statistics.get("historyMsgCount"))); |
| | | data.put("maxPage", maxPage); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取浏览人次 |
| | | * 获取浏览人次 |
| | | * @param chatroomId |
| | | * @throws Exception |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * 重新统计数据,读取数据库 |
| | | * 重新统计数据,读取数据库 |
| | | * |
| | | * @param type |
| | | * @param statistics |
| | |
| | | |
| | | Map<String, Long> result = new HashMap<>(); |
| | | result.put("msgCount", msgCount); |
| | | //历史消息数量,正常消息、中奖消息,打赏消息 |
| | | //历史消息数量,正常消息、中奖消息,打赏消息 |
| | | result.put("historyMsgCount", msgCount+lotteryResultCount+rewardCount); |
| | | result.put("onlineCount", onlineCount); |
| | | result.put("praiseCount", praiseCount); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 按页获取历史消息 |
| | | * 按页获取历史消息 |
| | | */ |
| | | @Override |
| | | public Result getHistoryMes(String chatroomId, Integer pageNo) |
| | | { |
| | | //统计数据 |
| | | //统计数据 |
| | | List<MsgChatroomMsg> mesList = redis.lstRange(CACHEKEY_MSG_QUEUE + chatroomId, 0l, -1l); |
| | | Map<String,Long> statistics = this.reStatistics(chatroomId); |
| | | Integer maxPage = getMaxPage(chatroomId, String.valueOf(statistics.get("historyMsgCount")+mesList.size())); |
| | | String key = CACHEKEY_MESSAGE_HISTORY + chatroomId+ "_" + pageNo; |
| | | if (StringUtils.isEmpty(chatroomId) || pageNo == null |
| | | || pageNo > maxPage) { |
| | | return new Result(false, "参数错误"); |
| | | return new Result(false, "参数错误"); |
| | | } |
| | | List<MsgChatroomMsg> msgList = redis.lstAll(key); |
| | | msgList = this.distinct(msgList, chatroomId); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 去除重复消息 |
| | | * 去除重复消息 |
| | | * |
| | | * @return |
| | | */ |
| | |
| | | |
| | | |
| | | /** |
| | | * 获取A类消息最大页码 |
| | | * 获取A类消息最大页码 |
| | | * |
| | | * @param chatroomId |
| | | * @return |