当前位置:网站首页>Redis入门完整教程:API的理解和使用
Redis入门完整教程:API的理解和使用
2022-07-04 22:29:00 【谷哥学术】
Redis提供了5种数据结构,理解每种数据结构的特点对于Redis开发运
维非常重要,同时掌握Redis的单线程命令处理机制,会使数据结构和命令
的选择事半功倍,本章内容如下:
·预备知识:几个简单的全局命令,数据结构和内部编码,单线程命令
处理机制分析。
·5种数据结构的特点、命令使用、应用场景。
·键管理、遍历键、数据库管理。
2.1 预备
在正式介绍5种数据结构之前,了解一下Redis的一些全局命令、数据结
构和内部编码、单线程命令处理机制是十分有必要的,它们能为后面内容的
学习打下一个好的基础,主要体现在两个方面:第一、Redis的命令有上百
个,如果纯靠死记硬背比较困难,但是如果理解Redis的一些机制,会发现
这些命令有很强的通用性。第二、Redis不是万金油,有些数据结构和命令
必须在特定场景下使用,一旦使用不当可能对Redis本身或者应用本身造成
致命伤害。
2.1.1 全局命令
Redis有5种数据结构,它们是键值对中的值,对于键来说有一些通用的
命令。
1.查看所有键
keys *
下面插入了3对字符串类型的键值对:
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*命令会将所有的键输出:
127.0.0.1:6379> keys *
1) "python"
2) "java"
3) "hello"
2.键总数
dbsize
下面插入一个列表类型的键值对(值是多个元素组成):
127.0.0.1:6379> rpush mylist a b c d e f g
(integer) 7
dbsize命令会返回当前数据库中键的总数。例如当前数据库有4个键,
分别是hello、java、python、mylist,所以dbsize的结果是4:
127.0.0.1:6379> dbsize
(integer) 4
dbsize命令在计算键总数时不会遍历所有键,而是直接获取Redis内置的
键总数变量,所以dbsize命令的时间复杂度是O(1)。而keys命令会遍历所
有键,所以它的时间复杂度是O(n),当Redis保存了大量键时,线上环境
禁止使用。
3.检查键是否存在
exists key
如果键存在则返回1,不存在则返回0:
127.0.0.1:6379> exists java
(integer) 1
127.0.0.1:6379> exists not_exist_key
(integer) 0
4.删除键
del key [key ...]
del是一个通用命令,无论值是什么数据结构类型,del命令都可以将其
删除,例如下面将字符串类型的键java和列表类型的键mylist分别删除:
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
返回结果为成功删除键的个数,假设删除一个不存在的键,就会返回
0:
127.0.0.1:6379> del not_exist_key
(integer) 0
同时del命令可以支持删除多个键:
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.键过期
expire key seconds
Redis支持对键添加过期时间,当超过过期时间后,会自动删除键,例
如为键hello设置了10秒过期时间:
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> expire hello 10
(integer) 1
ttl命令会返回键的剩余过期时间,它有3种返回值:
·大于等于0的整数:键剩余的过期时间。
·-1:键没设置过期时间。
·-2:键不存在
可以通过ttl命令观察键hello的剩余过期时间:
# 还剩 7 秒
127.0.0.1:6379> ttl hello
(integer) 7
...
# 还剩 1 秒
127.0.0.1:6379> ttl hello
(integer) 1
# 返回结果为 -2 ,说明键 hello 已经被删除
127.0.0.1:6379> ttl hello
(integer) -2
127.0.0.1:6379> get hello
(nil)
有关键过期更为详细的使用以及原理会在2.7节介绍。
6.键的数据结构类型
type key
例如键hello是字符串类型,返回结果为string。键mylist是列表类型,返
回结果为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
如果键不存在,则返回none:
127.0.0.1:6379> type not_exsit_key
none
边栏推荐
- Unity vscode emmylua configuration error resolution
- Logo Camp d'entraînement section 3 techniques créatives initiales
- Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
- Redis入门完整教程:慢查询分析
- Detailed explanation of flask context
- Redis入门完整教程:GEO
- 记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
- Lost in the lock world of MySQL
- PMO: compare the sample efficiency of 25 molecular optimization methods
- Sobel filter
猜你喜欢
Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
Attack and defense world misc advanced area can_ has_ stdio?
[machine learning] handwritten digit recognition
攻防世界 MISC 高手进阶区 001 normal_png
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
Redis入门完整教程:Pipeline
[OpenGL] note 29 anti aliasing (MSAA)
Redis入门完整教程:慢查询分析
串口数据帧
Attack and defense world misc advanced grace-50
随机推荐
UML diagram memory skills
Redis入门完整教程:Redis使用场景
攻防世界 MISC 进阶区 hit-the-core
LOGO special training camp section I identification logo and Logo Design Ideas
Li Kou 98: verify binary search tree
蓝队攻防演练中的三段作战
Analysis of environmental encryption technology
Logo special training camp Section IV importance of font design
Logo special training camp Section V font structure and common design techniques
The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
繁华落尽、物是人非:个人站长该何去何从
Recommendation of mobile app for making barcode
md5工具类
Advanced area a of attack and defense world misc Masters_ good_ idea
Easy to use app recommendation: scan QR code, scan barcode and view history
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
环境加密技术解析
页面关闭前,如何发送一个可靠请求
Redis入门完整教程:客户端通信协议