# set key value [EX seconds] [PX milliseconds] [NX|XX]
127.0.0.1:6379> set name test1
OK
# get key
127.0.0.1:6379> get name
"test1"
# setnx key value
127.0.0.1:6379> setnx name test2
(integer) 0
# setex key seconds value
127.0.0.1:6379> setex test1 10 test1
OK
# setrange key offset value
127.0.0.1:6379> set email 123456789@qq.com
OK
127.0.0.1:6379> setrange email 10 ww
(integer) 16
127.0.0.1:6379> get email
"123456789@ww.com"
# mset key value [key value ...]
127.0.0.1:6379> mset test1 test1 test2 test2 test3 test3
OK
# mget key [key...]
127.0.0.1:6379> mget test1 test2 test3
1) "test1"
2) "test2"
3) "test3"
# getset key value
127.0.0.1:6379> getset test1 test@1
"test1"
127.0.0.1:6379> get test1
"test@1"
127.0.0.1:6379> set index 1
OK
127.0.0.1:6379> incr index
(integer) 2
127.0.0.1:6379> decr index
(integer) 1
127.0.0.1:6379> incrby index 3
(integer) 4
127.0.0.1:6379> decrby index 2
(integer) 2
127.0.0.1:6379> get name
"test1"
# append key value
127.0.0.1:6379> append name test
(integer) 9
127.0.0.1:6379> get name
"test1test"
127.0.0.1:6379> get name
"test1test"
127.0.0.1:6379> strlen name
(integer) 9
# hset key field value
127.0.0.1:6379> hset user id 1
(integer) 1
127.0.0.1:6379> hset user name zhangsan
(integer) 1
127.0.0.1:6379> hset user age 18
(integer) 1
# hget key field
# 注意,取值的时候需要指定hash中的field
127.0.0.1:6379> hget user
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> hget user name
"zhangsan"
127.0.0.1:6379> hget user id
"1"
127.0.0.1:6379> hget user age
"18"
# hmset key field value [field value ...]
127.0.0.1:6379> hmset user1 id 10 name lisi age 18
OK
# 获取时也必须要指定field
127.0.0.1:6379> hmget user1
(error) ERR wrong number of arguments for 'hmget' command
# hmget key field [field ...]
127.0.0.1:6379> hmget user1 id name age
1) "10"
2) "lisi"
3) "18"
127.0.0.1:6379> hmget user id age name
1) "1"
2) "18"
3) "zhangsan"
# user 已经存在id,在设置返回0标示失败
127.0.0.1:6379> hsetnx user id 2
(integer) 0
127.0.0.1:6379> hmget user id age name
1) "1"
2) "18"
3) "zhangsan"
# user 不存在sex,返回1设置成功
127.0.0.1:6379> hsetnx user sex 1
(integer) 1
127.0.0.1:6379> hmget user id age name sex
1) "1"
2) "18"
3) "zhangsan"
4) "1"
# 存在返回1,不存在返回0
127.0.0.1:6379> hexists user id
(integer) 1
127.0.0.1:6379> hexists user score
(integer) 0
# hincrby key field increment
127.0.0.1:6379> hget user id
"2"
127.0.0.1:6379> hincrby user id 1
(integer) 3
127.0.0.1:6379> hget user id
"3"
127.0.0.1:6379> hlen user
(integer) 4
127.0.0.1:6379> hkeys user
1) "id"
2) "name"
3) "age"
4) "sex"
127.0.0.1:6379> hkeys user
1) "id"
2) "name"
3) "age"
4) "sex"
# hdel key field
127.0.0.1:6379> hdel user sex
(integer) 1
127.0.0.1:6379> hkeys user
1) "id"
2) "name"
3) "age"
127.0.0.1:6379> hvals user
1) "3"
2) "zhangsan"
3) "18"
127.0.0.1:6379> hgetall user
1) "id"
2) "3"
3) "name"
4) "zhangsan"
5) "age"
6) "18"
可以使用List实现队列或者栈,可理解为横向数组,取数据从左端取
List 下标从左往右 0,1,2,3…; 从右往左 -1,-2,-3,-4…
# lpush 依次添加到list左端
# lpush key value [value...]
127.0.0.1:6379> lpush list1 hello world
(integer) 2
127.0.0.1:6379> lrange list1 0 -1
1) "world"
2) "hello"
# rpush 在List右端添加元素
# rpush key value [value...]
127.0.0.1:6379> rpush list2 hello world
(integer) 2
127.0.0.1:6379> lrange list2 0 -1
1) "hello"
2) "world"
# linsert key before|after pivot value
127.0.0.1:6379> linsert list1 before 'hello' test
(integer) 3
127.0.0.1:6379> lrange list1 0 -1
1) "world"
2) "test"
3) "hello"
127.0.0.1:6379> lrange list1 0 -1
1) "world"
2) "test"
3) "hello"
127.0.0.1:6379> lset list1 1 test1
OK
127.0.0.1:6379> lrange list1 0 -1
1) "world"
2) "test1"
3) "hello"
count > 0 从左往右 删除count个
count < 0 从右往左 删除count个
count = 0 删除所有匹配
127.0.0.1:6379> lrange list1 0 -1
1) "world"
2) "test2"
3) "world"
127.0.0.1:6379> lrem list1 1 test2
(integer) 1
127.0.0.1:6379> lrange list1 0 -1
1) "world"
2) "world"
127.0.0.1:6379> rpush lists i love java hello world
(integer) 5
127.0.0.1:6379> lrange lists 0 -1
1) "i"
2) "love"
3) "java"
4) "hello"
5) "world"
127.0.0.1:6379> ltrim lists 0 3
OK
127.0.0.1:6379> lrange lists 0 -1
1) "i"
2) "love"
3) "java"
4) "hello"
127.0.0.1:6379> lrange lists 0 -1
1) "i"
2) "love"
3) "java"
4) "hello"
5) "world"
127.0.0.1:6379> lpop lists
"i"
127.0.0.1:6379> lrange lists 0 -1
1) "i"
2) "love"
3) "java"
4) "hello"
5) "world"
127.0.0.1:6379> lpop lists
"i"
127.0.0.1:6379> rpop lists
"world"
127.0.0.1:6379> lrange lists 0 -1
1) "love"
2) "java"
3) "hello"
# rpoplpush source destination
127.0.0.1:6379> rpoplpush lists lists
"hello"
127.0.0.1:6379> lrange lists 0 -1
1) "hello"
2) "love"
3) "java"
127.0.0.1:6379> lrange lists 0 -1
1) "java"
2) "love"
3) "i"
127.0.0.1:6379> lindex lists 0
"java"
127.0.0.1:6379> lindex lists -1
"i"
127.0.0.1:6379> lindex lists -2
"love"
127.0.0.1:6379> lindex lists -3
"java"
127.0.0.1:6379> lrange lists 0 -1
1) "java"
2) "love"
3) "i"
127.0.0.1:6379> llen lists
(integer) 3
set 无序,Zset有序;元素不能重复,可以做集合操作交集、并集、差集
127.0.0.1:6379> sadd set1 i love java
(integer) 3
127.0.0.1:6379> smembers set1
1) "love"
2) "java"
3) "i"
127.0.0.1:6379> smembers set1
1) "love"
2) "java"
3) "i"
127.0.0.1:6379> srem set1 i love
(integer) 2
127.0.0.1:6379> smembers set1
1) "java"
127.0.0.1:6379> SMEMBERS set1
1) "love"
2) "c++"
3) "i"
4) "java"
127.0.0.1:6379> spop set1
"love"
127.0.0.1:6379> smembers set1
1) "c++"
2) "i"
3) "java"
127.0.0.1:6379> smembers set1
1) "c++"
2) "i"
3) "java"
127.0.0.1:6379> smembers set2
1) "python"
2) "love"
3) "c++"
4) "i"
127.0.0.1:6379> sdiff set1 set2
1) "java"
127.0.0.1:6379> sdiff set2 set1
1) "love"
2) "python"
127.0.0.1:6379> smembers set1
1) "c++"
2) "i"
3) "java"
127.0.0.1:6379> smembers set2
1) "python"
2) "love"
3) "c++"
4) "i"
127.0.0.1:6379> sdiffstore set3 set2 set1
(integer) 2
127.0.0.1:6379> smembers set3
1) "love"
2) "python"
sinterstore 取交集并存在新的set中
127.0.0.1:6379> smembers set1
1) "c++"
2) "i"
3) "java"
127.0.0.1:6379> smembers set2
1) "python"
2) "love"
3) "c++"
4) "i"
127.0.0.1:6379> sinter set1 set2
1) "c++"
2) "i"
sunionstore 取并集并存储到新的set中
127.0.0.1:6379> smembers set1
1) "c++"
2) "i"
3) "java"
127.0.0.1:6379> smembers set2
1) "python"
2) "love"
3) "c++"
4) "i"
127.0.0.1:6379> sunion set1 set2
1) "love"
2) "c++"
3) "i"
4) "python"
5) "java"
127.0.0.1:6379> smembers set1
1) "c++"
2) "i"
3) "java"
127.0.0.1:6379> smembers set2
1) "python"
2) "love"
3) "c++"
4) "i"
127.0.0.1:6379> smove set1 set2 java
(integer) 1
127.0.0.1:6379> smembers set1
1) "c++"
2) "i"
127.0.0.1:6379> smembers set2
1) "python"
2) "love"
3) "c++"
4) "i"
5) "java"
127.0.0.1:6379> smembers set1
1) "c++"
2) "i"
127.0.0.1:6379> scard set1
(integer) 2
127.0.0.1:6379> smembers set2
1) "python"
2) "love"
3) "c++"
4) "i"
5) "java"
127.0.0.1:6379> sismember set2 i
(integer) 1
127.0.0.1:6379> sismember set2 c
(integer) 0
127.0.0.1:6379> smembers set2
1) "love"
2) "i"
3) "c++"
4) "python"
5) "java"
127.0.0.1:6379> srandmember set2 2
1) "i"
2) "java"
127.0.0.1:6379> smembers set2
1) "love"
2) "i"
3) "c++"
4) "python"
5) "java"
ZSet 中添加元素:zadd key [NX|XX] [CH] [INCR] score member [score member …]
显示zset中元素:zrange key start stop [WITHSCORES]
127.0.0.1:6379> zadd zset1 1 i 2 love 3 java 4 c++ 5 python
(integer) 5
127.0.0.1:6379> zrange zset1 0 -1
1) "i"
2) "love"
3) "java"
4) "c++"
5) "python"
127.0.0.1:6379> zrange zset 0 -1 withscores
(empty list or set)
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "i"
2) "1"
3) "love"
4) "2"
5) "java"
6) "3"
7) "c++"
8) "4"
9) "python"
10) "5"
127.0.0.1:6379> zrange zset1 0 -1
1) "i"
2) "love"
3) "java"
4) "c++"
5) "python"
127.0.0.1:6379> zrem zset1 i
(integer) 1
127.0.0.1:6379> zrange zset1 0 -1
1) "love"
2) "java"
3) "c++"
4) "python"
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "java"
2) "3"
3) "c++"
4) "4"
5) "python"
6) "5"
7) "love"
8) "8"
127.0.0.1:6379> zincrby zset1 10 love
"18"
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "java"
2) "3"
3) "c++"
4) "4"
5) "python"
6) "5"
7) "love"
8) "18"
127.0.0.1:6379> zincrby zset1 -5 love
"13"
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "java"
2) "3"
3) "c++"
4) "4"
5) "python"
6) "5"
7) "love"
8) "13"
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "java"
2) "3"
3) "c++"
4) "4"
5) "python"
6) "5"
7) "age"
8) "10"
9) "love"
10) "13"
127.0.0.1:6379> zrangebyscore zset1 5 20
1) "python"
2) "age"
3) "love"
127.0.0.1:6379> zadd zset1 1 i 2 love 3 java 4 python 5 c++ 6 c
(integer) 6
127.0.0.1:6379> zrange zset1 0 -1
1) "i"
2) "love"
3) "java"
4) "python"
5) "c++"
6) "c"
127.0.0.1:6379> zremrangebyrank zset1 0 1
(integer) 2
127.0.0.1:6379> zrange zset1 0 -1
1) "java"
2) "python"
3) "c++"
4) "c"
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "java"
2) "3"
3) "python"
4) "4"
5) "c++"
6) "5"
7) "c"
8) "6"
127.0.0.1:6379> zremrangebyscore zset1 3 5
(integer) 3
127.0.0.1:6379> zrange zset1 0 -1
1) "c"
zrevrank key member 按照倒序方式获取索引
127.0.0.1:6379> zrange zset2 0 -1
1) "i"
2) "love"
3) "java"
4) "python"
5) "c++"
6) "c"
127.0.0.1:6379> zrank zset2 python
(integer) 3
127.0.0.1:6379> zrange zset2 0 -1 withscores
1) "i"
2) "1"
3) "love"
4) "2"
5) "java"
6) "3"
7) "python"
8) "4"
9) "c++"
10) "5"
11) "c"
12) "6"
127.0.0.1:6379> zrangebyscore zset2 1 3
1) "i"
2) "love"
3) "java"
127.0.0.1:6379> zrangebyscore zset2 1 3 withscores
1) "i"
2) "1"
3) "love"
4) "2"
5) "java"
6) "3"
127.0.0.1:6379> zrange zset2 0 -1
1) "i"
2) "love"
3) "java"
4) "python"
5) "c++"
6) "c"
127.0.0.1:6379> zcard zset2
(integer) 6
127.0.0.1:6379> zrange zset2 0 -1 withscores
1) "i"
2) "1"
3) "love"
4) "2"
5) "java"
6) "3"
7) "python"
8) "4"
9) "c++"
10) "5"
11) "c"
12) "6"
127.0.0.1:6379> zcount zset2 1 3
(integer) 3
127.0.0.1:6379> zrange zset2 0 -1 withscores
1) "i"
2) "1"
3) "love"
4) "2"
5) "java"
6) "3"
7) "python"
8) "4"
9) "c++"
10) "5"
11) "c"
12) "6"
127.0.0.1:6379> zremrangebyrank zset2 0 2
(integer) 3
127.0.0.1:6379> zrange zset2 0 -1 withscores
1) "python"
2) "4"
3) "c++"
4) "5"
5) "c"
6) "6"