Java Code Examples for org.redisson.api.RBucket#isExists()

The following examples show how to use org.redisson.api.RBucket#isExists() . 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: UserRepository.java    From j360-dubbo-app-all with Apache License 2.0 6 votes vote down vote up
/**
 * redission 作为二级缓存DAO层的案例
 * @param itemId
 * @return
 */
public UserDO getGoodsCacheable(long itemId) {
    try{
        RBucket<UserDO> rBucket = redissonClient.getBucket(String.format(UserKeys.USER_DO_ID, itemId, JsonJacksonCodec.INSTANCE));
        if (rBucket.isExists()) {
            rBucket.expire(AppConfig.COMMON_CACHE_DAYS, TimeUnit.MINUTES);
            return rBucket.get();
        }
        UserDO topicDO = getGoods(itemId);
        if (Objects.nonNull(topicDO)) {
            redissonClient.getBucket(String.format(UserKeys.USER_DO_ID, itemId),JsonJacksonCodec.INSTANCE)
                    .setAsync(topicDO, AppConfig.COMMON_CACHE_DAYS, TimeUnit.MINUTES);
        }
        return topicDO;
    }catch(Throwable th){
        throw new RepositoryException(ErrorCode.DB_ERROR.getErrorCode(),ErrorCode.DB_ERROR.getErrorMsg(),th);
    }

}
 
Example 2
Source File: UserRepository.java    From j360-dubbo-app-all with Apache License 2.0 6 votes vote down vote up
/**
 * redission 作为二级缓存DAO层的案例
 * @param id
 * @return
 */
public String getUserCacheable(long id) {
    try{
        RBucket<String> rBucket = redissonClient.getBucket(String.format(UserKeys.USER_DO_ID, id, JsonJacksonCodec.INSTANCE));
        if (rBucket.isExists()) {
            rBucket.expire(AppConfig.COMMON_CACHE_DAYS, TimeUnit.MINUTES);
            return rBucket.get();
        }
        String name = jdbcTemplate.queryForObject("select username from user where id = ?", String.class, new Object[]{id});
        if (Objects.nonNull(name)) {
            redissonClient.getBucket(String.format(UserKeys.USER_DO_ID, id),JsonJacksonCodec.INSTANCE)
                    .setAsync(name, AppConfig.COMMON_CACHE_DAYS, TimeUnit.MINUTES);
        }
        return name;
    }catch(Throwable th){
        throw new RepositoryException(ErrorCode.DB_ERROR.getErrorCode(),ErrorCode.DB_ERROR.getErrorMsg(),th);
    }

}