Java Code Examples for org.springframework.data.redis.core.HashOperations#increment()

The following examples show how to use org.springframework.data.redis.core.HashOperations#increment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageServiceController.java    From sctalk with Apache License 2.0 4 votes vote down vote up
/**
 * 发送消息(群组消息 )
 * @param messageSendReq 群组消息
 * @return 消息ID
 * @since  1.0
 */
@PostMapping(path = "/groupMessage/add")
@Transactional
public BaseModel<Long> sendMessage(@RequestBody GroupMessageSendReq messageSendReq) {

    byte type = (byte) messageSendReq.getMsgType().getNumber();

    // 存储群消息(ID自增,计数)
    final String groupSetKey = RedisKeys.concat(RedisKeys.GROUP_INFO, messageSendReq.getGroupId(), RedisKeys.SETTING_INFO);

    final HashOperations<String, String, String> hashOptions = redisTemplate.opsForHash();
    final long msgId = hashOptions.increment(groupSetKey, RedisKeys.GROUP_MESSAGE_ID, 1);
    String content;
    if (messageSendReq.getMsgType() == IMBaseDefine.MsgType.MSG_TYPE_GROUP_AUDIO) {
        
        long audioId = audioInternalService.saveAudioInfo(messageSendReq.getUserId(), messageSendReq.getGroupId(),
                messageSendReq.getCreateTime(), messageSendReq.getContent());
        
        if (audioId == DBConstant.INVALIAD_VALUE) {
            // 录音保存失败
            return new BaseModel<Long>().setResult(NormarCmdResult.DFS_ERROR);
        }
        
        content = String.valueOf(audioId);
    } else {
        content = messageSendReq.getMsgContent();
    }
    IMGroupMessage groupMessageEntity = new IMGroupMessage();
    groupMessageEntity.setUserId(messageSendReq.getUserId());
    groupMessageEntity.setGroupId(messageSendReq.getGroupId());
    groupMessageEntity.setContent(content);
    groupMessageEntity.setCreated(messageSendReq.getCreateTime());
    groupMessageEntity.setMsgId(msgId);
    groupMessageEntity.setType(type);
    groupMessageEntity.setStatus(DBConstant.DELETE_STATUS_OK);
    groupMessageEntity.setUpdated(messageSendReq.getCreateTime());

    groupMessageRepository.save(groupMessageEntity);
    
    // 更新Session
    long sessionId = sessionService.getSessionId(messageSendReq.getUserId(), messageSendReq.getGroupId(),
            SessionType.SESSION_TYPE_GROUP_VALUE, false);
    if (sessionId == DBConstant.INVALIAD_VALUE) {
        sessionId = sessionService.addSession(messageSendReq.getUserId(), messageSendReq.getGroupId(),
                SessionType.SESSION_TYPE_GROUP_VALUE);
    }
    
    sessionService.update(sessionId, messageSendReq.getCreateTime());

    // 更新最后消息时间
    IMGroup group = groupRepository.findOne(messageSendReq.getGroupId());
    group.setLastChated(CommonUtils.currentTimeSeconds());
    groupRepository.save(group);

    // 计数
    hashOptions.increment(groupSetKey, RedisKeys.COUNT, 1);
    
    // 未读消息
    final String userUnreadKey = RedisKeys.concat(RedisKeys.GROUP_UNREAD, messageSendReq.getUserId());
    hashOptions.increment(userUnreadKey, String.valueOf(messageSendReq.getGroupId()), 1);

    return new BaseModel<Long>() {
        {
            setData(msgId);
        }
    };
}
 
Example 2
Source File: MessageServiceController.java    From sctalk with Apache License 2.0 4 votes vote down vote up
/**
 * 发送个人消息
 * @param messageSendReq 消息内容
 * @return 消息ID
 * @since  1.0
 */
@PostMapping(path = "/message/add")
@Transactional
public BaseModel<Long> sendMessage(@RequestBody MessageSendReq messageSendReq) {

    byte type = (byte) messageSendReq.getMsgType().getNumber();
    
    // 处理relation_id
    final Long relateId =
            relationShipService.getRelationId(messageSendReq.getUserId(), messageSendReq.getToId(), true);
    
    // 保存关系信息(消息ID,)
    final HashOperations<String, String, String> hashOptions = redisTemplate.opsForHash();
    final String relKey = RedisKeys.concat(RedisKeys.RELATION_INFO, relateId % 10000);
    Long msgId = hashOptions.increment(relKey, String.valueOf(relateId), 1);

    String content;
    if (messageSendReq.getMsgType() == IMBaseDefine.MsgType.MSG_TYPE_SINGLE_AUDIO) {
        long audioId = audioInternalService.saveAudioInfo(messageSendReq.getUserId(), messageSendReq.getToId(),
                messageSendReq.getCreateTime(), messageSendReq.getContent());
        if (audioId == DBConstant.INVALIAD_VALUE) {
            // 录音保存失败
            return new BaseModel<Long>().setResult(NormarCmdResult.DFS_ERROR);
        }
        
        content = String.valueOf(audioId);
    } else {
        content = messageSendReq.getMsgContent();
    }
    
    IMMessage messageEntity = new IMMessage();
    messageEntity.setUserId(messageSendReq.getUserId());
    messageEntity.setToId(messageSendReq.getToId());
    messageEntity.setContent(content);
    messageEntity.setCreated(messageSendReq.getCreateTime());
    messageEntity.setMsgId(msgId);
    messageEntity.setRelateId(relateId);
    messageEntity.setType(type);
    messageEntity.setStatus(DBConstant.DELETE_STATUS_OK);
    messageEntity.setUpdated(messageSendReq.getCreateTime());

    messageRepository.save(messageEntity);
    
    // 更新Session
    long sessionId = sessionService.getSessionId(messageSendReq.getUserId(), messageSendReq.getToId(),
            SessionType.SESSION_TYPE_SINGLE_VALUE, false);
    if (sessionId == DBConstant.INVALIAD_VALUE) {
        sessionId = sessionService.addSession(messageSendReq.getUserId(), messageSendReq.getToId(),
                SessionType.SESSION_TYPE_SINGLE_VALUE);
    }

    sessionService.update(sessionId, messageSendReq.getCreateTime());
    
    // 2019-7-9 update start
    // 参考\db_proxy_server\business\MessageContent.cpp sendMessage
    long peersessionId = sessionService.getSessionId(messageSendReq.getToId(), messageSendReq.getUserId(), 
            SessionType.SESSION_TYPE_SINGLE_VALUE, false);
    if (peersessionId == DBConstant.INVALIAD_VALUE) {
    	peersessionId = sessionService.addSession(messageSendReq.getToId(), messageSendReq.getUserId(),
                SessionType.SESSION_TYPE_SINGLE_VALUE);
    }

    // 2019-7-9 update end
    sessionService.update(peersessionId, messageSendReq.getCreateTime());

    // 计数
    // 存储用户信息及未读信息
    final String useUreadrKey = RedisKeys.concat(RedisKeys.USER_UNREAD, messageSendReq.getToId());
    hashOptions.increment(useUreadrKey, String.valueOf(messageSendReq.getUserId()), 1);

    BaseModel<Long> messageIdRes = new BaseModel<>();
    messageIdRes.setData(msgId);
    return messageIdRes;
}