SET:添加或者修改已经存在的一个String类型的键值对
127.0.0.1:6379> help set
SET key value [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|KEEPTTL] [NX|XX] [GET]
summary: Set the string value of a key
since: 1.0.0
group: string
127.0.0.1:6379>
示例:
添加一个key为name,value为张三的键值对
127.0.0.1:6379> set name zhangsan
OK
127.0.0.1:6379> get name
"zhangsan"
127.0.0.1:6379>
修改name的值为lisi
127.0.0.1:6379> set name lisi
OK
127.0.0.1:6379> get name
"lisi"
127.0.0.1:6379>
GET:根据key获取String类型的value
127.0.0.1:6379> help get
GET key
summary: Get the value of a key
since: 1.0.0
group: string
127.0.0.1:6379>
示例:
查看name的值
127.0.0.1:6379> get name
"lisi"
127.0.0.1:6379>
注意:如果获取的key不存在,则会返回(nil),也就是空的意思
MSET:批量添加多个String类型的键值对
127.0.0.1:6379> help mset
MSET key value [key value ...]
summary: Set multiple keys to multiple values
since: 1.0.1
group: string
127.0.0.1:6379>
示例:
创建a1 01,a2 02,a3 03 三个键值对
127.0.0.1:6379> mset a1 01 a2 02 a3 03
OK
127.0.0.1:6379> mget a1 a2 a3
1) "01"
2) "02"
3) "03"
127.0.0.1:6379>
修改a1 ,a2 ,a3的值为001,002,003
127.0.0.1:6379> mset a1 001 a2 002 a3 003
OK
127.0.0.1:6379> mget a1 a2 a3
1) "001"
2) "002"
3) "003"
127.0.0.1:6379>
MGET:根据多个key获取多个String类型的value
127.0.0.1:6379> help mget
MGET key [key ...]
summary: Get the values of all the given keys
since: 1.0.0
group: string
127.0.0.1:6379>
示例:
获取a1,a2,a3的值
127.0.0.1:6379> mget a1 a2 a3
1) "001"
2) "002"
3) "003"
127.0.0.1:6379>
INCR:让一个整型的key自增1
127.0.0.1:6379> help INCR
INCR key
summary: Increment the integer value of a key by one
since: 1.0.0
group: string
127.0.0.1:6379>
示例:
让age从15自增到18
127.0.0.1:6379> set age 15
OK
127.0.0.1:6379> incr age
(integer) 16
127.0.0.1:6379> incr age
(integer) 17
127.0.0.1:6379> incr age
(integer) 18
127.0.0.1:6379>
INCRBY:让一个整型的key自增并指定步长
127.0.0.1:6379> help INCRBY
INCRBY key increment
summary: Increment the integer value of a key by the given amount
since: 1.0.0
group: string
127.0.0.1:6379>
示例:
让age从18自增到15
127.0.0.1:6379> get age
"18"
127.0.0.1:6379> INCRBY age -3
(integer) 15
127.0.0.1:6379>
让age从15自增到18
127.0.0.1:6379> get age
"15"
127.0.0.1:6379> INCRBY age 3
(integer) 18
127.0.0.1:6379>
INCRBYFLOAT:让一个浮点类型的数字自增并指定步长
127.0.0.1:6379> help INCRBYFLOAT
INCRBYFLOAT key increment
summary: Increment the float value of a key by the given amount
since: 2.6.0
group: string
127.0.0.1:6379>
示例:
让num从5.5自增到9.5
127.0.0.1:6379> set num 5.5
OK
127.0.0.1:6379> INCRBYFLOAT num 4
"9.5"
127.0.0.1:6379>
让num从9.5自增到10
127.0.0.1:6379> get num
"9.5"
127.0.0.1:6379> INCRBYFLOAT num 0.5
"10"
127.0.0.1:6379>
SETNX:添加一个String类型的键值对,前提是这个key不存在,否则不执行
注意:如果key存在则不添加,不存在则添加
127.0.0.1:6379> help SETNX
SETNX key value
summary: Set the value of a key, only if the key does not exist
since: 1.0.0
group: string
127.0.0.1:6379>
示例:
存在key name,不存在name1,分别分别进行添加(key存在则不添加,不存在则添加)
127.0.0.1:6379> get name
"lisi"
127.0.0.1:6379> get name1
(nil)
127.0.0.1:6379> SETNX name zhangsan
(integer) 0
127.0.0.1:6379> SETNX name1 zhangsan
(integer) 1
127.0.0.1:6379>
注意:(nil)代表空,0代表添加失败,1代表成功添加一条数据
SETEX:添加一个String类型的键值对,并且指定有效期(有效期的单位是秒)
127.0.0.1:6379> help SETEX
SETEX key seconds value
summary: Set the value and expiration of a key
since: 2.0.0
group: string
127.0.0.1:6379>
示例:
创建一个s1 wangwu键值对,设置有效期为10秒
127.0.0.1:6379> SETEX s1 10 wangwu
OK
127.0.0.1:6379> get s1
"wangwu"
127.0.0.1:6379> ttl s1
(integer) 3
127.0.0.1:6379> ttl s1
(integer) 1
127.0.0.1:6379> ttl s1
(integer) 0
127.0.0.1:6379> ttl s1
(integer) -2
127.0.0.1:6379> get s1
(nil)
127.0.0.1:6379>