String
# 新增
set key value
# 根据key获得value
get key
# 删除
del key
# 查询所有keys
key *
# 更新
getset key new_value
# 递增,value必须为整形
incr num
# 递减
decr num
127.0.0.1:6379> set name 'xuxiufen'
OK
127.0.0.1:6379> set text 'asasasasasasdsd'
OK
127.0.0.1:6379> get name
"xuxiufen"
127.0.0.1:6379> get text
"asasasasasasdsd"
127.0.0.1:6379> kyes
(error) ERR unknown command `kyes`, with args beginning with:
127.0.0.1:6379> keys
(error) ERR wrong number of arguments for 'keys' command
127.0.0.1:6379> keys;
(error) ERR unknown command `keys;`, with args beginning with:
127.0.0.1:6379> keys *
1) "name"
2) "text"
127.0.0.1:6379> set num 1
OK
127.0.0.1:6379> get num
"1"
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> decr num
(integer) 1
# 指定递增步数
127.0.0.1:6379> incrby num 6
(integer) 8
# 指定递减步数
127.0.0.1:6379> decrby num 2
(integer) 6
# 拼接 append key plus_value
127.0.0.1:6379> get num
"6"
127.0.0.1:6379> append num 1
(integer) 2
127.0.0.1:6379> get num
"61"
# 若没有对应的key,则创建key:value
Hash
# 创建一组hash键值对 key filed value
127.0.0.1:6379> hset myhash username jack
(integer) 1
127.0.0.1:6379> hset myhash age 28
(integer) 1
# 创建多组
127.0.0.1:6379> hmset myhash2 username alex age 30
OK
# 查询
127.0.0.1:6379> hget myhash username
"jack"
127.0.0.1:6379> hget myhash age
"28"
127.0.0.1:6379> hmget myhash2 username age
1) "alex"
2) "30"
127.0.0.1:6379> hgetall myhash
1) "username"
2) "jack"
3) "age"
4) "28"
# 删除kye内部的一些
127.0.0.1:6379> hdel myhash2 username age
(integer) 2
127.0.0.1:6379> hgetall myhash2
(empty list or set)
# 更新特定指定的val
127.0.0.1:6379> hincrby myhash age 12
(integer) 40
127.0.0.1:6379> hget myhash age
"40"
# 判断字段是否存在
127.0.0.1:6379> hexists myhash username
(integer) 1
# 获取字段个数
127.0.0.1:6379> hlen myhash
(integer) 2
# 获取所有key
127.0.0.1:6379> hkeys myhash
1) "username"
2) "age"
List
# 从左侧依次插入值,没有列表则创建
lpush lst1 1 2 3 a b c
# 从右侧依次插入值
rpush lst2 1 2 3 a b c
# 查看列表,范围:start end
127.0.0.1:6379> lrange lst1 0 -1
1) "c"
2) "b"
3) "a"
4) "3"
5) "2"
6) "1"
127.0.0.1:6379> lrange lst2 0 -1
1) "1"
2) "2"
3) "3"
4) "a"
5) "b"
6) "c"
# 弹出并显示值
127.0.0.1:6379> lpop lst1
"c"
127.0.0.1:6379> rpop lst1
"1"
# 从列表的左侧或右侧 移除 指定个数 的val
127.0.0.1:6379> rpush lst4 4 a b a 1 3 1 4 a 4
(integer) 10
127.0.0.1:6379> lrem lst4 2 a # 从左往右遍历移除 2 个字符a
(integer) 2
127.0.0.1:6379> lrange lst4 0 -1
1) "4"
2) "b"
3) "1"
4) "3"
5) "1"
6) "4"
7) "a"
8) "4"
127.0.0.1:6379> lrem lst4 -2 4 # 从右往左开始遍历 删除 2个字符4
(integer) 2
127.0.0.1:6379> lrange lst4 0 -1
1) "4"
2) "b"
3) "1"
4) "3"
5) "1"
6) "a"
127.0.0.1:6379> lrem lst4 0 1 # 删除所有 字符-1
(integer) 2
127.0.0.1:6379> lrange lst4 0 -1
1) "4"
2) "b"
3) "3"
4) "a"
# 修改指定索引的val
127.0.0.1:6379> lrange lst4 0 -1
1) "4"
2) "44"
3) "3"
4) "a"
# 向 指定列表 的 某元素 前面 或者 后面 插入值
127.0.0.1:6379> linsert lst4 before 4 11
(integer) 5
127.0.0.1:6379> lrange lst4 0 -1
1) "11"
2) "4"
3) "44"
4) "3"
5) "a"
127.0.0.1:6379> linsert lst4 after 4 33
(integer) 6
127.0.0.1:6379> lrange lst4 0 -1
1) "11"
2) "4"
3) "33"
4) "44"
5) "3"
6) "a"
# 从lst1右侧弹出第一个值,再将此值从lst3左侧插入
rpoplpush lst1 lst2
Set
- 和list类型不同的是,不允许出重复元素。
- Set包含的最大元素数量是2**32。
# 创建集合
127.0.0.1:6379> sadd set1 a b c
(integer) 3
127.0.0.1:6379> sadd set1 a # 不能添加重复元素
(integer) 0
127.0.0.1:6379> sadd set1 1 2 3
(integer) 3
# 从set中移除指定的元素
127.0.0.1:6379> srem set1 1 2
(integer) 2
# 查看集合的所有元素
127.0.0.1:6379> smembers set1
1) "a"
2) "b"
3) "c"
4) "3"
# 判断元素是否在set中,存在则返回1,不存在则返回0
127.0.0.1:6379> sismember set1 a
(integer) 1
127.0.0.1:6379> sismember set1 aa
(integer) 0
# 集合运算
127.0.0.1:6379> sadd s1 a b c
(integer) 3
127.0.0.1:6379> sadd s2 a c 1 2
(integer) 4
# 差集
127.0.0.1:6379> sdiff s1 s2
1) "b"
# 交集
127.0.0.1:6379> sinter s1 s2
1) "c"
2) "a"
# 并集
127.0.0.1:6379> sunion s1 s2
1) "a"
2) "2"
3) "b"
4) "c"
# 集合长度
127.0.0.1:6379> scard s1
(integer) 3
扩展应用:
# 弹出随机的元素
127.0.0.1:6379> srandmember s1
"a"
# 将s1和s2 差集 的元素放进集合s3
127.0.0.1:6379> sdiffstore s3 s1 s2
(integer) 1
127.0.0.1:6379> smembers s3
1) "b"
# 将s1和s2 交集 的元素放进集合s4
127.0.0.1:6379> sinterstore s4 s1 s2
(integer) 2
127.0.0.1:6379> smembers s4
1) "a"
2) "c"
# 将s1和s2的 并集 存放进集合s5
127.0.0.1:6379> sunionstore s5 s1 s2
(integer) 5
127.0.0.1:6379> smembers s5
1) "a"
2) "2"
3) "b"
4) "c"
5) "1"
sorted-set
# 增加
127.0.0.1:6379> zadd mysort 70 zs 88 lisi 68 wangwu
(integer) 3
127.0.0.1:6379> zadd mysort 100 zs # 若存在,则更新对应的score
(integer) 0
127.0.0.1:6379> zadd mysort 90 kate
(integer) 1
# 查询
127.0.0.1:6379> zscore mysort zs # 查询某个member对应的key
"100"
# 获取集合长度
127.0.0.1:6379> zcard mysort
(integer) 4
# 按索引查询整个集合:默认从小到大
127.0.0.1:6379> zrange mysort 0 -1
1) "wangwu"
2) "jack"
3) "lisi"
4) "kate"
5) "rose"
127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "wangwu"
2) "68"
3) "jack"
4) "85"
5) "lisi"
6) "88"
7) "kate"
8) "90"
9) "rose"
10) "95"
# 降序查询集合
127.0.0.1:6379> zrevrange mysort 0 -1 withscores
1) "rose"
2) "95"
3) "kate"
4) "90"
5) "lisi"
6) "88"
7) "jack"
8) "85"
9) "wangwu"
10) "68"
# 删除指定的member
127.0.0.1:6379> zrem mysort zs
(integer) 1
# 按照索引删除member,包括两端
127.0.0.1:6379> zremrangebyrank mysort 0 2
(integer) 3
# 按照score的范围删除member
127.0.0.1:6379> zremrangebyscore mysort 80 100
(integer) 3
# 增加member对应的score
127.0.0.1:6379> zincrby mysort 12 bb
"79"
# 统计指定score范围内的member个数
127.0.0.1:6379> zcount mysort 60 80
(integer) 1