zeng

zeng

may world peace

How to operate the five basic data types of Redis in Redisson

How to operate the five basic data types of Redis in Redisson

Redis supports five data types:

  1. string
  2. hash
  3. list
  4. set
  5. zset (sorted set)
string#

String is the most basic type in Redis, and the string type is binary safe. This means that Redis strings can contain any data, such as jpg images or serialized objects. The string type is the most fundamental data type in Redis, and the maximum value that can be stored in a string type is 512MB.

Redisson encapsulates the string data structure in Redis into RBucket, which can be obtained through the RedissonClient's getBucket(key) method. Through this instance, you can set the value or set the value along with the expiration time.

RBucket<Object> rBucket = redissonClient.getBucket("k1");
rBucket.set("v1", 500, TimeUnit.MILLISECONDS);
String sValue = (String) rBucket.get();
hash#

Redis hash is a collection of key-value (key=>value) pairs. Redis hash is a mapping table of string type fields and values, and hash is particularly suitable for storing objects. Hash is suitable for storing objects and can modify a specific attribute value like updating an attribute in a database (without needing to retrieve the entire string, deserialize it into an object, modify it, and then serialize it back), such as storing, reading, and modifying user attributes.

Redisson encapsulates the string data structure in Redis into RMap.

final RMap rMap = redissonClient.getMap("m1");
rMap.put("id", "1");
rMap.put("name", "jk");
rMap.expire(500, TimeUnit.MILLISECONDS);
String mValue = (String) rMap.get("name");
list#

Redis list is a simple list of strings, sorted in the order of insertion. You can add an element to the head (left) or tail (right) of the list. Lists can be used for recent message rankings, such as a timeline in a social media feed, or for message queues.

Redisson encapsulates the string data structure in Redis into RList.

final RList<Object> rList = redissonClient.getList("l1");
rList.add("tom");
rList.add("king");
rList.add("jack");
rList.expire(500, TimeUnit.MILLISECONDS);
String lValue = (String) rList.get(1);
set#

Redis set is an unordered collection of string types. The set is implemented through a hash table, so the complexity of adding, deleting, and searching is O(1). Sets are suitable for handling mutual friends, or utilizing uniqueness to count all distinct IPs visiting a website, and for friend recommendations based on tag intersections, recommending if above a certain threshold.

Redisson encapsulates the string data structure in Redis into RSet.

final RSet<Object> rSet = redissonClient.getSet("s1");
rSet.add("java");
rSet.add("javascript");
rSet.expire(500, TimeUnit.MILLISECONDS); // Get all values through key
redissonClient.getSet("s1");
zset#

Redis zset, like set, is also a collection of string type elements, and it does not allow duplicate members. The difference is that each element is associated with a double type score. Redis uses the score to sort the members of the collection in ascending order. The members of zset are unique, but the scores can be duplicated. Zset is suitable for leaderboards or weighted message queues.

Redisson encapsulates the string data structure in Redis into RScoredSortedSet.

RScoredSortedSet<Object> rScoredSortedSet = redissonClient.getScoredSortedSet("zs1");
rScoredSortedSet.addScore("tom", 3.0);
rScoredSortedSet.addScore("king", 3.5); // Get all values through key
redissonClient.getScoredSortedSet("zs1");
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.