当前位置:网站首页>(1) Redis: Key-Value based storage system
(1) Redis: Key-Value based storage system
2022-08-02 02:45:00 【zhangts20】
1. Redis 介绍与安装
1.1 Redis 基本介绍
Redis 是一种基于 Key-Value 的存储系统,可用作数据库、Caching and message middleware, etc.仓库地址: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 内部,Data is stored in binary form,所以 Redis Many data structures are supported,如字符串、Lists and sets, etc.
1.2 Redis 安装(Ubuntu)
下载地址: https://github.com/redis/redis/releases,After the download is complete, unzip it locally.The binary startup files are all in the installation directory src 目录,redis-server Used to start the 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 after the server,We can match at the code end Redis 进行操作.It can be seen from the startup interface of the client,我们在操作 Redis 时需指定 IP 地址和端口号,Also specify the database object for the operation:
import redis
r = redis.StrictRedis.from_url(url='redis://127.0.0.1:6379/0')
建立连接后,可使用 ping Check out the build.Returns if established successfully PONG,Otherwise, return an error message:
res = r.ping()
字符串是 Redis Commonly manipulated data types,get 和 set are methods for manipulating strings.
r.set(name='name', value='zhang')
print(r.get(name='name')) # b'zhang'
输出数据类型为 bytes 的二进制形式.同时,方法 append Used to add content to the end of a string,如果对应 name If it does not exist, create and initialize it:
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'
After adding the relevant data on the code side,我们也可在 server-cli View the operation result.首先使用 select 命令切换数据库,The database name we used earlier was 0.
127.0.0.1:6379> select 0
OK
然后,使用 keys 命令查看所有的 key 值:
127.0.0.1:6379> keys *
1) "name"
2) "age"
The result shows the two fields we just added.此时,可以使用 type Check the type of the corresponding field、Take the corresponding value, etc.
127.0.0.1:6379> type name
string
127.0.0.1:6379> get name
"zhangwang"
Redis The storage of default fields is persistent,可以通过 expire The method sets the valid time of the field,单位为秒.
r.expire(name='age', time=5) # 设置字段 age Expires after five seconds
time.sleep(5)
print(r.get(name='age')) # None
3. 总结
- 本文总结了 Redis 的基本内容,目前只用到了 Redis 的基本功能,Subsequent additions will be made based on usage.
- Redis Other uses of the command can refer to the website https://www.runoob.com/redis/redis-commands.html.
边栏推荐
- 字典常用方法
- esp32经典蓝牙和单片机连接,,,手机蓝牙作为主机
- 亲身经历过的面试题
- feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
- MySQL索引优化实战
- FOFAHUB usage test
- 2022-08-01 Install mysql monitoring tool phhMyAdmin
- Remember a pit for gorm initialization
- cocos中使用async await异步加载资源
- NAS和私有云盘的区别?1篇文章说清楚
猜你喜欢
Talking about the "horizontal, vertical and vertical" development trend of domestic ERP
BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
How engineers treat open source
analog IC layout-Environmental noise
面对职场“毕业”,PM&PMO应该如何从容的应对?如何跳槽能够大幅度升职加薪?
FOFAHUB使用测试
字符串常用方法
详解最强分布式锁工具:Redisson
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
随机推荐
线程的不同状态
【LeetCode】102.二叉树的层序遍历
Outsourcing worked for three years, it was abolished...
yaml
PAT甲级打卡-1001-1004
240...循迹
架构:应用架构的演进以及微服务架构的落地实践
NIO‘s Sword(牛客多校赛)
[Server data recovery] Data recovery case of server Raid5 array mdisk disk offline
EasyGBS平台播放视频时偶尔出现播放失败是什么原因?
菜刀webshell特征分析
【LeetCode】145.二叉树的后序遍历
欧拉公式的证明
The state status is displayed incorrectly after the openGauss switch
Chopper webshell feature analysis
忽晴忽雨
AI target segmentation capability for fast video cutout without green screen
1688API
指针数组和数组指针
【LeetCode】144.二叉树的前序遍历