当前位置:网站首页>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
边栏推荐
猜你喜欢

The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development

【剑指Offer】6-10题

Locust performance test - environment construction and use

Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf

集群的概述与定义,一看就会

Redis入门完整教程:客户端通信协议

Unity-VScode-Emmylua配置报错解决

The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal

Mongodb aggregation operation summary

Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
随机推荐
[machine learning] handwritten digit recognition
环境加密技术解析
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
Attack and defense world misc advanced area ditf
Redis入門完整教程:Pipeline
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
云服务器设置ssh密钥登录
Business is too busy. Is there really no reason to have time for automation?
[odx Studio Edit pdx] - 0.2 - Comment comparer deux fichiers pdx / odx
9 - class
攻防世界 MISC 进阶区 3-11
Sobel filter
Duplicate ADMAS part name
UML图记忆技巧
MySQL Architecture - logical architecture
Analysis of environmental encryption technology
MYSQL架构——用户权限与管理
mamp下缺少pcntl扩展的解决办法,Fatal error: Call to undefined function pcntl_signal()
堆排序代码详解
剑指Offer 68 - II. 二叉树的最近公共祖先