当前位置:网站首页>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
边栏推荐
- Redis的持久化机制
- 攻防世界 MISC 进阶区 can_has_stdio?
- Locust performance test - environment construction and use
- Redis入門完整教程:Pipeline
- MYSQL架构——逻辑架构
- Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
- Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
- UML图记忆技巧
- Advanced area of attack and defense world misc 3-11
- Redis入门完整教程:事务与Lua
猜你喜欢
A complete tutorial for getting started with redis: hyperloglog
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
[OpenGL] note 29 anti aliasing (MSAA)
Attack and defense world misc advanced area can_ has_ stdio?
质量体系建设之路的分分合合
Attack and defense world misc advanced area Hong
Redis入门完整教程:键管理
Duplicate ADMAS part name
Persistence mechanism of redis
Serial port data frame
随机推荐
MYSQL架构——逻辑架构
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
集群的概述与定义,一看就会
Redis的持久化机制
Notepad++ -- editing skills
环境加密技术解析
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
位运算符讲解
华泰证券是国家认可的券商吗?开户安不安全?
浅聊一下中间件
攻防世界 MISC 进阶区 hit-the-core
攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
Redis入門完整教程:Pipeline
Complete tutorial for getting started with redis: bitmaps
[ODX studio edit PDX] - 0.2-how to compare two pdx/odx files of compare
Attack and defense world misc advanced grace-50
云服务器设置ssh密钥登录
常用技术指标之一文读懂BOLL布林线指标
【图论】拓扑排序