Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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();
The result is :
isUpdated:true
prevObject:4934
isSet:false
I'm puzzled about the usage for trySet
method, why is failed in this example, I don't find any explanation in the Redisson API document for this method, and another question is why the objectSize
is 5
? As the value of bucket is 321
now, I think the objectSize
should be 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.
So if the bucket already has an object, trySet will fail and return false. If the bucket is empty, trySet will succeed and set the value.
https://static.javadoc.io/org.redisson/redisson/3.4.1/org/redisson/api/RBuckets.html#trySet-java.util.Map-
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.