相关文章推荐
仗义的铅笔  ·  前沿综述 | ...·  9 月前    · 
高大的香蕉  ·  JS Math.random() ...·  1 年前    · 
好帅的楼梯  ·  Dart 基本语法 - 简书·  1 年前    · 
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-

bucket.size() returns the size of object in bytes, not the length of the string. That just means asides from the 3 numbers each taking 1 byte, the object has 2 other bytes of data in it containing other information. – andytung Oct 8, 2018 at 23:32

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.