当前位置:网站首页>(一)Redis: 基于 Key-Value 的存储系统
(一)Redis: 基于 Key-Value 的存储系统
2022-08-02 02:38:00 【zhangts20】
1. Redis 介绍与安装
1.1 Redis 基本介绍
Redis 是一种基于 Key-Value 的存储系统,可用作数据库、缓存和消息中间件等。仓库地址:https://github.com/redis/redis
Redis is an open source (BSD licensed), in-memory data structure store used as a database, cache, message broker, and streaming engine.
在 Redis 内部,数据以二进制的形式存放,所以 Redis 支持诸多数据结构,如字符串、列表和集合等。
1.2 Redis 安装(Ubuntu)
下载地址: https://github.com/redis/redis/releases,下载完成后本地解压即可。二进制启动文件均在安装目录下的 src 目录,redis-server 用于服务端的启动、redis-cli 用于客户端。服务端的启动:
$ ./redis-server
572504:C 31 Jul 2022 08:53:47.438 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
572504:C 31 Jul 2022 08:53:47.438 # Redis version=6.2.7, bits=64, commit=00000000, modified=0, pid=572504, just started
572504:C 31 Jul 2022 08:53:47.438 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
572504:M 31 Jul 2022 08:53:47.440 * Increased maximum number of open files to 10032 (it was originally set to 1024).
572504:M 31 Jul 2022 08:53:47.440 * monotonic clock: POSIX clock_gettime
572504:M 31 Jul 2022 08:53:47.441 # A key '__redis__compare_helper' was added to Lua globals which is not on the globals allow list nor listed on the deny list.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.7 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 572504
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
572504:M 31 Jul 2022 08:53:47.441 # Server initialized
572504:M 31 Jul 2022 08:53:47.441 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
572504:M 31 Jul 2022 08:53:47.446 * Loading RDB produced by version 6.2.7
572504:M 31 Jul 2022 08:53:47.446 * RDB age 143485 seconds
572504:M 31 Jul 2022 08:53:47.446 * RDB memory usage when created 0.78 Mb
572504:M 31 Jul 2022 08:53:47.446 # Done loading RDB, keys loaded: 7, keys expired: 0.
572504:M 31 Jul 2022 08:53:47.446 * DB loaded from disk: 0.005 seconds
572504:M 31 Jul 2022 08:53:47.446 * Ready to accept connections
客户端的启动:
$ ./redis-cli
127.0.0.1:6379>
2. 使用
启动 Redis 的服务端后,我们可以在代码端对 Redis 进行操作。从客户端的启动界面可知,我们在操作 Redis 时需指定 IP 地址和端口号,同时指定操作的数据库对象:
import redis
r = redis.StrictRedis.from_url(url='redis://127.0.0.1:6379/0')
建立连接后,可使用 ping 查看建立情况。如果成功建立则返回 PONG,否则返回错误提示信息:
res = r.ping()
字符串是 Redis 常操作的数据类型,get 和 set 是用于操作字符串的方法。
r.set(name='name', value='zhang')
print(r.get(name='name')) # b'zhang'
输出数据类型为 bytes 的二进制形式。同时,方法 append 用于在字符串末尾添加内容,如果对应 name 不存在则新建并初始化:
r.append(key='name', value='wang')
print(r.get(name='name')) # b'zhangwang'
r.append(key='age', value=20)
print(r.get(name='age')) # b'20'
在代码端添加了相关数据后,我们也可在 server-cli 端查看操作结果。首先使用 select 命令切换数据库,前面我们使用的数据库名称为 0。
127.0.0.1:6379> select 0
OK
然后,使用 keys 命令查看所有的 key 值:
127.0.0.1:6379> keys *
1) "name"
2) "age"
结果展现出我们刚才添加的两个字段。此时,可以使用 type 查看对应字段的类型、取对应值等。
127.0.0.1:6379> type name
string
127.0.0.1:6379> get name
"zhangwang"
Redis 默认字段的存储是持久化的,可以通过 expire 方法设置字段的有效时间,单位为秒。
r.expire(name='age', time=5) # 设置字段 age 五秒后过期
time.sleep(5)
print(r.get(name='age')) # None
3. 总结
- 本文总结了 Redis 的基本内容,目前只用到了 Redis 的基本功能,后续会根据使用情况补充相关内容。
- Redis 命令的其他使用可以参考网站 https://www.runoob.com/redis/redis-commands.html.
边栏推荐
- Rasa 3 x learning series - Rasa - 4873 dispatcher Issues. Utter_message study notes
- How engineers treat open source
- 20. 用两个栈实现队列
- [Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
- 一次SQL优化,数据库查询速度提升 60 倍
- 最大层内元素和
- 周鸿祎称微软抄袭,窃取360安全模式
- Flask 报错:WARNING This is a development server. Do not use it in a production deployment
- OC和Swift语言的区别
- 数仓:为什么说 ETL 的未来不是 ELT,而是 EL (T)
猜你喜欢
十字光标太小怎么调节、CAD梦想画图算量技巧
字典常用方法
永磁同步电机36问(三)——SVPWM代码实现
esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机
Chopper webshell feature analysis
Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
2022-07-30 mysql8 executes slow SQL-Q17 analysis
AI目标分割能力,无需绿幕即可实现快速视频抠图
analog IC layout-Parasitic effects
Remember a gorm transaction and debug to solve mysql deadlock
随机推荐
Remember a pit for gorm initialization
nacos startup error, the database has been configured, stand-alone startup
Power button 1374. Generate each character string is an odd number
2022-08-01 反思
MySQL - CRUD operations
Outsourcing worked for three years, it was abolished...
1688以图搜货
OperatingSystemMXBean获取系统性能指标
简单的页面跳转活动
Docker-compose安装mysql
svm.SVC应用实践1--乳腺癌检测
Install mysql using docker
淘宝详情.
The principle and code implementation of intelligent follower robot in the actual combat of innovative projects
Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
PAT甲级打卡-1001-1004
Can Youxuan database import wrongly be restored?
789. 数的范围
2022河南青训联赛第(三)场
[ORB_SLAM2] SetPose, UpdatePoseMatrices