Redis installation, common data structures and related commands

Time:2021-6-9

1、 About redis

1. What is redis?

Redis is an open source, free and high performance key – value database.
Data persistence is supported, that is, the data in memory can be stored in the disk, and the data in the disk can be loaded and used again after the service is restarted.
Support a variety of types, such as string, list, set, Zset, hash, etc.
Support data backup, master – slave mode data backup (Master – slave).
All operations of redis are atomic, that is, they are executed successfully or not.
Official website:
  https://redis.io/

 

2. Install redis

(1) Using docker to install and redis

[reference address: (install docker on Linux (centos7)]
    https://www.cnblogs.com/l-y-h/p/12622730.html#_label0
    
[reference address: (docker to install redis)]
    https://www.cnblogs.com/l-y-h/p/12622730.html#_label6

 

(2) Centos7 compile and install redis 5.0.8
Step 1: download the redis package.

[download address of official website:]
    https://redis.io/download
    http://download.redis.io/releases/redis-5.0.8.tar.gz
    
[download directly in Linux:]
    [[email protected] local]# wget http://download.redis.io/releases/redis-5.0.8.tar.gz

  

Step 2: decompress and compile.

[decompression:]
    [[email protected] local]# tar -zxvf redis-5.0.8.tar.gz 
    
[enter the extracted directory and compile it (provided that GCC, make and other programs have been installed in the system):]
    [[email protected] local]# cd redis-5.0.8/
    [[email protected] local]# make
    
[after compiling: enter the SRC directory to see redis server, redis CLI and other programs]
    [[email protected] redis-5.0.8]# cd src/

 

 

 

Step 3: start the service program (redis server), and use the built-in client (redis CLI) for interaction.

[redis server]]
    Method 1: 
        . / redis server starts redis according to the default configuration file
    Method 2:
        . / redis server.. / redis.conf starts redis according to the specified configuration file (you can customize the location of the configuration file)

[open client for interaction (redis CLI):]
    ./redis-cli

Start a redis service by using a connection mode in xshell.

 

 

 

Open another xshell connection, execute redis cli, and execute. / redis cli in the SRC directory of the installation package.

 

 

 

Step 4: close the service

[input instruction in redis cli]
    [[email protected] src]# ./redis-cli shutdown

 

 

 

3. Redis visualization tool download

(1) Download from official website (to be charged)
Address: https://redisdesktop.com/pricing

 

 

 

(2) Download the cracked version
Link: https://pan.baidu.com/s/1v3ZJEdzEGBIf5gYGsyhRBw
Extraction code: ubhz
perhaps
  http://www.downza.cn/soft/210734.html

 

 

 

 

 

 

 

 

 

2、 Key related commands in redis

Commands are not case sensitive, but data is case sensitive.

1. Keys (used to view the key list)

[format:]
    Keys pattern is used to find the keys that match the pattern format

[example:]
    127.0.0.1:6379> keys *

 

 

 

2. Del (used to delete a key)

[format:]
    Del key when the key exists, delete the key and return 1. If the key does not exist, it is ignored and 0 is returned.
    
[example:]
    127.0.0.1:6379> del a

 

 

 

3. Exists (to determine whether a key exists)

[format:]
    Exists key is used to determine whether a key exists. If it exists, 1 is returned. If it does not exist, 0 is returned
    
[example:]
    127.0.0.1:6379> exists b

 

 

 

4. Type (returns the type of the specified key storage value)

[format:]
    Type key is used to determine the type of a key

[example:]
    127.0.0.1:6379> type c

 

 

 

5. Rename, renamenx (used to change the name of the key)

[rename format:]
    rename old_ key_ name new_ key_ Name is used to modify the key name.
Note:
    If old_ key_ When name does not exist, an error is returned.
    If new_ key_ If name exists, the old value will be covered and OK will be returned. 

[renamenx format:]
    renamenx old_ key_ name new_ key_ Name is used to modify the key name
Note:
    If old_ key_ When name does not exist, an error is returned.
    If new_ key_ If name exists, it will not be modified and 0 will be returned. 

[example:]
    127.0.0.1:6379> rename a e
    127.0.0.1:6379> renamenx a e

 

 

 

 

 

 

6. Random key (randomly returns a key of the current library)

[format:]
    Random key randomly returns a key in the current library. If there is no data in the current library, null is returned.

[example:]
    127.0.0.1:6379> randomkey

 

 

 

3、 Redis data type string and common commands

1. String (string)

The most basic type, a key corresponds to a value.
Is binary security, can store any data (such as pictures), storage maximum of 512 MB.

2. Set (set a value of string type to the specified key and override it directly regardless of the type)

[format:]
    Set key value sets a value for the key.
    
[example:]
     127.0.0.1:6379> set b world

 

 

 

3. Get (get the value of the specified key)

[format:]
    Get key is used to return the value of key,
Note:
    If the key does not exist, null is returned,
    If the key does not store string data, an error is returned
    
[example:]
    127.0.0.1:6379> get b

 

 

 

4. Getrange (intercepts the string at the specified position of the value stored in the key)

[format:]
    Getrange key start end intercepts strings, including start and end (counting from 0).
    
[example:]
    127.0.0.1:6379> getrange a 0 -1
    127.0.0.1:6379> getrange a 3 7

 

 

 

5. GetSet (sets the value of the specified key)

[format:]
    GetSet key value sets the value of the specified key
Note:
    When the key does not exist, null is returned
    When the value of key is not string type, an error is returned (different from set).

[example:]
    127.0.0.1:6379> getset a world

 

 

 

6. Mget (get the value of multiple keys at one time)

[format:]
    Mget key1 [key2] get values of multiple keys at one time
Note:
    If the value of key does not belong to string, null will be returned.
    If the key does not exist, null is returned.

[example:]
    127.0.0.1:6379> mget a b

 

 

 

7. Mset, msetnx (set multiple key value pairs at a time)

[Mset format:]
    Mset key1 value1 [key2 Value2] set multiple key value pairs at one time
    
[msetnx format:]
    Msetnx key1 value1 [key2 Value2] set multiple key value pairs at one time
Note:
    The key cannot be set until it does not exist.
    
[example:]
    127.0.0.1:6379> mset a hello b world
    127.0.0.1:6379> msetnx c java d python

 

 

 

8. Setnx (set value when key does not exist)

[format:]
    Setnx key value adds a key - value pair that never exists
Note:
    If the key exists, the value will not be set and 0
    If the key does not exist, set the value and return 1

 

 

 

9. Append (appends a string to the value of the specified key)

[format:]
    Append key value appends a string to the specified key
Note:
    If the key does not exist, it is equivalent to set key value
    If the key exists, the string is appended to the end of its value.

[example:]
    127.0.0.1:6379> append a world

 

 

 

10. Incr, incrby, decr, decrby

[format:]
    Incr key adds 1 to the value of the key
    Incrby key value adds the specified value to the value of the key 
    Decr key subtracts 1 from the value of key
    Decrby key value subtracts the specified value from the value of the key
Note:
    The value of value must be a string that can be converted to a numeric value for successful execution

[example:]
    127.0.0.1:6379> incr b
    127.0.0.1:6379> decr b
    127.0.0.1:6379> incrby b 20
    127.0.0.1:6379> decrby b 10

 

 

 

11. Strlen (gets the length of the value string of the specified key)

[format:]
    Strlen key gets the length of the string stored in the value of the specified key
    
[example:]
    127.0.0.1:6379> strlen a

 

 

 

4、 Redis data type — hash and common commands

1. Hash (hash, hash)

Is a field – value collection of string type key (refers to the hash table name). Suitable for storing objects.
You can store 2 ^ 32 – 1 key value pairs.

2. Hkeys (get all fields of the specified hash table key)

[format:]
    Hkeys key is used to obtain all fields of the specified key.
Note:
    If the key does not exist, an empty list information will be prompted.

[example:]
    127.0.0.1:6379> hkeys test

 

 

 

3. Hset (assign values to fields in hash table)

[format:]
    Hset key field value assign value to the field field of the hash table key.
Note:
    If the key does not exist, it will be created.
    If the field is repeated, the new value will override the old value.

[example:]
    127.0.0.1:6379> hset test a helloworld

 

 

 

4. Hmset (set multiple field – values to the specified key at the same time)

[format:]
    Hmset key field1 value1 [field2 Value2] set multiple field - value pairs to the hash table key at the same time
Note:
    If the field is repeated, the new value covers the old value.

[example:]
    127.0.0.1:6379> hmset test a hello d world

 

 

 

5. Hget, hmget, hgetall (get the field – value value of the hash table key)

[hget format:]
    Hget key field is used to return the value of the specified field in the specified hash table key
    
[hmget format:]
    Hmget key field1 [field2] is used to return the value of one or more fields at a time
    
[hgetall format:]
    Hgetall key returns all the field - values under the specified key
    
[example:]
    127.0.0.1:6379> hget test a
    127.0.0.1:6379> hmget test a b c e
    127.0.0.1:6379> hgetall test

 

 

 

6. Hvals (get the corresponding values of all fields under the specified key)

[format:]
    Hvals key returns the value corresponding to all fields under the specified key
    
[example:]  
    127.0.0.1:6379> hvals test

 

 

 

7. HLEN (get the number of fields in the specified key)

[format:]
    HLEN key gets the total number of fields in the specified hash table key

[example:]
    127.0.0.1:6379> hlen test

 

 

 

8. HDEL (delete one or more fields in the specified key)

[format:]
    HDEL key field1 [field2] delete the specified field in the specified hash table key
    
[example:]
    127.0.0.1:6379> hdel test a

 

 

 

9. Hexists (determine whether there is a field in the specified key)

[format:]
    Hexists key field determines whether there is a field in the specified hash table key. If there is no field, 0 is returned. If there is a field, 1 is returned.

[example:]
    127.0.0.1:6379> hexists test a

 

 

 

5、 Redis data type — list and common commands

1. List (list)

Is a list of strings, sorted according to the insertion order. A key corresponds to multiple values.
You can store 2 ^ 32 – 1 value elements.

2. Lpush, lpushx (insert one or more values into the head of the list)

[lpush format:]
    lpush key value1 [value2]          
Note:
    Insert value into the head of the list. If the key does not exist, create a key

[lpushx format:]
    lpush key value1 [value2]         
Note:
    Insert value into the head of the list. If the key does not exist, it will be invalid and return an empty list information.
            
[example:]
    127.0.0.1:6379> lpush test 1 2
    127.0.0.1:6379> lpushx test 3 4

 

 

 

3. Rpush, rpushx (insert one or more values at the end of the list)

[rpush format:]
    rpush key value1 [value2]          
Note:
    Insert value into the head of the list. If the key does not exist, create a key

[rpushx format:]
    rpush key value1 [value2]         
Note:
    Insert value into the head of the list. If the key does not exist, it will be invalid and return an empty list information.
            
[example:]
    127.0.0.1:6379> rpush test 1 2
    127.0.0.1:6379> rpushx test 3 4

 

 

 

4. Lrange (returns the list of the specified range)

[format:]
    lrange key start end
Note:
    Returns the list within the specified range, where - 1 is the first to last number and 0 is the first number

[example:]
    127.0.0.1:6379> lrange test 0 -1
    127.0.0.1:6379> lrange test -3 3

 

 

 

5. Llen (get the length of the list)

[format:]
    llen key 
Note:
    Returns the length of a list. If the key does not exist, it returns 0.

[example:]
    127.0.0.1:6379> llen test

 

 

 

6. Lindex (used to get elements with specified subscripts in the list)

[format:]
    lindex key index
Note:
   Returns the element in the list with the specified index, where - 1 is the penultimate number and 0 is the first number

[example:]
    127.0.0.1:6379> lindex test 0
    127.0.0.1:6379> lindex test -1

 

 

 

7. Lpop (remove the first element from the list header)

[format:]
    Lpop key removes the first element from the key header and returns null when the key does not exist
    
[example:]
    127.0.0.1:6379> lpop test

 

 

 

8. Rpop (removes the last element from the end of the list)

[format:]
    Rpop key removes the last element from the tail of the key and returns null when the key does not exist
    
[example:]
    127.0.0.1:6379> rpop test

 

 

 

9. Lset (set element value by subscript of list)

[format:]
    lset key index value
Note:
    Set the element in the list that specifies the subscript index, where - 1 is the penultimate number and 0 is the first number

[example:]
    127.0.0.1:6379> lset test 0 hello

 

 

 

10. Ltrim (intercepts elements of a specified range in a list)

[format:]
    ltrim key start end
Note:
    Intercept the elements in the specified range in the list, and the elements not in the range will be deleted. Where - 1 is the penultimate number and 0 is the first number
    
[example:]
    127.0.0.1:6379> ltrim test 1 3

 

 

 

6、 Redis data type — set and common commands

1. Set

Is an unordered collection of strings, elements are not repeated and unordered.
One key corresponds to multiple values. You can store 2 ^ 32 – 1 value elements.

2. Sadd (add one or more elements to a collection)

[format:]
    sadd key value1 [value2]
Note:
    Add one or more elements to the set key. Duplicate elements will not be added.
            
[example:]
    127.0.0.1:6379> sadd test 1 2 3 1 2

 

 

 

3. Smembers (used to return all elements of the collection)

[format:]
    smembers key
Note:
    Return all the elements of the specified set key. If the key does not exist, return (empty list or set)
    
[example:]
    127.0.0.1:6379> smembers test

 

 

 

4. Scar (used to return the number of elements in the current collection)

[format:]
    scard key
Note:
    Returns the number of elements of the specified key.
    
[example:]
    127.0.0.1:6379> scard test

 

 

 

5. Sismember (determine whether the set contains an element)

[format:]
    sismember key value
Note:
    Judge whether there is an element value in the set key
    
[example:]
    127.0.0.1:6379> sismember test 4

 

 

 

6. SREM (remove one or more elements)

[format:]
    srem key value1 [value2]
Note:
    Remove one or more elements and match by value.
    
[example:]
    127.0.0.1:6379> srem test 2 3 4

 

 

 

7. Spop (randomly remove one or more elements)

[format:]
    spop key [count]
Note:
    Count indicates the number of removed elements. The default value is 1. If the key does not exist, null is returned.
        
[example:]
    127.0.0.1:6379> spop test 6
    127.0.0.1:6379> spop test

 

 

8. Srandmember (returns one or more elements at random)

[format:]
    srandmember key [count]

[example:]
    127.0.0.1:6379> srandmember test 4

 

 

 

9. Smove (moves a specified element from one collection to another)

[format:]
    smove source destination member
Note:
    Move the member element from the source collection to the destination collection.

[example:]
    127.0.0.1:6379> smove test test1 4

 

 

 

7、 Redis data type — Zset and common commands

1. Zset (ordered set)

Is an ordered set of strings, elements are not repeated and ordered,
Internally maintain a score of double type, sort elements according to the value, and double value can be repeated.
One key corresponds to multiple values. You can store 2 ^ 32 – 1 value elements.

2. Zadd (add one or more elements to a collection)

[format:]
    zadd key score1 value1 [score2 value2]
Note;
    Score is the specified integer or fraction, used to sort.
    Add one or more elements to the collection. If an element already exists, the old score will be overridden.

[example:]
    127.0.0.1:6379> zadd test 2 hello 1 world 3 helloworld

 

 

3. Zrange, zrevrange (returns the set elements of the specified range)

[zrange format:]
    zrange key start end [withscores]
Note:
    They are arranged in increasing order according to the score of the elements.
    WithCores represents the score of the display element.
    Where - 1 is the penultimate number and 0 is the first number.

[zrevrange format:]
    zrevrange key start end [withscores]
Note:
    The elements are sorted in descending order of score.

[example:]    
    127.0.0.1:6379> zrange test 0 -1
    127.0.0.1:6379> zrange test 0 -1 withscores

 

 

 

 

4. Zcard, zcount (get the number of elements in the set)

[zcard format:]
    zcard key
Note:
    Gets the number of all elements of the specified key

[zcount format:]
    zcount key min max
Note:
    Gets the number of elements within the specified score range of the specified key.

[example:]
    127.0.0.1:6379> zcard test
    127.0.0.1:6379> zcount test 1.5 4.6

 

 

5. Zscore (returns the score value of the specified element)

[format:]
    zscore key member
Note:
    Returns the score of the specified member in the specified key
    
[example:]
    127.0.0.1:6379> zscore test hello

 

 

6. Zrem (remove one or more elements)

[format:]
    zrem key member1 [member2]
Note:
    Remove one or more members from the collection.
    
[example:]
    127.0.0.1:6379> zrem test world hello

 

 

7. Zrank, zrevrank (get the ranking of the specified element in the collection)

[zrank format:]
    zrank key member
Note:
    It is arranged in ascending order according to the score and returns the ranking of members in the sequence.
    Where, 0 is the first place, increasing in order.

[zrevrank format:]
    zrevrank key member
Note:
    In descending order of score, the ranking of members in the sequence is returned.

[example:]
    127.0.0.1:6379> zrank test java
    127.0.0.1:6379> zrevrank test java

 

 

8. Zre range by rank, zre range by score

[zre range by rank format:]
    zremrangebyrank key start end
Note:
    Remove the elements in the [start, end] range according to the ranking.

[zre range by score format:]
    zremrangebyscore key min max
Note:
    According to the score value, remove the elements in the [min, Max] interval    
    
[example:]
    127.0.0.1:6379> zremrangebyrank test1 2 3
    127.0.0.1:6379> zremrangebyscore test2 2 100

 

Recommended Today

What is “hybrid cloud”?

In this paper, we define the concept of “hybrid cloud”, explain four different cloud deployment models of hybrid cloud, and deeply analyze the industrial trend of hybrid cloud through a series of data and charts. 01 introduction Hybrid cloud is a computing environment that integrates multiple platforms and data centers. Generally speaking, hybrid cloud is […]