redisson rbucket中的trySet方法有什么用
what's the usage for trySet method in redisson rbucket
我现在正在学习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.
因此,如果桶中已经有一个对象,trySet 将失败并且 return false。如果bucket为空,trySet会成功并设置值。
我现在正在学习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.
因此,如果桶中已经有一个对象,trySet 将失败并且 return false。如果bucket为空,trySet会成功并设置值。