Original author, public number [programmer reading], welcome to pay attention to the public number, reprint the article please indicate the source oh.

In the last article, we briefly talked about Redis application scenarios, installation, how to connect and other relatively basic knowledge, so in this article, we will continue to work hard, continue to learn the basic knowledge of Redis.

In this article, we’ll take a look at Redis data structures and general commands.

Redis data structure

Redis supports many different data structures, including five basic data structures and several more complex data structures, which can meet different application scenarios.

Five basic data structures

  1. String: A String that is the basis for building other data structures

  2. Hash: a Hash list

  3. List the List:

  4. Set: a Set implemented on the basis of a hash list

  5. Sort Set: ordered Set

Complex data structures

  1. Bitmaps: Bitmaps that operate on strings to implement space-saving data structures.

  2. Hyperloglog: Data structure used to estimate the probability of the number of elements in a set.

  3. Geo: geospatial, geographical space index radius query.

  4. BloomFilter: indicates the BloomFilter.

Similarities between different data structures

From the above introduction, we can see the different data structures supported, but in fact, each data structure of Redis consists of a key and a value, which can be abstracted as:

Redis data structure composition

The key value of all data structures is any legal string. The difference between different data structures lies in the different values stored by value.

For example, the simplest String data structure has a value of String, so String can be represented as:

The Hash data structure, whose value is a Hash list, can be expressed as:

Here’s a list of strings and hashes to illustrate, but we’ll talk more about the internal structure of data structures and how they operate in detail in a future article.

Redis common command

The Redis command is divided into 15 topic groups by function. The Kyes topic command is common to all data structures, so it is necessary to learn more about the other data structure commands.

keys

The time complexity of this command is O(N). N increases as the number of keys in Redis increases, so Redis has a large number of keys. The keys command will take a long time to execute. Do not use keys on a production server.

# key = O(n)
keys pattern #pattern can be a string containing a matching pattern, including *,+,? ,[A-Z], etc.
Copy the code

The sample

> mset hello_test1 one hello_test2 two helloa a hellob b
> keys hello*
1) "hello_test1"
2) "hello_test2"
3) "helloa"
4) "hellob"
> keys heelo?
1) "helloa"
2) "hellob"
> keys hello[a-z]
1) "helloa"
2) "hellob"
Copy the code

exists

The exists command is used to check whether one or more keys exist. If multiple keys exist, they are separated by Spaces. The return value of exists is an integer, indicating how many keys exist.

# exists, time complexity O(1)
exists key [key ...]
Copy the code

The sample

> set test1 t1
> exists test1 test2 
(integer1)Only one key exists
> exists test3 test3
(integer) 0 #key does not exist
Copy the code

del

The del command is used to delete one or more keys, which are separated by Spaces. The returned value is an integer, indicating the number of existing keys successfully deleted. Therefore, if only one key is deleted, you can judge the success from the returned value.

# del, time complexity O(n)
del key [key ...]
Copy the code

The sample

> set test t
> del test
(integer) 1
> mset test1 2 test2 1
> del test1 test2 test3
(integer2)# return 2, indicating successful deletion of both
# delete again, return 0, because the number of successful deletes is 0
> del test1 test2 test3
(integer) 0
Copy the code

expire,pexpire

Expire sets the number of seconds after the key expires, and pexpire sets the number of milliseconds after the key expires, returning 1 on success and 0 on failure.

Expire: O(1)
expire key seconds

# pexpire: O(1)
pexpire key milliseconds
Copy the code

The sample

> mset test test_value test1 test1_value
> expire test 10 # Set expiration to 10 seconds
(integer) 1
> pexpire test1_value 10000 # Set expiration to 10000 milliseconds (10s)
(integer) 1
> expire ttt 100
(integer) 0 Failed to set key, return 0
Copy the code

ttl,pttl

The TTL and PTTL commands are used to obtain the expiration time of a key. The returned value is an integer, representing the following meanings:

  1. If the key does not exist or expires, -2 is returned.
  2. If the key exists and is permanently valid, -1 is returned.
  3. Returns the number of seconds remaining (PTTL is milliseconds) when the key has an expiration time set
TTL command, time complexity O(1)
ttl key

# PTTL, time complexity O(1)
pttl key
Copy the code

Example (DEMO of TTL, PTTL is similar)

> set test test
> expire test 100
> ttl test
(integer) 98# return the number of seconds left
> set test1 # permanent
> ttl test1
(integer) -1
> ttl test2
(integer2) -# Does not exist or expires
# 100 seconds
> ttl test # test has expired
(integer2) -Copy the code

expireat,pexpireat

The expreat parameter timestamp is expressed in seconds, and the pexpireat parameter timestamp is expressed in milliseconds. Similar to the expire and pEXPIRE functions, return 1 for success and 0 for failure.

#expireat = O(1)
expireat key timestamp

#pexpireat = O(1)
pexpireat key milliseconds-timestamp
Copy the code

The sample

> set test test
> expireat test 1560873600 # 2019-06-19 00:00:00
(integer1 >)set test1 test1
> pexpireat test1, 156087360000# 2019-06-19 00:00:00 millisecond representation
(integer1)Copy the code

persist

If the key has an expiration time, run the persist command to remove it and return 1. If the key does not exist or is permanently valid, return 0.

# persist, time complexity O(1)
persist key
Copy the code

The sample

> set test test
> ttl test
(integer1) -# indicates that the value is valid forever
> persist test
(integer) 0 Run the persist command for permanently valid or nonexistent keys
> expire test 10
(integer) 1
> persist test
(integer1)Copy the code

type

To see what type of key data structures, the return value to a string and a list, the set, the hash, zset, respectively we mentioned earlier Redis five kinds of basic data structure.

Geo, Hyperloglog,bitmaps and other complex data structures are implemented on these five basic data structures. For example, geo is a Zset type, and Hyperloglog and Bitmaps are strings.

# type, time complexity O(1)
type key
Copy the code

The sample

> set test test
> type test
string
> hset htest test test
> type htest
hash
Copy the code

summary

The above introduction is the most commonly used in Redis general command, although simple, but it is very necessary to master its usage and use of matters needing attention, in fact, for ordinary developers, most of the time, also just use these basic general commands to operate Redis.


Your attention is the biggest encouragement on my writing road!