org.redisson.api.RBuckets Java Examples

The following examples show how to use org.redisson.api.RBuckets. 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: RedissonTransactionalBucketsTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSet() {
    RBucket<String> b1 = redisson.getBucket("test1");
    b1.set("1");
    RBucket<String> b2 = redisson.getBucket("test2");
    b2.set("2");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RBuckets buckets = transaction.getBuckets();
    Map<String, Object> bbs = new LinkedHashMap<>();
    bbs.put("test1", "11");
    bbs.put("test2", "22");
    buckets.set(bbs);
    
    Map<String, Object> newBuckets = buckets.get("test1", "test2");
    assertThat(newBuckets).isEqualTo(bbs);
    
    transaction.commit();
    
    assertThat(redisson.getBuckets().get("test1", "test2")).isEqualTo(bbs);
    assertThat(redisson.getKeys().count()).isEqualTo(2);
}
 
Example #2
Source File: BucketsTrySetOperation.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void commit(CommandAsyncExecutor commandExecutor) {
    RBuckets bucket = new RedissonBuckets(codec, commandExecutor);
    bucket.trySetAsync(values);
    
    unlock(commandExecutor);
}
 
Example #3
Source File: RedissonTransactionalBucketsTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testGet() {
    RBucket<String> b = redisson.getBucket("test");
    b.set("123");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RBuckets buckets = transaction.getBuckets();
    assertThat(buckets.get("test").get("test")).isEqualTo("123");
    
    transaction.commit();
    
    assertThat(redisson.getKeys().count()).isEqualTo(1);
    assertThat(b.get()).isEqualTo("123");
}
 
Example #4
Source File: RedissonTransactionalBucketsTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
    public void testTrySet() {
        redisson.getBucket("test1").set("1");
        redisson.getBucket("test2").set("2");
        
        RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
        RBuckets buckets = transaction.getBuckets();
        Map<String, Object> bbs1 = new LinkedHashMap<>();
        bbs1.put("test1", "10");
        bbs1.put("test2", "20");
        assertThat(buckets.trySet(bbs1)).isFalse();
        assertThat(buckets.delete("test1", "test2")).isEqualTo(2);
        Map<String, Object> bbs2 = new LinkedHashMap<>();
        bbs2.put("test1", "11");
        bbs2.put("test2", "22");
        assertThat(buckets.trySet(bbs2)).isTrue();
        Map<String, Object> bbs3 = new LinkedHashMap<>();
        bbs3.put("test1", "13");
        bbs3.put("test2", "23");
        assertThat(buckets.trySet(bbs3)).isFalse();
        
        System.out.println("commit " + Thread.currentThread().getId());
        transaction.commit();
        redisson.getKeys().getKeys().forEach(x -> System.out.println(x));
        
//        assertThat(redisson.getBuckets().get("test1", "test2")).isEqualTo(bbs2);
        assertThat(redisson.getKeys().count()).isEqualTo(2);
    }
 
Example #5
Source File: RedissonBucketsTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testCodec() {
    RBuckets buckets = redisson.getBuckets(StringCodec.INSTANCE);
    Map<String, String> items = buckets.get("buckets:A", "buckets:B", "buckets:C");

    items.put("buckets:A", "XYZ");
    items.put("buckets:B", "OPM");
    items.put("buckets:C", "123");

    buckets.set(items);
    items = buckets.get("buckets:A", "buckets:B", "buckets:C");
    Assert.assertEquals(3, items.size());
    Assert.assertEquals("XYZ", items.get("buckets:A"));
}
 
Example #6
Source File: TracingRBuckets.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
public TracingRBuckets(RBuckets buckets,
    TracingRedissonHelper tracingRedissonHelper) {
  this.buckets = buckets;
  this.tracingRedissonHelper = tracingRedissonHelper;
}
 
Example #7
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RBuckets getBuckets() {
  return new TracingRBuckets(redissonClient.getBuckets(), tracingRedissonHelper);
}
 
Example #8
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RBuckets getBuckets(Codec codec) {
  return new TracingRBuckets(redissonClient.getBuckets(codec), tracingRedissonHelper);
}
 
Example #9
Source File: UserRepository.java    From j360-dubbo-app-all with Apache License 2.0 4 votes vote down vote up
public List<UserDO> findIn(List<Long> ids) {
    try{
        ids = ids.stream().distinct().collect(Collectors.toList());
        ListUtil.sort(ids);
        RBuckets rBuckets = redissonClient.getBuckets(JsonJacksonCodec.INSTANCE);
        Map<String, UserDO> vpMaps = rBuckets.get(ModelUtil.formatStrings(UserKeys.USER_DO_ID, ids));

        List<UserDO> list = Lists.newArrayListWithCapacity(ids.size());
        List<Long> redisIds = Lists.newArrayListWithCapacity(ids.size());
        ids.stream().forEach( id -> {
            String key = String.format(UserKeys.USER_DO_ID, id);
            if (vpMaps.containsKey(key)) {
                UserDO topicDO = vpMaps.get(key);
                if (Objects.nonNull(topicDO)) {
                    list.add(topicDO);
                    redisIds.add(id);

                    //延迟过期
                    redissonClient.getBucket(key,JsonJacksonCodec.INSTANCE)
                            .expire(AppConfig.COMMON_CACHE_DAYS, TimeUnit.MINUTES);
                }
            }
        });

        List<Long> lastIds = ListUtils.removeAll(ids, redisIds);
        if (CollectionUtils.isNotEmpty(lastIds)) {
            List<UserDO> dbList =  list(lastIds);
            if (CollectionUtils.isNotEmpty(dbList)) {
                list.addAll(dbList);

                dbList.stream().forEach(topicDO -> {
                    redissonClient.getBucket(String.format(UserKeys.USER_DO_ID, topicDO.getUid()),JsonJacksonCodec.INSTANCE)
                            .setAsync(topicDO, AppConfig.COMMON_CACHE_DAYS, TimeUnit.MINUTES);
                });
            }
        }

        return list;
    }catch(Throwable th){
        throw new RepositoryException(ErrorCode.DB_ERROR.getErrorCode(),ErrorCode.DB_ERROR.getErrorMsg(),th);
    }
}
 
Example #10
Source File: RedissonTransaction.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RBuckets getBuckets() {
    checkState();
    
    return new RedissonTransactionalBuckets(commandExecutor, options.getTimeout(), operations, executed, id);
}
 
Example #11
Source File: RedissonTransaction.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public RBuckets getBuckets(Codec codec) {
    checkState();
    
    return new RedissonTransactionalBuckets(codec, commandExecutor, options.getTimeout(), operations, executed, id);
}