Redis data structure analysis

Redis has five basic data structures, namely string (string), list (list), set (collection), hash (hash), and zset (ordered set). All Redis data structures use a unique key string as a name, and then use this key to obtain the corresponding value data.

A brief description of each type follows

One, string (string)

The value corresponding to the key is a string type, as shown in the following figure:

grammar:

1. Single key-value pair setting

2. Batch key-value pair settings

Two, list (list)

The list is equivalent to the LinkedList in java, which is a linked list rather than an array . When the last element of the list is popped (pop), the data structure is automatically deleted and the memory is reclaimed

 Push into the stack: rpush

Pop: lpop, rpop

Keep the value of a certain interval: ltrim(start_index, end_index)

Get elements by index: lindex

List all values ​​in a range: lrange(start_index, end_index)

 

 

Three, hash (hash)

Equivalent to HashMap in java, it is an unordered dictionary and a two-dimensional structure of array + linked list

The value of the hash dictionary in redis can only be a string , and some syntax commands are as follows:

 

 It can also be counted in the hash structure, and the corresponding instruction is hincrby

 

Four, set (collection)

The set collection is equivalent to the HashSet in java, and the internal key-value pairs are unordered and unique. Some instructions are as follows:

Five, zset (ordered collection)

Zset is equivalent to the combination of SortedSet and HashMap in java. On the one hand, it has the uniqueness of the internal value; on the other hand, it can also assign a score to each value, which represents the sorting weight of this value.

zset should support random insertion and deletion

 

 

Summarize

The four data structures of list/set/hash/zset are container-type data structures, sharing two general rules:

1、create if not exits

If the container does not exist, create one and then operate

2、drop if no elements

If there are no more elements in the container, the container will be deleted and the memory will be released

3. Expiration time

All data structures of Redis can set an expiration time, and when the time is up, Redis will automatically delete the corresponding object.

Note: If a string has an expiration time set, and then you call the set method to modify it, its expiration time will disappear

Guess you like

Origin blog.csdn.net/dazhanglao/article/details/125843526