我现在正在学习Redisson,我有一个例子如下:
public class TestRedisson {
public static void main(String[] args) {
Config config = new Config();
config.useSingleServer().setAddress("//localhost:6379");
RedissonClient redisson = Redisson.create(config);
RBucket<String> bucket = redisson.getBucket("test");
bucket.set("123");
boolean isUpdated = bucket.compareAndSet("123", "4934");
System.out.println("isUpdated:" + isUpdated);
System.out.println(bucket.get());
String prevObject = bucket.getAndSet("321");
System.out.println("prevObject:" + prevObject);
System.out.println(bucket.get());
boolean isSet = bucket.trySet("901");
System.out.println("isSet:" + isSet);
System.out.println(bucket.get());
long objectSize = bucket.size();
System.out.println(objectSize);
redisson.shutdown();
}
}
结果是:
isUpdated:true
4934
prevObject:4934
321
isSet:false
321
5
我对trySet
方法的用法感到困惑,为什么在这个例子中失败了,我在Redisson API文档中没有找到这个方法的任何解释,另一个问题是为什么objectSize
是 5
?由于现在bucket的值为321
,我认为objectSize
应该为3
。
最佳答案
我自己也在研究这个。
从文档中可以看出:
Try to save objects mapped by Redis key. If at least one of them is already exist then don't set none of them.
所以如果bucket已经有一个对象,trySet将会失败并返回false。如果桶为空,trySet就会成功并设置值。
关于redis - redisson rbucket中的trySet方法有什么用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51276593/