Redis data structure (detailed explanation)

With the foundation of NoSQL in the previous article, we all know that Redis is a typical NoSQL, so let’s briefly introduce Redis first:

What is Redis?

Redis (Remote Dictionary Server) is an open source high-performance key-value storage system written in ANSI C language. It stores data in the form of key-value pairs and provides fast and stable data reading and writing operations.

The following are some important features and structures of Redis:

  1. Key-value storage: Redis uses key-value pairs to store data. Each key is unique and can use a variety of different data types as values.
  2. Data type: Redis supports multiple data types, including string, hash, list, set, sorted set, etc. These data types provide flexible data manipulation methods.
  3. Memory storage: Redis data is usually stored in memory, which allows it to provide very fast data reading and writing speeds. At the same time, Redis also supports persisting data to the hard disk so that the data can be restored after restarting.
  4. Persistence method: Redis provides two persistence methods, namely RDB (Redis Database) snapshot and AOF (Append-Only File) log. RDB can store data set snapshots on the hard disk within a specified time interval, while AOF records the log of each write operation and recovers the data by replaying the log.
  5. High performance: Redis uses a variety of optimization techniques to provide high-performance data reading and writing operations. These include using a single-threaded model, memory-based operations, asynchronous IO, etc.

In general, Redis is a high-performance, flexible, and reliable key-value storage system suitable for various scenarios, such as caching, message queues, counters, real-time rankings, etc. It is not just a simple caching tool, but also provides rich data structures and powerful functions, allowing developers to build complex applications more conveniently.

What are the keys and values ​​of Redis?

In Redis, key and value respectively refer to the two parts of the data pair stored in the Redis database.

What is the key? A key is an identifier used to uniquely identify a data item. It is a string used to store data in Redis and retrieve it when needed. Keys are unique, data items with the same key will be overwritten.

What is the value? The value is the actual data stored in the Redis database associated with the key. Values ​​can be of different data types such as strings, hashes, lists, sets, sorted sets, etc. Depending on the data type, values ​​can have different operations and structures.

To summarize: from the above description, we know that the key of Redis is usually only the String type, and what we usually call the data type of Redis actually refers to the value of a key-value pair in Redis. The data type of the value is various. Diverse. This is the most important point we need to understand before learning the Redis data structure.

What are the five commonly used data structures in Redis?

1.String type (string)

The String type, also known as the string type, is the simplest storage type in Redis. Its value is a string, but depending on the format of the string, it can be divided into three categories:

  • string: ordinary string

  • int: Integer type, can perform auto-increment and auto-decrement operations

  • float: floating point type, can perform auto-increment and auto-decrement operations

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

Common commands of String type

key hierarchical structure

Redis key allows multiple words to form a hierarchical structure. Multiple words are separated by ':'. The format is as follows:

After we use: to separate them, a hierarchical structure will naturally form:

If Value is a Java object, such as a User object, the object can be serialized into a JSON string (essentially still a string type) and stored:

2. Hash type

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

Why is there this structure?

Because the String structure stores json after serializing the object into a JSON string, it is very inconvenient when a certain field of the object needs to be modified. The Hash structure can store each field in the object independently, and CRUD can be done for a single field. :

String stores json as follows:

The Hash structure is stored as follows:

And there can be multiple fields in the value. The advantage is that it is more flexible and can operate on a specific field in the value, which is not possible with String.

Common commands in Hash type

3.list list

The list type in Redis is similar to the LinkedList in Java and can be regarded as a doubly linked list structure. That is, it can support forward retrieval and reverse retrieval.

Structure: user (key) 1 2 3 (all values)

There are multiple values; they are ordered and indexed.

The characteristics are also similar to LinkedList

  • orderly

  • Elements can be repeated

  • Inserting and deleting same blocks

  • Query speed is average

Commonly used commands:

4.set

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

  • disorder

  • Elements cannot be repeated

  • Find fast

  • Supports intersection, union, difference and other functions

The stored elements are unordered, that is, they are not in the order of addition, and they are not repeated.

Operations on a single collection:

sadd key element 1, element 2,... add element

srem key element delete element

smembers key View all elements in the specified key

scard key to view the number of elements

sismember key member: Determine whether an element exists in the set

smembers: Get all elements in the set

Operations between multiple collections:

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

Set 5

(sorted set: ordered set)

The stored elements are set collections that can be sorted (you can specify the sorting rules yourself), and duplication is not allowed.

Redis's SortedSet is a sortable set collection, which is somewhat similar to the TreeSet in Java, but the underlying data structure is very different. Each element in 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 properties:

  • Sortable

  • Elements are not repeated

  • Query speed is fast

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

Note: member refers to the added element.

Common operations:

zadd key score member: Add one or more elements to the sorted set. At the same time, we need to add a score. This score is added by ourselves. If it already exists, update its score value.

zrem key member: Delete a specified element in the sorted set.

zscore key member: Get the score value of the element member specified by value in key.

zrank key member: Get the ranking of the specified element member in the key.

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: Let the specified element in the sorted set increase by 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 differences, intersections, and unions.

Note: The rankings in the above operations are in ascending order by default. If you want to order in descending order, just add REV after the first z letter of the command.

Summarize

Redis is a key-value database. The key is generally of String type, but the value types are diverse. The following types generally refer to the value type. The keys are mostly String, and there may only be some hierarchical structure at most; Values ​​are diverse. For example, if value is of set type, then what is stored is multiple set elements, which is equivalent to value being a set collection. For example, if value type is hash type, it is equivalent to storing key-value pairs in value. , and there can be multiple pairs.

The above are the commonly used data structures in Redis. It should be noted that the commands mentioned above need to be entered at the control end. Of course, we can also use some visualization tools to operate data.

Guess you like

Origin blog.csdn.net/weixin_52394141/article/details/132262165