发布时间:2022-11-26 文章分类:编程知识 投稿人:赵颖 字号: 默认 | | 超大 打印

redis基本数据结构操作:

0.基本的常用key操作和数据库操作:

keys *查看当前库所有key (匹配:keys *1)

127.0.0.1:6379> set username qiuge
OK
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> keys *
1) "age"
2) "username"

exists key判断某个key是否存在

127.0.0.1:6379> keys *
1) "age"
2) "username"
127.0.0.1:6379> exists username
(integer) 1
127.0.0.1:6379> exists gender
(integer) 0

type key 查看你的key是什么类型

127.0.0.1:6379> type username
string
127.0.0.1:6379> type age
string

del key 删除指定的key数据

127.0.0.1:6379> keys *
1) "age"
2) "username"
127.0.0.1:6379> del username
(integer) 1
127.0.0.1:6379> keys *
1) "age"

unlink key 根据value选择非阻塞删除:仅将keys从keyspace元数据中删除,真正的删除会在后续异步操作。

127.0.0.1:6379> keys *
1) "age"
127.0.0.1:6379> unlink age
(integer) 1
127.0.0.1:6379> keys *
(empty array)

expire key 10 10秒钟:为给定的key设置过期时间

ttl key 查看还有多少秒过期,-1表示永不过期,-2表示已过期

127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> set username qiuge
OK
127.0.0.1:6379> ttl username
(integer) -1
127.0.0.1:6379> expire username 10
(integer) 1
127.0.0.1:6379> ttl username
(integer) 6
127.0.0.1:6379> ttl username
(integer) 1
127.0.0.1:6379> ttl username
(integer) -2
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> set username qiuge
OK
127.0.0.1:6379> keys *
1) "username"
127.0.0.1:6379> expire username -1
(integer) 1
127.0.0.1:6379> ttl username
(integer) -2
127.0.0.1:6379> keys *
(empty array)

注意:如果设置为后台启动,数据结构创建的时候默认是永久的,也就是说只要设置了,哪怕中途重启了服务器也还是存在的,使用expire设置时间是秒数,设置时间为-1并不是设置为永久,而是一设置时间过期。

select命令切换数据库

127.0.0.1:6379> flushall
OK
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> select 0
OK
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> set username qiuge
OK
127.0.0.1:6379> keys *
1) "username"
127.0.0.1:6379> select 5
OK
127.0.0.1:6379[5]> keys *
(empty array)
127.0.0.1:6379[5]> set age 19
OK
127.0.0.1:6379[5]> keys *
1) "age"

dbsize查看当前数据库的key的数量

127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> dbsize
(integer) 0
127.0.0.1:6379> set username qiuge
OK
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> keys *
1) "username"
2) "age"
127.0.0.1:6379> dbsize
(integer) 2

flushdb清空当前库

127.0.0.1:6379> select 0
OK
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379> set username qiuge
OK
127.0.0.1:6379> keys *
1) "username"
127.0.0.1:6379> select 5
OK
127.0.0.1:6379[5]> keys *
(empty array)
127.0.0.1:6379[5]> set age 19
OK
127.0.0.1:6379[5]> keys *
1) "age"
127.0.0.1:6379[5]> flushdb
OK
127.0.0.1:6379[5]> keys *
(empty array)
127.0.0.1:6379[5]> select 0
OK
127.0.0.1:6379> keys *
1) "username"

flushall通杀全部库

127.0.0.1:6379> select 0
OK
127.0.0.1:6379> keys *
1) "username"
127.0.0.1:6379> select 5
OK
127.0.0.1:6379[5]> keys *
(empty array)
127.0.0.1:6379[5]> set username wangge
OK
127.0.0.1:6379[5]> keys *
1) "username"
127.0.0.1:6379[5]> flushall
OK
127.0.0.1:6379[5]> keys *
(empty array)
127.0.0.1:6379[5]> select 0
OK
127.0.0.1:6379> keys *
(empty array)