Redis命令
2020-04-14 本文已影响0人
世外大帝
string 字符串
# 添加
127.0.0.1:6379> set username zhangsan
OK
127.0.0.1:6379> get username
"zhangsan"
# 修改
127.0.0.1:6379> set username zhangsanfeng
OK
127.0.0.1:6379> get username
"zhangsanfeng"
# 删除
127.0.0.1:6379> del username1
(integer) 1
127.0.0.1:6379> get username1
(nil)
hash 哈希
# 添加
127.0.0.1:6379> hset myhash username lisi
(integer) 1
127.0.0.1:6379> hset myhash pass 123
(integer) 1
# 查询
127.0.0.1:6379> hget myhash username
"lisi"
127.0.0.1:6379> hget myhash pass
"123"
# 查询所有
127.0.0.1:6379> hgetall myhash
1) "username"
2) "lisi"
3) "pass"
4) "123"
# 删除
127.0.0.1:6379> hdel myhash username
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "pass"
2) "123"
list 列表
# 添加
127.0.0.1:6379> lpush mylist zhangsan # 加入左侧列表
(integer) 1
127.0.0.1:6379> lpush mylist zhangsanfeng # 加入左侧列表
(integer) 2
127.0.0.1:6379> rpush mylist zhangwuji # 加入右侧列表
(integer) 3
127.0.0.1:6379> rpush mylist zhangcuishan # 加入右侧列表
(integer) 4
------加入顺序-----
zhangsan # 左侧加入
zhangsanfeng zhangsan # 左侧加入
zhangsanfeng zhangsan zhangwuji # 右侧加入
zhangsanfeng zhangsan zhangwuji zhangcuishan # 右侧加入
# 查找
127.0.0.1:6379> lrange mylist 0 -1 # 左侧查找
1) "zhangsanfeng"
2) "zhangsan"
3) "zhangwuji"
4) "zhangcuishan"
# 删除
127.0.0.1:6379> lpop mylist # 左侧弹出
"zhangsanfeng"
127.0.0.1:6379> rpop mylist # 右侧弹出
"zhangcuishan"
127.0.0.1:6379> lrange mylist 0 -1 # 再次查询
1) "zhangsan"
2) "zhangwuji"
set 不重复集合
# 添加
127.0.0.1:6379> sadd myset a
(integer) 1
127.0.0.1:6379> sadd myset b
(integer) 1
127.0.0.1:6379> sadd myset c
(integer) 1
127.0.0.1:6379> sadd myset a # set不允许重复
(integer) 0
# 查找
127.0.0.1:6379> smembers myset
1) "c"
2) "b"
3) "a"
# 删除
127.0.0.1:6379> srem myset a
(integer) 1
127.0.0.1:6379> smembers myset
1) "c"
2) "b"
sortedset 有序集合
# 添加
127.0.0.1:6379> zadd mysort 60 zhangsan
(integer) 1
127.0.0.1:6379> zadd mysort 61 lisi
(integer) 1
127.0.0.1:6379> zadd mysort 62 wangwu
(integer) 1
# 查找
127.0.0.1:6379> zrange mysort 0 -1
1) "zhangsan"
2) "lisi"
3) "wangwu"
127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "zhangsan"
2) "60"
3) "lisi"
4) "61"
5) "wangwu"
6) "62"
# 修改
127.0.0.1:6379> zadd mysort 100 lisi
(integer) 0
127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "zhangsan"
2) "60"
3) "wangwu"
4) "62"
5) "lisi"
6) "100"
# 删除
127.0.0.1:6379> zrem mysort wangwu
(integer) 1
127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "zhangsan"
2) "60"
3) "lisi"
4) "100"
通用命令
# 查询所有键
127.0.0.1:6379> keys *
1) "mysort"
2) "username"
3) "mylist"
4) "myset"
5) "myhash"
# 查询键类型
127.0.0.1:6379> type mylist
list
127.0.0.1:6379> type mysort
zset
# 删除指定的键
127.0.0.1:6379> del mysort
(integer) 1
127.0.0.1:6379> keys *
1) "username"
2) "mylist"
3) "myset"
4) "myhash"