当前位置:网站首页>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
边栏推荐
- ECS settings SSH key login
- Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
- Google collab trample pit
- NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
- Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
- 【图论】拓扑排序
- Redis入门完整教程:发布订阅
- Redis入门完整教程:HyperLogLog
- Hit the core in the advanced area of misc in the attack and defense world
- Redis入门完整教程:事务与Lua
猜你喜欢
攻防世界 MISC 进阶 glance-50
[roommate learned to use Bi report data processing in the time of King glory in one game]
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
Breakpoint debugging under vs2019 c release
浅聊一下中间件
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
Hit the core in the advanced area of misc in the attack and defense world
Install the gold warehouse database of NPC
攻防世界 MISC 进阶区 hit-the-core
安装人大金仓数据库
随机推荐
A complete tutorial for getting started with redis: transactions and Lua
Redis入门完整教程:发布订阅
剑指Offer 68 - II. 二叉树的最近公共祖先
【OpenGL】笔记二十九、抗锯齿(MSAA)
[Lua] Int64 support
String类中的常用方法
ECS settings SSH key login
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
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
Redis入门完整教程:键管理
Sword finger offer 68 - ii The nearest common ancestor of binary tree
Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
攻防世界 misc 高手进阶区 a_good_idea
Li Kou 98: verify binary search tree
Three stage operations in the attack and defense drill of the blue team
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
Redis入门完整教程:Pipeline
Locust performance test - environment construction and use
【剑指offer】1-5题