当前位置:网站首页>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
边栏推荐
- Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
- 集群的概述与定义,一看就会
- Breakpoint debugging under vs2019 c release
- Redis入门完整教程:Bitmaps
- Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
- Create Ca and issue certificate through go language
- Logo special training camp section II collocation relationship between words and graphics
- MySQL Architecture - user rights and management
- Business is too busy. Is there really no reason to have time for automation?
- Logo special training camp Section IV importance of font design
猜你喜欢

业务太忙,真的是没时间搞自动化理由吗?

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

Li Kou 98: verify binary search tree

Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click

Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1

Redis入门完整教程:事务与Lua

串口数据帧

Redis入门完整教程:慢查询分析

攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf

Serial port data frame
随机推荐
Redis入门完整教程:有序集合详解
Erik baleog and Olaf, advanced area of misc in the attack and defense world
Test will: bug classification and promotion solution
9 - 类
Hit the core in the advanced area of misc in the attack and defense world
Duplicate ADMAS part name
Attack and defense world misc master advanced zone 001 normal_ png
MYSQL架构——用户权限与管理
La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
醒悟的日子,我是怎么一步一步走向软件测试的道路
Analog rocker controlled steering gear
Practice and principle of PostgreSQL join
质量体系建设之路的分分合合
【机器学习】手写数字识别
环境加密技术解析
Common methods in string class
Detailed explanation of flask context
Sword finger offer 68 - ii The nearest common ancestor of binary tree
9 - class
Redis入门完整教程:Redis使用场景