Redis02: common data types

brief introduction summary ...
summary
Common data types
incr/incrby
decr/decrby
append
strlen
Summary
hincrby
hmset/hmget
hexists
hdel
hkeys/hvals
Summary
ltrim
lpop
Summary
Summary
brief introduction

summary

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

incr/incrby

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

127.0.0.1:6379> set num 1 (integer) 1 127.0.0.1:6379> incr num (integer) 2 127.0.0.1:6379> keys * 1) "num" 127.0.0.1:6379> incr num 127.0.0.1:6379>

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

127.0.0.1:6379> incrby num 2 (integer) 5 127.0.0.1:6379> incrby num 2 (integer) 7 127.0.0.1:6379> incrby num 2 (integer) 9 127.0.0.1:6379>

decr/decrby

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

127.0.0.1:6379> incr num (integer) 10 127.0.0.1:6379> decr num (integer) 9 127.0.0.1:6379> decrby num 3

append

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

127.0.0.1:6379> keys * 1) "num" 2) "test1" 3) "test" 127.0.0.1:6379> get test "123" 127.0.0.1:6379> append test "abc" (integer) 6 127.0.0.1:6379> get test "123abc" 127.0.0.1:6379>

strlen

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

127.0.0.1:6379> get test "123abc" 127.0.0.1:6379> strlen test (integer) 6 127.0.0.1:6379> strlen tnt (integer) 0 127.0.0.1:6379> set tnt "" OK 127.0.0.1:6379> strlen tnt (integer) 0 127.0.0.1:6379> exists tnt (integer) 1 127.0.0.1:6379>

mset/mget

Set / obtain multiple key values at the same time

Syntax: MSET key value [key value]

MGET key [key]

127.0.0.1:6379> flushall OK 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> mset a 1 b 2 c 3 OK 127.0.0.1:6379> mget a b c 1) "1" 2) "2" 3) "3" 127.0.0.1:6379>

Summary

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

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

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.

hincrby

Set the auto increment method of auto increment steps hincrby field key value  

127.0.0.1:6379> hdecrby article total 1 #Error during execution 127.0.0.1:6379> hincrby article total -1 #There is no hdecrby subtraction command (integer) 1 127.0.0.1:6379> hget article total #Get value

hmset/hmget

HMSET and HMGET set and get the object attribute hmset key   field value field value

127.0.0.1:6379> hmset person username tony age 18 OK 127.0.0.1:6379> hmget person age username 1) "18" 2) "tony" 127.0.0.1:6379> hgetall person 1) "username" 2) "tony" 3) "age" 4) "18" 127.0.0.1:6379>

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

hexists

Whether the hexists key exists for the attribute   field

127.0.0.1:6379> hexists killer (error) ERR wrong number of arguments for 'hexists' command 127.0.0.1:6379> hexists killer a (integer) 0 127.0.0.1:6379> hexists user username (integer) 1 127.0.0.1:6379> hexists person age (integer) 1 127.0.0.1:6379>

hdel

Delete attribute hdel key   field

127.0.0.1:6379> hdel user age (integer) 1 127.0.0.1:6379> hgetall user 1) "username" 2) "chen" 127.0.0.1:6379> hgetall person 1) "username" 2) "tony" 3) "age" 4) "18" 127.0.0.1:6379>

hkeys/hvals

Only get the field name HKEYS or field value HVALS

hkeys key

hvals key

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

Summary

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.

Lrange

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

lpush

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

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

rpush

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

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

del

Delete the entire list data

del key

linsert

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

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

lset

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

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

lrem

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 127.0.0.1:6379> rpush mylist5 "hello" (integer) 1 redis 127.0.0.1:6379> rpush mylist5 "hello" (integer) 2 redis 127.0.0.1:6379> rpush mylist5 "foo" (integer) 3 redis 127.0.0.1:6379> rpush mylist5 "hello" (integer) 4 redis 127.0.0.1:6379> lrem mylist5 2 "hello" (integer) 2 redis 127.0.0.1:6379> lrange mylist5 0 -1 1) "foo" 2) "hello"

  When count < 0, delete from end to end

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

When count=0, delete all

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

ltrim

Retain data within the specified key range itrim key start end

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

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

lpop

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

rpop

Delete data from the tail and return the deleted data

blpop

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

brpop

Delete data from tail

llen

Returns the list length corresponding to the key

lindex

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

rpoplpush

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

Summary

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)

sadd

Failed to add element repeatedly. The added quantity is returned

127.0.0.1:6379> sadd name tony (integer) 1 127.0.0.1:6379> sadd name hellen (integer) 1 127.0.0.1:6379> sadd name rose (integer) 1 127.0.0.1:6379> sadd name rose (integer) 0

smembers

Get content smembers key

spop

Remove and return a random element in the collection. Note: the removal is random

127.0.0.1:6379> smembers internet 1) "amoeba" 2) "redis" 3) "rabbitmq" 4) "nginx" 127.0.0.1:6379> spop internet "rabbitmq" 127.0.0.1:6379> spop internet "nginx" 127.0.0.1:6379> smembers internet 1) "amoeba" 2) "redis"

scard

Get the number of members scar name

smove

Move elements to another collection

127.0.0.1:6379> sadd internet amoeba nginx redis (integer) 3 127.0.0.1:6379> sadd bigdata hadopp spark rabbitmq (integer) 3 127.0.0.1:6379> smembers internet 1) "amoeba" 2) "redis" 3) "nginx" 127.0.0.1:6379> smembers bigdata 1) "hadopp" 2) "spark" 3) "rabbitmq" 127.0.0.1:6379> smove bigdata internet rabbitmq (integer) 1 127.0.0.1:6379> smembers internet 1) "amoeba" 2) "redis" 3) "rabbitmq" 4) "nginx" 127.0.0.1:6379> smembers bigdata 1) "hadopp" 2) "spark"

sunion

Merge set sunion key1 key2

127.0.0.1:6379> sunion internet bigdata 1) "redis" 2) "nginx" 3) "rabbitmq" 4) "amoeba" 5) "hadopp" 6) "spark"

Summary

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.)

8 October 2021, 06:05 | Views: 1477

Add new comment

For adding a comment, please log in
or create account

0 comments