How to operate Redis five basic data types in Redisson
Redis supports five data types:
- string
- hash
- list
- set
- zset(sorted set)
string#
String is the most basic type in Redis, and it is binary-safe. This means that Redis strings can contain any data, such as jpg images or serialized objects. The maximum value that can be stored in a string is 512MB.
Redisson encapsulates the string data structure in Redis into an RBucket. You can obtain an RBucket object instance by using the getBucket(key) method of RedissonClient, and through this instance, you can set the value or set the value and 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 pairs. Redis hash is a mapping table of string type fields and values. Hash is especially suitable for storing objects. Hash is suitable for storing objects and can be used to update a property in the same way as in a database, without retrieving the entire string, deserializing it into an object, modifying it, and then serializing it back.
Redisson encapsulates the string data structure in Redis into an 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 string list that is sorted in the order of insertion. You can add an element to the head (left) or tail (right) of the list. List can be used for latest news ranking, such as the timeline of a friend circle, or message queues.
Redisson encapsulates the string data structure in Redis into an 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 type elements. Sets are implemented using hash tables, so the complexity of adding, deleting, and searching is O(1). Set is suitable for handling common friends, or using uniqueness to count all unique IP addresses accessing a website, and recommending friends based on tags, where the intersection is greater than a certain threshold.
Redisson encapsulates the string data structure in Redis into an RSet.
final RSet<Object> rSet = redissonClient.getSet("s1");
rSet.add("java");
rSet.add("javascript");
rSet.expire(500,TimeUnit.MILLISECONDS);
zset#
Redis zset is also a collection of string type elements, just like set, but 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 in the set from small to large. The members of zset are unique, but the score can be repeated. Zset is suitable for leaderboards or message queues with weights.
Redisson encapsulates the string data structure in Redis into an RScoredSortedSet.
RScoredSortedSet<Object> rScoredSortedSet = redissonClient.getScoredSortedSet("zs1");
rScoredSortedSet.addScore("tom",3.0);
rScoredSortedSet.addScore("king",3.5);