当前位置:网站首页>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
边栏推荐
- 环境加密技术解析
- 攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
- 小程序vant tab组件解决文字过多显示不全的问题
- Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
- Advanced area a of attack and defense world misc Masters_ good_ idea
- A complete tutorial for getting started with redis: redis shell
- 9 - class
- EditPlus--用法--快捷键/配置/背景色/字体大小
- Insert sort, select sort, bubble sort
- Sword finger offer 68 - I. nearest common ancestor of binary search tree
猜你喜欢
攻防世界 MISC 进阶区 Ditf
Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
Redis入门完整教程:Bitmaps
A complete tutorial for getting started with redis: redis shell
P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
Business is too busy. Is there really no reason to have time for automation?
Advanced area a of attack and defense world misc Masters_ good_ idea
Attack and defense world misc advanced area ditf
EditPlus--用法--快捷键/配置/背景色/字体大小
随机推荐
Advanced area of attack and defense world misc 3-11
P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
微信公众号解决从自定义菜单进入的缓存问题
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
The solution to the lack of pcntl extension under MAMP, fatal error: call to undefined function pcntl_ signal()
Redis入门完整教程:哈希说明
页面关闭前,如何发送一个可靠请求
Complete tutorial for getting started with redis: bitmaps
Redis入门完整教程:GEO
Erik baleog and Olaf, advanced area of misc in the attack and defense world
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
Redis入門完整教程:Pipeline
Li Kou 98: verify binary search tree
MySQL Architecture - logical architecture
[ODX studio edit PDX] - 0.2-how to compare two pdx/odx files of compare
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
攻防世界 MISC 进阶区 can_has_stdio?
华泰证券是国家认可的券商吗?开户安不安全?
【室友用一局王者荣耀的时间学会了用BI报表数据处理】
String类中的常用方法