0%

redis数据类型

linux下 redis安装

参考: https://www.redis.net.cn/download/

下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost redis]# 
[root@localhost redis]# wget http://download.redis.io/releases/redis-5.0.4.tar.gz
--2021-01-29 02:56:57-- http://download.redis.io/releases/redis-5.0.4.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1966337 (1.9M) [application/octet-stream]
Saving to: ‘redis-5.0.4.tar.gz’

100%[=========================================================================================================================================>] 1,966,337 55.7KB/s in 39s

2021-01-29 02:57:36 (49.2 KB/s) - ‘redis-5.0.4.tar.gz’ saved [1966337/1966337]

[root@localhost redis]#
[root@localhost redis]# ls
redis-5.0.4.tar.gz
[root@localhost redis]#

解压

1
2
3
4
5
6
[root@localhost redis]# tar xzf redis-5.0.4.tar.gz -C /usr/redis/
[root@localhost redis]# cd /usr/redis/
[root@localhost redis]# ls
redis-5.0.4
[root@localhost redis]#

编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost redis-5.0.4]# yum install gcc-c++
[root@localhost redis-5.0.4]# make
............
INSTALL redis-sentinel
CC redis-cli.o
LINK redis-cli
CC redis-benchmark.o
LINK redis-benchmark
INSTALL redis-check-rdb
INSTALL redis-check-aof

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/usr/redis/redis-5.0.4/src'
[root@localhost redis-5.0.4]#

启动服务(默认)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@localhost redis-5.0.4]# src/redis-server 
2189:C 30 Jan 2021 01:49:16.869 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2189:C 30 Jan 2021 01:49:16.869 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=2189, just started
2189:C 30 Jan 2021 01:49:16.869 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
2189:M 30 Jan 2021 01:49:16.871 * Increased maximum number of open files to 10032 (it was originally set to 1024).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.4 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 2189
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

2189:M 30 Jan 2021 01:49:16.873 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2189:M 30 Jan 2021 01:49:16.874 # Server initialized
2189:M 30 Jan 2021 01:49:16.874 # 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.
2189:M 30 Jan 2021 01:49:16.874 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
2189:M 30 Jan 2021 01:49:16.903 * DB loaded from disk: 0.029 seconds
2189:M 30 Jan 2021 01:49:16.903 * Ready to accept connections

启动客户端

1
2
3
4
5
6
7
[root@localhost redis-5.0.4]# src/redis-cli 
127.0.0.1:6379> set name yangl
OK
127.0.0.1:6379> get name
"yangl"
127.0.0.1:6379>

性能测试

序号 选项 描述 默认值
1 -h 指定服务器主机名 127.0.0.1
2 -p 指定服务器端口 6379
3 -s 指定服务器 socket
4 -c 指定并发连接数 50
5 -n 指定请求数 10000
6 -d 以字节的形式指定 SET/GET 值的数据大小 2
7 -k 1=keep alive 0=reconnect 1
8 -r SET/GET/INCR 使用随机 key, SADD 使用随机值
9 -P 通过管道传输 请求 1
10 -q 强制退出 redis。仅显示 query/sec 值
11 –csv 以 CSV 格式输出
12 -l 生成循环,永久执行测试
13 -t 仅运行以逗号分隔的测试命令列表。
14 -I Idle 模式。仅打开 N 个 idle 连接并等待。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost redis-5.0.4]# src/redis-benchmark -h localhost -p 6379 -c 100 -n 100000
====== PING_INLINE ======
100000 requests completed in 1.37 seconds # 对10W个请求进行写入测试
100 parallel clients # 100个并发客户管
3 bytes payload # 每次写入3个字节
keep alive: 1 # 只有一台服务器来处理这个请求,单机性能

88.95% <= 1 milliseconds
97.47% <= 2 milliseconds
99.54% <= 3 milliseconds
99.88% <= 4 milliseconds
99.99% <= 5 milliseconds
100.00% <= 5 milliseconds # 所有请求在 5 毫秒处理完成
72992.70 requests per second # 每秒处理 72992.70 次请求

1611908215889

key 操作

在redis中无论什么数据类型,在数据库中都是以key-value形式保存,通过进行对Redis-key的操作,来完成对数据库中数据的操作。

keys * :查看当前数据库中所有的key。

flushdb:清空当前数据库中的键值对。

flushall:清空所有数据库的键值对。

下面学习的命令:

  • exists key:判断键是否存在
  • del key:删除键值对
  • move key db:将键值对移动到指定数据库
  • expire key second:设置键值对的过期时间
  • type key:查看value的数据类型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
[root@localhost redis-5.0.4]# src/redis-cli
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> keys * # 查看当前数据库中所有的key
1) "foo"
2) "key:__rand_int__"
3) "mylist"
4) "counter:__rand_int__"
127.0.0.1:6379>
127.0.0.1:6379> DBSIZE # 查看当前数据库的大小,与key数量相关
(integer) 4
127.0.0.1:6379> flushdb # 清空当前数据库中的键值对。
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set name yangl
OK
127.0.0.1:6379> type name # 查看 name 的数据类型
string
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> type age
string
127.0.0.1:6379> keys *
1) "age"
2) "name"
127.0.0.1:6379> move age 1 # 将键值对移动到指定数据库
(integer) 1
127.0.0.1:6379>
127.0.0.1:6379> EXISTS age # 判断键是否存在
(integer) 0 # 不存在
127.0.0.1:6379> EXISTS name
(integer) 1 # 存在
127.0.0.1:6379>
127.0.0.1:6379> SELECT 1 # 切换数据库 DB 1
OK
127.0.0.1:6379[1]>
127.0.0.1:6379[1]> keys *
1) "age"
127.0.0.1:6379[1]> del age # 删除键值对
(integer) 1
127.0.0.1:6379[1]> set age 20
OK
127.0.0.1:6379[1]> EXPIRE age 10 # 设置键值对的过期时间
(integer) 1
127.0.0.1:6379[1]> ttl age # 查看key的过期剩余时间
(integer) 8
127.0.0.1:6379[1]> ttl age
(integer) 4
127.0.0.1:6379[1]> ttl age
(integer) -2 # -2 表示key过期,-1表示key未设置过期时间
127.0.0.1:6379[1]> get age # 过期的key 会被自动delete
(nil)
127.0.0.1:6379[1]>
127.0.0.1:6379[1]>

更多命令参考:https://www.redis.net.cn/tutorial/3507.html

5种最基本的数据类型

Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库高速缓存消息队列代理。它支持字符串哈希表列表集合有序集合位图hyperloglogs等数据类型。内置复制、Lua脚本、LRU收回、事务以及不同级别磁盘持久化功能,同时通过Redis Sentinel提供高可用,通过Redis Cluster提供自动分区

String

List

Hash

Set

Sorted Set

3种高级数据类型

bitmap

GeoHash

HyperLogLog

5.0最新版本数据类型

Streams

参考:

https://www.redis.net.cn/download/


----------- 本文结束啦感谢您阅读 -----------