Specific usage of Redis five data types

We all know that Redis supports 5 data types:

  • string character string (can be integer, floating point and character string, collectively referred to as elements)
  • hash (key of hash must be unique)
  • list list (implement the queue, the element is not unique, the principle of first in, first out)
  • set collection (different elements)
  • zset (sorted set) ordered set

In my actual project, I only used the String type. Since the string type is binary safe, it means that it can contain any data (serialized objects and jpg images). Do you think the String type is sufficient?

Back to the topic, next I will implement the storage of each data type.

String

Overview

  • String is the most basic type of redis. You can understand it as exactly the same type as Memcached. A key corresponds to a value.
  • The string type is binary safe. This means that the redis string can contain any data. Such as jpg images or serialized objects.
  • The string type is the most basic data type of Redis, and the value of the string type can store up to 512MB.

example:

jedis.set("test", "123");// 保存key为test,value为123的数据
jedis.get("test"); // 查看key为test的数据

Hash

Overview

  • Redis hash is a collection of key-value (key=>value) pairs.
  • Redis hash is a mapping table between field and value of string type, and hash is particularly suitable for storing objects.

example:

jedis.hset("test1","map1","value1");// 保存一个redis的key为test1,map的key为map1,value为value1的数据
jedis.hget("test1","map1"); // 获取redis下key为test1中map的key为map1的值

List

Overview

  • The 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.

example

jedis.lpush("test2","1");
jedis.lpush("test2","2");
String pop1 = jedis.lpop("test2");// 输出"2"
String pop2 = jedis.lpop("test2");// 输出"1"  // 说明是先进后出
jedis.lpush("test2","1");
jedis.lpush("test2","2");
List<String> tests2 = jedis.lrange("test2", 0, -1);// ["2","1"]

set

Overview

  • Redis's Set is an unordered collection of string type.
  • The collection is implemented through a hash table, so the complexity of adding, deleting, and searching is O(1).

sadd command

Add a string element to the set set corresponding to the key, and return 1 if it succeeds, and return 0 if the element is already in the set.

example

        // set类型
        jedis.sadd("test3","1","4","2");
        Set<String> test3 = jedis.smembers("test3");// 查看集合列表
        Long test31 = jedis.scard("test3");// 查看集合有多少条记录

zset(sorted set)

Overview

  • Redis zset, like set, is also a collection of string type elements, and duplicate members are not allowed.
  • The difference is that each element is associated with a double type score. Redis uses scores to sort the members of the set from small to large.
  • The members of zset are unique, but the score (score) can be repeated.

example

        // 保存一个key为test4的值,按score排序,默认从小到大排序        
        jedis.zadd("test4",1.0,"你好");
        jedis.zadd("test4",3.0,"你好3");
        jedis.zadd("test4",2.0,"你好2");
        Set<String> test4 = jedis.zrange("test4", 0, -1);

 

 

 

Guess you like

Origin blog.csdn.net/qq_43037478/article/details/112576002