当前位置:网站首页>A complete tutorial for getting started with redis: understanding and using APIs

A complete tutorial for getting started with redis: understanding and using APIs

2022-07-04 23:00:00 Gu Ge academic

Redis Provides 5 Data structures , Understanding the characteristics of each data structure is important for Redis Development and transportation
Dimension is very important , At the same time master Redis The single thread command processing mechanism , Will make data structures and commands
With half the effort , This chapter is as follows :
· Preliminary knowledge : A few simple global commands , Data structure and internal coding , Single threaded command
Processing mechanism analysis .
·5 The characteristics of a data structure 、 Command to use 、 Application scenarios .
· Key management 、 Traversal keys 、 Database management .

2.1  get set
In the official introduction 5 Before the data structure , Get to know Redis Some global commands 、 Data junction
Structure and internal coding 、 Single thread command processing mechanism is very necessary , They can be used for the following
Lay a good foundation for learning , It is mainly reflected in two aspects : First of all 、Redis There are hundreds of orders
individual , It is difficult to learn by rote , But if you understand Redis Some mechanisms of , Will find
These commands have strong versatility . second 、Redis It's not all gold , Some data structures and commands
Must be used in specific scenarios , Once used improperly, it may be right Redis Caused by itself or application itself
Fatal injury .

2.1.1  Global command
Redis Yes 5 Data structures , They are the values in the key value pair , For keys, there are some general
command .
1. Look at all the keys
keys *
Inserted below 3 For key value pairs of string type :
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> set java jedis
OK
127.0.0.1:6379> set python redis-py
OK
keys* The command will output all keys :
127.0.0.1:6379> keys *
1) "python"
2) "java"
3) "hello"
2. Total number of keys
dbsize
Next, insert a list type key value pair ( Values are composed of multiple elements ):
127.0.0.1:6379> rpush mylist a b c d e f g
(integer) 7

dbsize The command returns the total number of keys in the current database . For example, the current database has 4 Key ,
Namely hello、java、python、mylist, therefore dbsize The result is 4:
127.0.0.1:6379> dbsize
(integer) 4
dbsize The command does not traverse all keys when calculating the total number of keys , But directly get Redis Built in
Key count variable , therefore dbsize The time complexity of the command is O(1). and keys The command will traverse
There are keys , So its time complexity is O(n), When Redis When a large number of keys are saved , The online environment
No use .
3. Check if the key exists
exists key
Returns... If the key exists 1, Returns if it does not exist 0:
127.0.0.1:6379> exists java
(integer) 1
127.0.0.1:6379> exists not_exist_key
(integer) 0
4. Delete key
del key [key ...]
del It's a general order , No matter what data structure type the value is ,del Commands can be used to
Delete , For example, the following will be string type keys java And list type keys mylist Delete separately :
127.0.0.1:6379> del java
(integer) 1
127.0.0.1:6379> exists java
(integer) 0
127.0.0.1:6379> del mylist
(integer) 1
127.0.0.1:6379> exists mylist
(integer) 0
The returned result is the number of keys deleted successfully , Suppose you delete a nonexistent key , It will return
0:
127.0.0.1:6379> del not_exist_key
(integer) 0
meanwhile del The command can support deleting multiple keys :
127.0.0.1:6379> set a 1
OK
127.0.0.1:6379> set b 2
OK
127.0.0.1:6379> set c 3
OK
127.0.0.1:6379> del a b c
(integer) 3
5. Key expired
expire key seconds
Redis Support adding expiration time to key , When the expiration time is exceeded , The key is automatically deleted , example
If key hello Set up 10 Second expiration time :
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> expire hello 10
(integer) 1
ttl The command returns the remaining expiration time of the key , It has 3 A return value :
· Greater than or equal to 0 The integer of : Key remaining expiration time .

·-1: Key does not set expiration time .
·-2: The key doesn't exist
Can pass ttl Command observation key hello The remaining expiration time of :
# And then there were 7 second
127.0.0.1:6379> ttl hello
(integer) 7
...
# And then there were 1 second
127.0.0.1:6379> ttl hello
(integer) 1
# The return result is -2 , Description Key hello It has been deleted
127.0.0.1:6379> ttl hello
(integer) -2
127.0.0.1:6379> get hello
(nil)
There are key expiration dates, and more detailed usage and principles will be in 2.7 Section introduction .
6. The data structure type of the key
type key
For example, key hello It's a string type , The return result is string. key mylist It's the list type , return
The result is list:
127.0.0.1:6379> set a b
OK
127.0.0.1:6379> type a
string
127.0.0.1:6379> rpush mylist a b c d e f g
(integer) 7
127.0.0.1:6379> type mylist
list
If the key doesn't exist , Then return to none:
127.0.0.1:6379> type not_exsit_key
none

原网站

版权声明
本文为[Gu Ge academic]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042229264239.html