我们一起了解Redis的爱恨情仇

数据库 Redis
Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API。

Redis是一个key-value 存储系统,是跨平台的非关系型数据库。

Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库,并提供多种语言的 API。

Redis 通常被称为数据结构服务器,因为值(value)可以是字符串(String)、哈希(Hash)、列表(list)、集合(sets)和有序集合(sorted sets)等类型。

redis的安装介质在:https://github.com/tporadowski/redis/releases

下载后运行得到一个运行路径。

修改redis.windows-service.conf文件,一个是端口,一个是密码认证

#bind 127.0.0.1             #注释掉这一句,使redis可以外部访问
port 6379 #默认端口,可以改成别的端口
protected-mode yes #修改为yes,开启保护模式,默认是yes
#daemonize no #这一句是注释的,windows版本不支持,默认是no
requirepass 123456 #密码
appendonly yes

进行指定目录下,运行redis

# 启动redis失败 Could not create server TCP listening socket 127.0.0.1:6379: bind: 操作成功

操作办法:

redis命令,设置key,value值

上面提到的字符串(String)、哈希(Hash)、列表(list)、集合(sets)和有序集合(sorted sets)等类型,具体操作也不复杂

C:\Program Files\Redis>redis-cli.exe -h 127.0.0.1 -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set key1 v1
OK
127.0.0.1:6379> get key1
"v1"
127.0.0.1:6379> get key
(nil)
127.0.0.1:6379> set name "wbq"
OK
127.0.0.1:6379> get name
"wbq"
127.0.0.1:6379> set name "whp"
OK
127.0.0.1:6379> get name
"whp"
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> hmset name field1 "wbq" field2 "whp"
OK
127.0.0.1:6379> hget name field1
"wbq"
127.0.0.1:6379> hget name field2
"whp"
127.0.0.1:6379> lpush alist redis
(integer) 1
127.0.0.1:6379> lpush alist db2
(integer) 2
127.0.0.1:6379> lpush alist mysql
(integer) 3
127.0.0.1:6379> lrange alist 0 2
1) "mysql"
2) "db2"
3) "redis"
127.0.0.1:6379> lrange alist 0 1
1) "mysql"
2) "db2"
127.0.0.1:6379> sadd aset set1
(integer) 1
127.0.0.1:6379> sadd aset set2
(error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
127.0.0.1:6379> sadd aset set3
(error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.
127.0.0.1:6379> sadd aset set3 [member ...]

又报错了,(error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.

大意就是说无法持久化写入磁盘,进行配置

127.0.0.1:6379> config set stop-writes-on-bgsave-error no
OK
127.0.0.1:6379> sadd aset set2
(integer) 1
127.0.0.1:6379> sadd aset set3
(integer) 1
127.0.0.1:6379> sadd aset set4
(integer) 1
127.0.0.1:6379> sadd aset set2
(integer) 0
127.0.0.1:6379> smembers aset
1) "set4"
2) "set3"
3) "set1"
4) "set2"
127.0.0.1:6379>

查看服务端错误日志,大意是说没有写入权限

[34752] 27 Apr 11:50:16.328 # fork operation complete
[34752] 27 Apr 11:50:16.340 # Background saving error
[34752] 27 Apr 11:50:22.046 * 1 changes in 900 seconds. Saving...
[34752] 27 Apr 11:50:22.115 * Background saving started by pid 7944
[7944] 27 Apr 11:50:22.251 # Failed opening the RDB file dump.rdb (in server root dir C:\Program Files\Redis) for saving: 数据无效。
[7944] 27 Apr 11:50:22.253 # rdbSave failed in qfork: Permission denied
[34752] 27 Apr 11:50:22.325 # fork operation complete
[34752] 27 Apr 11:50:22.338 # Background saving error
[34752] 27 Apr 11:50:28.046 * 1 changes in 900 seconds. Saving...
[34752] 27 Apr 11:50:28.112 * Background saving started by pid 20004
[20004] 27 Apr 11:50:28.229 # Failed opening the RDB file dump.rdb (in server root dir C:\Program Files\Redis) for saving: 数据无效。
[20004] 27 Apr 11:50:28.231 # rdbSave failed in qfork: Permission denied

对文件夹的读写权限进行变更后,一切正常

[34752] 27 Apr 11:50:28.326 # fork operation complete
[34752] 27 Apr 11:50:28.343 # Background saving error
[34752] 27 Apr 11:50:34.060 * 1 changes in 900 seconds. Saving...
[34752] 27 Apr 11:50:34.126 * Background saving started by pid 32840
[34752] 27 Apr 11:50:34.339 # fork operation complete
[34752] 27 Apr 11:50:34.383 * Background saving terminated with success

继续进行各类型键值操作。

127.0.0.1:6379> zadd asortedset 0 redis
(integer) 1
127.0.0.1:6379> zadd asortedset 1 db2
(integer) 1
127.0.0.1:6379> zadd asortedset 0 sqlserver
(integer) 1
127.0.0.1:6379> zrangebyscore asortedset 0 5
1) "redis"
2) "sqlserver"
3) "db2"
127.0.0.1:6379>

Redis支持多个数据库,并且每个数据库的数据是隔离的不能共享,并且基于单机才有,如果是集群就没有数据库的概念。

Redis是一个字典结构的存储服务器,而实际上一个Redis实例提供了多个用来存储数据的字典,客户端可以指定将数据存储在哪个字典中。这与我们熟知的在一个关系数据库实例中可以创建多个数据库类似,所以可以将其中的每个字典都理解成一个独立的数据库。

每个数据库对外都是一个从0开始的递增数字命名,Redis默认支持16个数据库(可以通过配置文件支持更多,无上限),可以通过配置databases来修改这一数字。客户端与Redis建立连接后会自动选择0号数据库,不过可以随时使用SELECT命令更换数据库,如要选择1号数据库:

127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get key1
(nil)
127.0.0.1:6379[1]> zrangebyscore asortedset 0 5
(empty list or set)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> zrangebyscore asortedset 0 5
1) "redis"
2) "sqlserver"
3) "db2"
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> zrangebyscore asortedset 0 5
(empty list or set)
127.0.0.1:6379[2]>

其实我真正的目的是为了实现一个最近24小时数据的队列,确保数据是持续滚动的

构建一个list列表键值,通过rpush实现24条记录写入,查看数据,持续lpop和rpush可实现一个队列,现在欠缺的是一个元数据了。

127.0.0.1:6379> del  userdaydata:username:user1:data
(integer) 1
127.0.0.1:6379> rpush userdaydata:username:user1:data 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
(integer) 24
127.0.0.1:6379> lrange userdaydata:username:user1:data 0 23
1) "0"
2) "1"
3) "2"
4) "3"
5) "4"
6) "5"
7) "6"
8) "7"
9) "8"
10) "9"
11) "10"
12) "11"
13) "12"
14) "13"
15) "14"
16) "15"
17) "16"
18) "17"
19) "18"
20) "19"
21) "20"
22) "21"
23) "22"
24) "23"
127.0.0.1:6379> lpop userdaydata:username:user1:data
"0"
127.0.0.1:6379> lrange userdaydata:username:user1:data 0 23
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
10) "10"
11) "11"
12) "12"
13) "13"
14) "14"
15) "15"
16) "16"
17) "17"
18) "18"
19) "19"
20) "20"
21) "21"
22) "22"
23) "23"
127.0.0.1:6379> rpush userdaydata:username:user1:data 0
(integer) 24
127.0.0.1:6379> lrange userdaydata:username:user1:data 0 23
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
10) "10"
11) "11"
12) "12"
13) "13"
14) "14"
15) "15"
16) "16"
17) "17"
18) "18"
19) "19"
20) "20"
21) "21"
22) "22"
23) "23"
24) "0"
127.0.0.1:6379> lpop userdaydata:username:user1:data
"1"
127.0.0.1:6379> lrange userdaydata:username:user1:data 0 23
1) "2"
2) "3"
3) "4"
4) "5"
5) "6"
6) "7"
7) "8"
8) "9"
9) "10"
10) "11"
11) "12"
12) "13"
13) "14"
14) "15"
15) "16"
16) "17"
17) "18"
18) "19"
19) "20"
20) "21"
21) "22"
22) "23"
23) "0"
127.0.0.1:6379> rpush userdaydata:username:user1:data 1
(integer) 24
127.0.0.1:6379> lrange userdaydata:username:user1:data 0 23
1) "2"
2) "3"
3) "4"
4) "5"
5) "6"
6) "7"
7) "8"
8) "9"
9) "10"
10) "11"
11) "12"
12) "13"
13) "14"
14) "15"
15) "16"
16) "17"
17) "18"
18) "19"
19) "20"
20) "21"
21) "22"
22) "23"
23) "0"
24) "1"
127.0.0.1:6379>

后文会将如何通过python进行redis操作。

参考1:https://blog.csdn.net/qq_45047809/article/details/112529734

参考2:https://www.runoob.com/redis/redis-tutorial.html

责任编辑:武晓燕 来源: python与大数据分析
相关推荐

2021-07-27 18:03:59

iOSSwift调度器

2021-10-27 07:15:37

SpringAOP编程(

2021-07-14 08:00:12

Numa架构Linux

2021-11-29 07:24:08

ACID事务大数据

2017-11-15 08:50:59

数据库MySQL

2010-04-01 13:58:16

WinCE 6.0Cashmere

2022-07-20 08:55:10

编辑器VueTiptap

2022-05-13 09:47:28

Docker容器

2022-09-02 12:13:22

TCPUDP场景

2020-11-24 10:13:20

测试开发管理

2020-07-23 07:51:51

Python编程语言工具

2022-10-08 00:00:05

SQL机制结构

2021-08-27 07:06:09

DubboDocker技术

2022-10-18 07:33:57

Maven构建工具

2022-03-31 18:59:43

数据库InnoDBMySQL

2021-01-12 05:08:49

DHCP协议模型

2022-05-24 08:21:16

数据安全API

2017-01-22 15:09:08

架构闭环演进

2023-04-26 07:30:00

promptUI非结构化

2021-04-12 06:08:16

HiveSpark大数据
点赞
收藏

51CTO技术栈公众号