Redis Series Textbook (III) – common commands

Time:2022-6-1

Step 1:5 data types
Step 2: string string
Step 3: list list
Step 4: hash dictionary, hash table
Step 5: set set
Step 6: sorted set
Step 7: official command manual

Step 1: 5 data types

Redis currently has five data types:

String (string)

List

Hash (Dictionary)

Set

Sorted set

Different data types have different command modes. Next, some common commands will be demonstrated according to different data types:

Step 2: string string

As shown in the figure, type in the following commands one by one, and you will naturally know what the use is….

set google http:`//www.google`

append google .com

get google

set visitors 0

incr visitors

incr visitors

get visitors

incrby visitors 100

get visitors

type google

type visitors

ttl google

rename google google-site

get google

get google-site

For those who do not understand individual commands, you can also refer to the following manuals to understand them separately

Set key valueset key=value

Get key get the value corresponding to the key

Getrange key start end the substring of the obtained string is stored in a key

GetSet key value sets the string value of the key and returns the old value

Getbit key offset returns the offset of the string value stored in the key value

MGET key1 [key2..] Get the values of all the given keys

Setbit key offset value sets or clears the offset of this bit from the string value stored in the key

Set key seconds value

Setnx key value sets the value of the key. Only when the key does not exist

Setrange key offset value overrides the offset of a portion of the string from the specified key

Strlen key gets the length of the value stored in the key

MSET key value [key value...] Set multiple keys and values

MSETNX key value [key value...] Set multiple keys and values, only when there is no key

Psetex key milliseconds value sets the millisecond value and expiration time of the key

Incr key increases the integer value of the key once

Incrby key increment the integer value of the key incremented by the given number

Incrbyfloat key increment the floating-point value of the key incremented by the given number

Decr key decrements the integer value of the key once

Decrby key increment decrements the integer value of a key by a given number

Append key value append value to a key

Del key if delete key exists

Dump key returns the serialized version of the value stored in the specified key

Exists key this command checks whether the key exists

Expire key seconds specifies the expiration time of the key

Expire key timestamp the specified key expiration time. Here, time is in UNIX timestamp format

Pexpire key milliseconds setting key expires in milliseconds

Pexpireat key milliseconds timestamp setting key expires in milliseconds when UNIX timestamp is specified

Keys pattern finds all keys that match the specified pattern

Move key DB move key to another database

Persist key remove expired keys

Pttl key gets the expiration key of the remaining time in milliseconds.

TTL key gets the remaining time for the key to expire.

Randomkey returns random keys from redis

Rename key newkey change the name of the key

Rename key newkey rename key if the new key does not exist

Type key returns the value of the data type stored in the key.

Redis Series Textbook (III) - common commands

Step 3: list list

As shown in the figure, type in the following commands one by one, and you will naturally know what the use is….

lpush list1 redis

lpush list1 hello

rpush list1 world

llen list1

lrange list1 0 3

lpop list1

rpop list1

lrange list1 0 3

For those who do not understand individual commands, you can also refer to the following manuals to understand them separately

Blpop key1 [key2] timeout fetch and get the first element in the list, or block until available

Brpop key1 [key2] timeout fetch and get the last element in the list, or block until available

Brpoplpush source destination timeout pops up a value from the list, pushes it to another list and returns it; Or block until available

Lindex key index get the corresponding element from a list index

Insert key before|after pivot value inserts an element after or before other elements in the list

Llen key get the length of the list

Lpop key gets and retrieves the first element in the list

Lpush key value1 [value2] preceded by a list of one or more values

Lpushx key value is preceded by a list of values, only if the list exists

Lrange key start stop get various elements from a list

Lrem key count value deletes an element from the list

Lset key index value sets the value of an element in the index in the list

Ltrim key start stop trims the list to the specified range

Rpop key retrieves and retrieves the last element in the list

Rpoplpush source destination deletes the list of the last element, appends it to another list, and returns it

Rpush key value1 [value2] add one or more values to the list

Rpushx key value adds a value list only if it exists in the list

Redis Series Textbook (III) - common commands

Step 4: hash dictionary, hash table

As shown in the figure, type in the following commands one by one, and you will naturally know what the use is….

hset person name jack

hset person age 20

hset person sex female

hgetall person

hkeys person

hvals person

For those who do not understand individual commands, you can also refer to the following manuals to understand them separately

HDEL key field[field...] Delete one or more attribute fields of the object. Nonexistent attributes will be ignored

Hexists key field to check whether the attribute field exists in the object

Hget key field get the value of the field attribute field in the object

Hgetall key gets all attribute fields and values of an object

Hincrby key field value increases the value of the specified field in the object by the given value. The atomic auto increment operation can only be used for the attribute value of integer

Hincrbyflow key field increment increases the value of the specified field in the object by a given floating-point number

Hkeys key get all attribute fields of the object

Hvals key gets all attribute values of an object

HLEN key gets the total number of all attribute fields of an object

HMGET key field[field...] Gets the value of one or more specified fields of an object

Hset key field value sets the value of the specified field of the object

HMSET key field value [field value ...] Set the value of one or more fields in the object at the same time

Hsetnx key field value sets the field value only when the specified field does not exist in the object

Hstrlen key field returns the string length of the value of the specified field of the object. If the object or field does not exist, the`0`.

Hscan key cursor [match pattern] [count] similar to scan command

Redis Series Textbook (III) - common commands

Step 5: set set

As shown in the figure, type in the following commands one by one, and you will naturally know what the use is….

SADD myset "Hello"

SADD myset "World"

SMEMBERS myset

SADD myset "one"

SISMEMBER myset "one"

SISMEMBER myset "two"

For those who do not understand individual commands, you can also refer to the following manuals to understand them separately

SADD key member [member ...] Add one or more elements to a set

Scar key gets the number of elements in the collection

SDIFF key [key ...] Get elements that do not exist in the queue

SDIFFSTORE destination key [key ...] Get the elements that do not exist in the queue and store them in a key result set

SINTER key [key ...] Get the intersection of two sets

SINTERSTORE destination key [key ...] Get the intersection of two sets and store it in one set

Sismember key member determines that a given value is a member of a set

Members key get all keys in the set

Smove source destination member moves a key in a collection to another collection

Spop key [count] get and delete elements in a collection

Srandmember key [count] randomly obtain an element from the collection

SREM key member [member ...] Delete one or more elements from the collection. Nonexistent elements are ignored

SUNION key [key ...] Add multiple set elements

SUNIONSTORE destination key [key ...] Merge the set elements and store the results in the new set

Sscan key cursor [match pattern] [count] iterates over the elements in the set

Redis Series Textbook (III) - common commands

Step 6: sorted set

As shown in the figure, type in the following commands one by one, and you will naturally know what the use is….

zadd dbs 100 redis

zadd dbs 98 memcached

zadd dbs 99 mongodb

zadd dbs 99 leveldb

zcard dbs

zcount dbs 10 99

zrank dbs leveldb

zrank dbs other

zrangebyscore dbs 98 100

For those who do not understand individual commands, you can also refer to the following manuals to understand them separately

Zadd key Score1 member1 [score2 member2] add one or more members to an ordered set, or update its score if it already exists

Number of ordered set members obtained by zcard key

Zcount key min max calculates the score between an ordered set member and a given value range

Zincrby key increment member increases a member's score in an ordered set

ZINTERSTORE destination numkeys key [key ...] Multiple cross sort sets and store to generate a new key ordered set.

Zlexcount key min max calculates the number of ordered set members between a given dictionary range

Zrange key start stop [withscores] returns an ordered set of member ranges (from low to high) from the index

Zrangebylex key min max [limit offset count] returns an ordered set of member ranges (from dictionary ranges)

Zrangebyscore key min max [withscores] [limit] returns all members of the ordered set key whose score value is between min and max (including those equal to min or max). The members of the ordered set are arranged in the order of increasing score value (from small to large)

Zrank key member determines the ordered set in the index of a member

ZREM key member [member ...] Remove one or more members from the ordered collection. Members that do not exist will be ignored

Zremrangebylex key min max deletes an ordered set of all members between a given dictionary range

Zremrangebyrank key start stop deletes an ordered set of all members within a given index

Zremrangebyscore key min max deletes an ordered set of all members within a given score

Zrevrange key start stop [withscores] returns an ordered set of member ranges, sorted by score through index, from high score to low score

Zrevrangebyscore key max min [withscores] returns an ordered set of member ranges, sorted from high to low by socre

Zrevrank key member determines the index of an ordered set member, sorted by score, from high score to low score

Zscore key member gets the scores associated with a given member in an ordered set

ZUNIONSTORE destination numkeys key [key ...] Add multiple set sorts, and the resulting sorted set is stored in a new key

Zscan key cursor [match pattern] [count] incremental iteration sort element set and related scores

Redis Series Textbook (III) - common commands

Step 7: Official command manual

If you want to query the detailed usage of each command, please go to the official redis command manual:
http://www.redis.cn/commands.html

For more information, click to learn: https://how2j.cn/k/redis/redis-commands/1369.html