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

The following examples show how to use org.springframework.data.redis.core.HashOperations#get() . 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: DistributedlockApplicationTests.java    From java-tutorial with MIT License 6 votes vote down vote up
@Test
public void hash() throws InterruptedException {
    HashOperations hashOperations = redisTemplate.opsForHash();
    Info info1 = new Info(1001, "Hong");
    Info info2 = new Info(1002, "Kong");
    //is exist
    if (hashOperations.getOperations().hasKey("info_1001")) {
        //delete
        hashOperations.delete("info_1001", "1001");
        hashOperations.delete("info_1002", "1002");
        Thread.sleep(3000);
    }
    //put
    hashOperations.put("info_1001", "1001", info1);
    hashOperations.put("info_1002", "1002", info2);
    //get
    Info info = (Info) hashOperations.get("info_1001", "1001");

    System.out.println();
    System.out.println(info);


}
 
Example 2
Source File: MessageServiceController.java    From sctalk with Apache License 2.0 6 votes vote down vote up
/**
 * 消息用户计数
 * @param userCountReq 会话信息
 * @return 更新结果
 * @since  1.0
 */
@PostMapping("/clearUserCounter")
public BaseModel<?> clearUserCounter(@RequestBody ClearUserCountReq userCountReq) {

    HashOperations<String, String, String> hashOptions = redisTemplate.opsForHash();

    if (userCountReq.getSessionType() == IMBaseDefine.SessionType.SESSION_TYPE_SINGLE) {
        // Clear P2P msg Counter
        final String userKey = RedisKeys.concat(RedisKeys.USER_UNREAD, userCountReq.getUserId());
        hashOptions.delete(userKey, String.valueOf(userCountReq.getPeerId()));
    } else if (userCountReq.getSessionType() == IMBaseDefine.SessionType.SESSION_TYPE_GROUP) {
        // Clear Group msg Counter
        final String groupSetKey = RedisKeys.concat(RedisKeys.GROUP_INFO, userCountReq.getPeerId(), RedisKeys.SETTING_INFO);
        final String countValue = hashOptions.get(groupSetKey, RedisKeys.COUNT);

        final String userUnreadKey = RedisKeys.concat(RedisKeys.GROUP_UNREAD, userCountReq.getUserId());
        hashOptions.put(userUnreadKey, String.valueOf(userCountReq.getPeerId()), countValue);
    } else {
        logger.warn("参数不正: SessionType={}", userCountReq.getSessionType());
    }
    return null;
}
 
Example 3
Source File: RedisHashServiceImpl.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
@Override
public <T> List<T> getValueByField(String key, String field) {
	HashOperations<String, String, T> hash = rt.opsForHash();
	if (!rt.hasKey(key)) {
		return Collections.emptyList();
	}
	T value = hash.get(key, field);
	if (PublicUtil.isEmpty(value)) {
		return Collections.emptyList();
	}
	List<T> values = Lists.newArrayList();
	values.add(value);
	log.info("getValueByField - 根据key获取给定字段的值. [OK] key={}, field={}, values={}", key, field, values);
	return values;
}
 
Example 4
Source File: UserTokenServiceImpl.java    From sctalk with Apache License 2.0 5 votes vote down vote up
@Override
public String getToken(long userId) {
    HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
    String key = RedisKeys.concat(RedisKeys.USER_INFO, userId);
    
    return opsHash.get(key, RedisKeys.USER_TOKEN);
}
 
Example 5
Source File: UserTokenServiceImpl.java    From sctalk with Apache License 2.0 5 votes vote down vote up
@Override
public String getToken(String userId) {
    HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
    String key = RedisKeys.concat(RedisKeys.USER_INFO, userId);
    
    return opsHash.get(key, RedisKeys.USER_TOKEN);
}
 
Example 6
Source File: LoginServiceController.java    From sctalk with Apache License 2.0 5 votes vote down vote up
@GetMapping(path = "/login/queryPushShield")
public BaseModel<Integer> queryPushShield(@RequestParam("userId") long userId) {
    
    String key = RedisKeys.concat(RedisKeys.USER_INFO, userId); 
    HashOperations<String, String, String> userMapOps = redisTemplate.opsForHash();
    String shieldStatus = userMapOps.get(key, RedisKeys.USER_SHIELD);
    
    BaseModel<Integer> res = new BaseModel<Integer>();
    if (shieldStatus != null) {
        res.setData(Integer.valueOf(shieldStatus));
    }
    return res;
}
 
Example 7
Source File: AbsRedisDao.java    From jeesupport with MIT License 5 votes vote down vote up
@Override
public T findById ( int _idx, ID _value, Class< T > _cls ) {
    try{
        database( _idx );
        HashOperations< String, ID, T > hash = tpl.opsForHash();
        Object obj = hash.get( _cls.getSimpleName(), _value );
        return ( T ) obj;
    }catch ( Exception e ) {
        log.error( "findById 发生错误:IDX=[" + _idx + "]" + e.toString(), e );
        throw e;
    }
}
 
Example 8
Source File: RedisSupport.java    From mykit-delay with Apache License 2.0 4 votes vote down vote up
public String getHashKey(String key, String mapKey) {
    HashOperations<String, String, String> hashOperations = template.opsForHash();
    return hashOperations.get(key, mapKey);
}
 
Example 9
Source File: RedisCacheUtils.java    From sophia_scaffolding with Apache License 2.0 4 votes vote down vote up
public T getMap(String key, String mapkey) {
    HashOperations<String, String, T> hashOperations = redisTemplate.opsForHash();
    return hashOperations.get(key, mapkey);
}
 
Example 10
Source File: RedisCacheUtils.java    From sophia_scaffolding with Apache License 2.0 4 votes vote down vote up
public T getMap(String key, String mapkey) {
    HashOperations<String, String, T> hashOperations = redisTemplate.opsForHash();
    return hashOperations.get(key, mapkey);
}
 
Example 11
Source File: RedisCacheUtils.java    From sophia_scaffolding with Apache License 2.0 4 votes vote down vote up
public T getMap(String key, String mapkey) {
    HashOperations<String, String, T> hashOperations = redisTemplate.opsForHash();
    return hashOperations.get(key, mapkey);
}
 
Example 12
Source File: RedisSupport.java    From sdmq with Apache License 2.0 4 votes vote down vote up
public String getHashKey(String key, String mapKey) {
    HashOperations<String, String, String> hashOperations = template.opsForHash();
    return hashOperations.get(key, mapKey);
}
 
Example 13
Source File: CacheServiceProvider.java    From AsuraFramework with Apache License 2.0 4 votes vote down vote up
@Override
public Object getHash(final String key, final String hashKey) {
	final HashOperations<String, Object, Object> operation = redisTemplate.opsForHash();
	final Object value = operation.get(getKey(key), hashKey);
	return value;
}
 
Example 14
Source File: MessageServiceImpl.java    From sctalk with Apache License 2.0 4 votes vote down vote up
@Override
@Transactional
public List<UnreadEntity> getUnreadGroupMsgCount(long userId) {
    // 查询GroupID
    SearchCriteria<IMGroupMember> groupMemberSearchCriteria = new SearchCriteria<>();
    groupMemberSearchCriteria.add(JpaRestrictions.eq("userId", userId, false));
    groupMemberSearchCriteria.add(JpaRestrictions.eq("status", DBConstant.DELETE_STATUS_OK, false));
    Sort sort = new Sort(Sort.Direction.DESC, "updated", "id");
    List<IMGroupMember> groupList = groupMemberRepository.findAll(groupMemberSearchCriteria, sort);

    List<UnreadEntity> unreadList = new ArrayList<>();

    HashOperations<String, String, String> hashOptions = redisTemplate.opsForHash();
    if (!groupList.isEmpty()) {
        for (IMGroupMember group : groupList) {
            
            final String groupSetKey = RedisKeys.concat(RedisKeys.GROUP_INFO, group.getGroupId(), RedisKeys.SETTING_INFO);
            final String userUnreadKey = RedisKeys.concat(RedisKeys.GROUP_UNREAD, userId);
            
            // 取群消息数和用户已读数
            String groupCount = hashOptions.get(groupSetKey, RedisKeys.COUNT);
            if (groupCount == null) {
                continue;
            }
            String userCount =
                    hashOptions.get(userUnreadKey, String.valueOf(group.getGroupId()));
            
            Integer unreadCount = userCount != null ? Integer.valueOf(groupCount) - Integer.valueOf(userCount)
                    : Integer.valueOf(groupCount);
            
            if (unreadCount > 0) {

                // 取最后一条记录的消息
                IMGroupMessageEntity lastMessage = null;
                
                SearchCriteria<IMGroupMessage> groupMessageSearchCriteria = new SearchCriteria<>();
                groupMessageSearchCriteria.add(JpaRestrictions.eq("groupId", group.getGroupId(), false));
                groupMessageSearchCriteria.add(JpaRestrictions.eq("status", DBConstant.DELETE_STATUS_OK, false));
                Sort sortMessage = new Sort(Sort.Direction.DESC, "created", "id");
                Pageable pageable = new PageRequest(0, 1, sortMessage);
                Page<IMGroupMessage> groupMessageList =
                        groupMessageRepository.findAll(groupMessageSearchCriteria, pageable);
                if (groupMessageList.hasContent()) {
                    lastMessage = groupMessageList.getContent().get(0);
                }

                UnreadEntity unreadEntity = new UnreadEntity();
                unreadEntity.setPeerId(group.getGroupId());
                unreadEntity.setSessionType(IMBaseDefine.SessionType.SESSION_TYPE_GROUP_VALUE);
                unreadEntity.setLaststMsgType(lastMessage.getType());
                unreadEntity.setUnReadCnt(unreadCount);
                if (lastMessage != null) {
                    unreadEntity.setLaststMsgId(lastMessage.getMsgId());
                    unreadEntity.setLaststMsgType(lastMessage.getType());
                    unreadEntity.setLatestMsgFromUserId(lastMessage.getUserId());
                    if (lastMessage.getType() == IMBaseDefine.MsgType.MSG_TYPE_GROUP_TEXT_VALUE) {
                        unreadEntity.setLatestMsgData(lastMessage.getContent());
                    } else if (lastMessage.getType() == IMBaseDefine.MsgType.MSG_TYPE_GROUP_AUDIO_VALUE) {
                        // "[语音]"加密后的字符串
                        byte[] content = SecurityUtils.getInstance().EncryptMsg("[语音]");
                        unreadEntity.setLatestMsgData(Base64Utils.encodeToString(content));
                    } else {
                        // 其他
                        unreadEntity.setLatestMsgData(lastMessage.getContent());
                    }
                }
                unreadList.add(unreadEntity);
            }
        }
    }

    return unreadList;
}
 
Example 15
Source File: RedisServiceImpl.java    From SpringBoot-Base-System with GNU Lesser General Public License v3.0 3 votes vote down vote up
/**
 * 查询哈希表 hKey 中给定域 hashKey 的值.
 * 
 * @tim 下午9:56:00
 * 
 * @version V1.0
 * @param hKey
 * @param hashKey
 * @return 给定K的V
 */
@Override
public List<String> hashGet(String hKey, Integer hashKey) {
	HashOperations<Object, Integer, List<String>> hashOps = redisTemplate.opsForHash();
	List<String> i = hashOps.get(hKey, hashKey);
	return i;
}