Getting started with Redis - 5 basic data types

The address of the original text is updated, and the reading effect is better!

Getting Started with Redis - 5 Basic Datatypes | CoderMast Programming Mast https://www.codermast.com/database/redis/five-base-datatype.html

illustrate

In our usual business, we basically only use the basic data types of Redis (String, List, Hash, Set, Sorted Set), and the special types (Geo, Bitmap, Hyperloglog) are only used in special business scenarios. Usually, we only need to master the basic data types, and the special types can be understood.

# String string

String type, that is, string type, is the simplest storage type in Redis. The String type is binary safe. It means that the String of redis can contain any data. Such as jpg images or serialized objects.

Its value is a string, but according to the format of the string, it can be divided into three categories:

  • string: ordinary string
  • int: Integer type, can do self-increment and self-decrement operations
  • float: Floating point type, can do self-increment and self-decrement operations

Regardless of the format, the bottom layer is stored in the form of a byte array, but the encoding method is different. The maximum space of string type cannot exceed 512m.

Common operation commands of String type :

Order describe
SET Add or modify an existing key-value pair of String type
GET Get the value of String type according to the key
MSET Add multiple key-value pairs of String type in batches
MGET Get multiple values ​​of String type according to multiple keys
INCR Increment an integer key by 1
INCRBY Let an integer key auto-increment and specify the step size, for example: incrby num 2 let the num value auto-increment by 2
INCRBYFLOAT Increment a floating-point number with a specified step size
SETNX Add a key-value pair of String type, provided that the key does not exist, otherwise it will not execute
SEVEN Add a key-value pair of String type and specify the validity period

Although there is no directory structure in the key of Redis, multiple words are allowed to form a hierarchical structure, and multiple words are separated by ":". Generally, the format used is: 项目名:业务名:类型:id.

This format is not fixed, you can also delete or add entries according to your own needs.

for example

For example, our project name is myblog, and there are two different types of data, user and product. We can define the key like this:

User related key: myblog:user:1

Product related key: myblog:product:1

If Value is a Java object, such as a User object, you can serialize the object into a JSON string and store it

KEY VALUE
myblog:user:1 {“id”:1, “name”: “Jack”, “age”: 21}
myblog:product:1 {"id":1, "name": "Xiaomi 11", "price": 4999}

# Hash hash

Hash type, also called hash, can also become hash type. Its value is an unordered dictionary, similar to the HashMap structure in Java.

The Hash structure can store each field in the object independently, and can do CRUD for a single field

Hash type data

Common commands for Hash are:

Order describe
HSET key field value Add or modify the value of the field of the hash type key
HGET key field Get the field value of a hash type key
HMSET hmset and hset have the same effect, hmset can be discarded after 4.0
HMGET Get the field values ​​of multiple hash type keys in batches
HGETALL Get all fields and values ​​in a hash type key
HKEYS Get all the fields in a hash type key
WHALES Get all the values ​​in a hash type key
HINCRBY Let the field value of a hash type key auto-increment and specify the step size
HSETNX Add a field value of a hash type key, provided that the field does not exist, otherwise it will not be executed

# List list

The List type in Redis is similar to the LinkedList in Java, which can be regarded as a doubly linked list structure. Both forward search and reverse search can be supported.

Traits are also similar to LinkedList:

  • orderly
  • elements can be repeated
  • Fast insertion and deletion
  • General query speed

It is often used to store an ordered data, for example: friend circle like list, comment list, etc.

Order describe
LPUSH key element … Insert one or more elements to the left of the list
LPOP key Remove and return the first element on the left of the list, or nil if there is no
RPUSH key element … Insert one or more elements to the right of the list
RPOP key Remove and return the first element on the right side of the list
LRANGE key star end Returns all elements within a subscript range
BLPOP and BRPOP Similar to LPOP and RPOP, except that it waits for the specified time when there are no elements, instead of returning nil directly

Schematic diagram of a double-ended queue

# Set collection

The Set structure of Redis is similar to the HashSet in Java, which can be regarded as a HashMap whose value is null. Because it is also a hash table, it has similar characteristics to HashSet

  • out of order
  • Elements cannot be repeated
  • find fast
  • Support intersection, union, difference and other functions
Order describe
SADD key member … Add one or more elements to the set
SREM key member Remove the specified element in the set
SCARD key Returns the number of elements in the set
SISMEMBER key member Determine whether an element exists in the set
SMEMBERS Get all elements in the set
SINTER key1 key2 … Find the intersection of key1 and key2
SDIFF key1 key2 … Find the difference between key1 and key2
SUNION key1 key2 … Find the union of key1 and key2

tip Intersection, difference, and union diagrams

# SortedSet ordered collection

Redis's SortedSet can also be called Zset, which is a sortable set collection, which is somewhat similar to TreeSet in Java, but the underlying data structure is very different.

Each element in the SortedSet has a score attribute, and the elements can be sorted based on the score attribute. The underlying implementation is a skip list (SkipList) plus a hash table.

SortedSet has the following characteristics:

  • sortable
  • elements are not repeated
  • query speed

Because of the sortable nature of SortedSet, it is often used to implement functions such as leaderboards.

Common commands for SortedSet are

Order describe
ZADD key score member Add one or more elements to the sorted set, and update its score value if it already exists
ZREM key member Delete a specified element in the sorted set
ZSCORE key member Get the score value of the specified element in the sorted set
ZRANK key member Get the rank of the specified element in the sorted set
ZCARD key Get the number of elements in the sorted set
ZCOUNT key min max Count the number of all elements whose score value is within a given range
ZINCRBY key increment member Make the specified elements in the sorted set self-increment, and the step size is the specified increment value
ZRANGE key min max After sorting by score, get the elements within the specified ranking range
ZRANGEBYSCORE key min max After sorting by score, get the elements within the specified score range
ZDIFF、ZINTER、ZUNION Find difference, intersection, union

Notice

All rankings are in ascending order by default. If you want to order in descending order, you REVcan add it after Z in the command.

Guess you like

Origin blog.csdn.net/qq_33685334/article/details/131248601