Redis02: common data types

brief introduction


As a key/value data storage system, Redis provides a variety of data types to facilitate data management. Then, the data generated in our project is stored based on the specified type, such as user login information, shopping cart information, commodity details, etc.

Common data types

The basic data structures in IDS include strings, hashes, lists, collections, and ordered collections. The specific type used in the work should be combined with the specific scene.

String type operation practice

String type is the simplest data type in redis. Its stored value can be string, and its maximum string length can be up to 512M. Based on this type, you can realize the word count of a blog, continuously append the log to the specified key, realize a distributed self increasing iid, and realize the like operation of a blog


When the stored string is an integer, redis provides a practical command INCR to increment the current key value and return the incremented value.

INCR key> set num 1
(integer) 1> incr num
(integer) 2> keys *
1) "num"> incr num>

If the object does not exist, it is created automatically, and if it exists, it is automatically + 1

Specify growth factor

Syntax: INCRBY key increment> incrby num 2
(integer) 5> incrby num 2
(integer) 7> incrby num 2
(integer) 9>


Decrements the specified integer

DECR key is decremented according to the default step size (1 by default)
Decrby key increment decrements according to the specified step size> incr num
(integer) 10> decr num
(integer) 9> decrby num 3


Append a value to the tail. If it does not exist, create the value. Its value is the written value, that is, it is equivalent to set key value. The return value is the length of the appended string

Syntax APPEND key value> keys *
1) "num"
2) "test1"
3) "test"> get test
"123"> append test "abc"
(integer) 6> get test


String length, returns the length of the data. If the key does not exist, it returns 0. Note that if the key value is an empty string, it also returns 0.

Syntax: STRLEN key> get test
"123abc"> strlen test
(integer) 6> strlen tnt
(integer) 0> set tnt ""
OK> strlen tnt
(integer) 0> exists tnt
(integer) 1>


Set / obtain multiple key values at the same time

Syntax: MSET key value [key value]

MGET key [key]> flushall
OK> keys *
(empty list or set)> mset a 1 b 2 c 3
OK> mget a b c
1) "1"
2) "2"
3) "3">


Blog word count: strlen

Append data to the specified key: append

Implement a distributed self increasing id: incr

Implement praise: incr, decr

Application practice of Hash type

Redis hash type is equivalent to HashMap in Java. The implementation principle is consistent with HashMap. It is generally used to store object information and store the mapping of a field and field value of key value. A hash type can contain up to 232-1 fields.

HSET and HGET assignment and value  

hset key field value 

hget key field> hset user username chenchen
(integer) 1> hget user username
"chenchen"> hset user username chen
(integer) 0> keys user
1) "user"> hgetall user
1) "username"
2) "chen">> hset user age 18
(integer) 1> hset user address "xi'an"
(integer) 1> hgetall user
1) "username"
2) "chen"
3) "age"
4) "18"
3) "address"
4) "xi'an">

The HSET command does not distinguish between insert and update operations. The HSET command returns 1 when performing insert operations and 0 when performing update operations.


Set the auto increment method of auto increment steps hincrby field key value> hdecrby article total 1		#Error during execution> hincrby article total -1		#There is no hdecrby subtraction command
(integer) 1> hget article total			#Get value


HMSET and HMGET set and get the object attribute hmset key   field value field value> hmset person username tony age 18
OK> hmget person age username
1) "18"
2) "tony"> hgetall person
1) "username"
2) "tony"
3) "age"
4) "18">

The order of the above HMGET fields can be defined, and the order of username and age can be changed


Whether the hexists key exists for the attribute   field> hexists killer
(error) ERR wrong number of arguments for 'hexists' command> hexists killer a
(integer) 0> hexists user username
(integer) 1> hexists person age
(integer) 1>


Delete attribute hdel key   field> hdel user age
(integer) 1> hgetall user
1) "username"
2) "chen"> hgetall person
1) "username"
2) "tony"
3) "age"
4) "18">


Only get the field name HKEYS or field value HVALS

hkeys key

hvals key> hkeys person
1) "username"
2) "age"> hvals person
1) "tony"
2) "18"
2.3.8	hlen
 Number of elements> hlen user
(integer) 1> hlen person
(integer) 2>


Do you need to write memory to publish an article? (required, hmset)

How to browse blogs? (hmget)

Determine whether the blog exists? (hexists)

Delete blog? (hdel)

How does distributed login store user information? (hmset)

List type application practice

Redis's list type is equivalent to LinkedList in java, and its principle is a two-way linked list. It supports forward, reverse lookup, traversal and other operations, and the insertion and deletion speed is relatively fast. It is often used to realize the design of hot sales list, latest comments, etc.


Get the data from scratch and show it

lrange key start end

lrange returns the elements of the specified interval in the list. The interval is specified by offsets start and end, where 0 represents the first element, 1 represents the second element, - 1 represents the last element, - 2 represents the penultimate element, and so on


Add a string element in the header of the list corresponding to the key

redis> lpush mylist "world"
(integer) 1
redis> lpush mylist "hello"
(integer) 2
redis> lrange mylist 0 -1
1) "hello"
2) "world"


Add a string element at the end of the list corresponding to the key

redis> rpush mylist2 "hello"
(integer) 1
redis> rpush mylist2 "world"
(integer) 2
redis> lrange mylist2 0 -1
1) "hello"
2) "world"


Delete the entire list data

del key


Add a character element before or after the specific position of the list corresponding to the key

redis> rpush mylist3 "hello"
(integer) 1
redis> rpush mylist3 "world"
(integer) 2
redis> linsert mylist3 before "world" "there"
(integer) 3
redis> lrange mylist3 0 -1
1) "hello"
2) "there"
3) "world"


Set the element value of the specified subscript in the list (generally used for modification) list key value

redis> rpush mylist4 "one"
(integer) 1
redis> rpush mylist4 "two"
(integer) 2
redis> rpush mylist4 "three"
(integer) 3
redis> lset mylist4 0 "four"
redis> lset mylist4 -2 "five"
redis> lrange mylist4 0 -1
1) "four"
2) "five"
3) "three"


Delete the elements with the same number of count and value from the key, lrem key count value  

When count > 0, it is deleted from beginning to end

redis> rpush mylist5 "hello"
(integer) 1
redis> rpush mylist5 "hello"
(integer) 2
redis> rpush mylist5 "foo"
(integer) 3
redis> rpush mylist5 "hello"
(integer) 4
redis> lrem mylist5 2 "hello"
(integer) 2
redis> lrange mylist5 0 -1
1) "foo"
2) "hello"

  When count < 0, delete from end to end

redis> rpush mylist6 "hello"
(integer) 1
redis> rpush mylist6 "hello"
(integer) 2
redis> rpush mylist6 "foo"
(integer) 3
redis> rpush mylist6 "hello"
(integer) 4
redis> lrem mylist6 -2 "hello"
(integer) 2
redis> lrange mylist6 0 -1
1) "hello"
2) "foo"

When count=0, delete all

redis> rpush mylist7 "hello"
(integer) 1
redis> rpush mylist7 "hello"
(integer) 2
redis> rpush mylist7 "foo"
(integer) 3
redis> rpush mylist7 "hello"
(integer) 4
redis> lrem mylist7 0 "hello"
(integer) 3
redis> lrange mylist7 0 -1
1) "foo"


Retain data within the specified key range itrim key start end

1 represents the second element, - 1 represents the last element

redis> rpush mylist8 "one"
(integer) 1
redis> rpush mylist8 "two"
(integer) 2
redis> rpush mylist8 "three"
(integer) 3
redis> rpush mylist8 "four"
(integer) 4
redis> ltrim mylist8 1 -1
redis> lrange mylist8 0 -1
1) "two"
2) "three"
3) "four"


Delete the data from the header of the list and return the deleted data


Delete data from the tail and return the deleted data


Delete data from the header and return nil after the set time


Delete data from tail


Returns the list length corresponding to the key


Returns the element index key index at the index position in the list with the name key


Transfer elements from the tail of the first list and add them to the head of the second list. Finally, return the removed element value. This operation is atomic. If the first list is empty or does not exist, return nil:

rpoplpush lst1 lst1

rpoplpush lst1 lst2


How to implement a queue structure based on redis? (head in and tail out, lpush/rpop)

How to implement a stack result based on redis? (head in and head out, lpush/lpop)

How to implement a blocking queue based on redis? (lpush/brpop)

How to realize the fairness of second kill? (first in first out lpush/rpop)

Sequential message queue (ditto)

How can users register to send mail improve efficiency? (optimize its efficiency through queues)

Dynamically update the product list (top sellers, linsert)

Merchant fan list (list structure)

Set type application practice

The members are unique. The set set in Redis is implemented by using hash table. Therefore, the complexity of searching, adding and searching is O(1)


Failed to add element repeatedly. The added quantity is returned> sadd name tony
(integer) 1> sadd name hellen
(integer) 1> sadd name rose
(integer) 1> sadd name rose
(integer) 0


Get content smembers key


Remove and return a random element in the collection. Note: the removal is random> smembers internet
1) "amoeba"
2) "redis"
3) "rabbitmq"
4) "nginx"> spop internet
"rabbitmq"> spop internet
"nginx"> smembers internet
1) "amoeba"
2) "redis"


Get the number of members scar name


Move elements to another collection> sadd internet amoeba nginx redis
(integer) 3> sadd bigdata hadopp spark rabbitmq
(integer) 3> smembers internet
1) "amoeba"
2) "redis"
3) "nginx"> smembers bigdata
1) "hadopp"
2) "spark"
3) "rabbitmq"> smove bigdata internet rabbitmq
(integer) 1> smembers internet
1) "amoeba"
2) "redis"
3) "rabbitmq"
4) "nginx"> smembers bigdata
1) "hadopp"
2) "spark"


Merge set sunion key1 key2> sunion internet bigdata
1) "redis"
2) "nginx"
3) "rabbitmq"
4) "amoeba"
5) "hadopp"
6) "spark"


Like function (you can only click once, do not repeat sadd, add like, srem cancel like, smembers like object, scard like number)

Achieve website voting (ditto)

Focus on customs clearance (ibid.)

Tags: Java Database Redis

Posted on Fri, 08 Oct 2021 06:05:49 -0400 by abitlikehomer