Redis常用命令与持久化

9 篇文章 0 订阅

字符串类型 String

储存:set key v

获取:get key v

删除:del key

哈希类型 hash(map格式)

储存:hset key field v

获取:hget key field v hgetall key

删除:hdel key field

127.0.0.1:6379> set age 12
OK
127.0.0.1:6379> get age
"12"
127.0.0.1:6379> hset a1 name ajun
(integer) 1
127.0.0.1:6379> hget a1 name
"ajun"
127.0.0.1:6379> hset a1 age 11
(integer) 1
127.0.0.1:6379> hgetall a1
1) "name"
2) "ajun"
3) "age"
4) "11"
127.0.0.1:6379> hdel a1 name
(integer) 1
127.0.0.1:6379> hgetall a1
1) "age"
2) "11"
127.0.0.1:6379>

列表类型 list(linkedlist格式 支持重复元素)

储存:lpush key v 储存到列表左边 rpush key v 储存到列表右边

获取:lrange key start end

删除:lpop key 删除列表左边元素并返回 rpop key 删除列表右边元素并返回

127.0.0.1:6379> lpush ajun a
(integer) 1
127.0.0.1:6379> ipush ajun b
(error) ERR unknown command 'ipush'
127.0.0.1:6379> lpush ajun b
(integer) 2
127.0.0.1:6379> rpush ajun c
(integer) 3
127.0.0.1:6379> lrange ajun 0 -1
1) "b"
2) "a"
3) "c"
127.0.0.1:6379> lpop ajun
"b"
127.0.0.1:6379> rpop ajun
"c"
127.0.0.1:6379>

lpush ajun a ipush ajun b rpush ajun c 顺序结果: b a c

集合类型 set(无序,允许重复元素)

储存:sadd key v

获取:smembers key

删除:srem key v

127.0.0.1:6379> sadd a a
(integer) 1
127.0.0.1:6379> sadd a a
(integer) 0
127.0.0.1:6379> sadd a b c d
(integer) 3
127.0.0.1:6379> smembers a
1) "d"
2) "b"
3) "c"
4) "a"
127.0.0.1:6379> srem a c
(integer) 1
127.0.0.1:6379> smembers a
1) "d"
2) "b"
3) "a"
127.0.0.1:6379>

有序集合类型 sortedset(有序,不允许重复)

储存:zadd key score value

获取:zrange key start end

删除:zrem key value

127.0.0.1:6379> zadd ajun4 60 ajun
(integer) 1
127.0.0.1:6379> zadd ajun4 80 azhu
(integer) 1
127.0.0.1:6379> zadd ajun4 40 zjiao
(integer) 1
127.0.0.1:6379> zrange ajun4
(error) ERR wrong number of arguments for 'zrange' command
127.0.0.1:6379> zrange ajun4 0 -1
1) "zjiao"
2) "ajun"
3) "azhu"
127.0.0.1:6379> zrange ajun4 withscore
(error) ERR wrong number of arguments for 'zrange' command
127.0.0.1:6379> zrange ajun4 0 -1 withscores
1) "zjiao"
2) "40"
3) "ajun"
4) "60"
5) "azhu"
6) "80"
127.0.0.1:6379> zadd ajun4 100 zjiao
(integer) 0
127.0.0.1:6379> zrange ajun4 0 -1 withscores
1) "ajun"
2) "60"
3) "azhu"
4) "80"
5) "zjiao"
6) "100"
127.0.0.1:6379> zrem ajun4 azhu
(integer) 1
127.0.0.1:6379> zrange ajun4 0 -1 withscores
1) "ajun"
2) "60"
3) "zjiao"
4) "100"
127.0.0.1:6379>

持久化

有两种持久化机制

RDB:本身就有的默认配置 打开 redis.windows.conf

#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving at all commenting all the "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save ""

save 900 1
save 300 10
save 60 10000

重启Redis要指定配置文件名称

要在当前命令行输入:D:\redis\day23_redis\windows-64>redis-server.exe redis.windows.conf


AOF:日志记录,记录每一条命令的操作。能在每次操作后,持续化数据 打开 redis.windows.conf

392行 appendonly no 默认关闭 可以开启 appendonly

420行:

appendfsync always 每次操作都持久化

appendfsync everysec 每隔1秒就持久化

appendfsync no 不持久化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值