设置键值:SET key value [EX seconds] [PX milliseconds] [NX|XX]
SETNX: 如果键存在则不设置其值
127.0.0.1:6379> set k2 v22
OK
127.0.0.1:6379> get k2
"v22"
127.0.0.1:6379> set k2 v2 NX //k2存在,返回错误
(nil)
127.0.0.1:6379> set k2 v222 xx //k2存在,修改
OK
127.0.0.1:6379> get k2
"v222"
MSET: 批量设置键值
获取键值:GET key
MGET: 批量获取键值
127.0.0.1:6379> mset date 2020 time 0402
OK
127.0.0.1:6379> get date
"2020"
127.0.0.1:6379> get time
"0402"
获取键值的生存时间:TTL
127.0.0.1:6379> set da time EX 10
OK
127.0.0.1:6379> get da
"time"
127.0.0.1:6379> get da
"time"
127.0.0.1:6379> get da
"time"
127.0.0.1:6379> get da
(nil)
为键对应的数值进行加减操作:
incr key : +1
decr key : -1
incrby key N : +N
decrby key N : -N
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> incre age
(error) ERR unknown command 'incre'
127.0.0.1:6379> incr age
(integer) 19
Hash 一张表
设置: HSET key field value
127.0.0.1:6379> hset xiaoming age 18
(integer) 1
127.0.0.1:6379> hset xiaoming address zj
(integer) 1
127.0.0.1:6379> hset xiaoming school jd
(integer) 1
获取: HSET key field
获取键的所有字段值: HGETALL key
127.0.0.1:6379> hget xiaoming
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> hgetall xiaoming
1) "age"
2) "18"
3) "address"
4) "zj"
5) "school"
6) "jd"
获取键的所有字段:HKEYS
127.0.0.1:6379> hkeys xiaoming
1) "age"
2) "address"
3) "school"
获取键的所有字段长度:HLEN
127.0.0.1:6379> hlen xiaoming
(integer) 3
删除: HDEL key field
127.0.0.1:6379> hdel xiaoming school
(integer) 1
127.0.0.1:6379> hgetall xiaoming
1) "age"
2) "18"
3) "address"
4) "zj"
判断给定字段是否在所对应的键中:在返回1;不在返回0
HEXISTS key field
127.0.0.1:6379> hexists xiaoming age
(integer) 1