2.redis数据操作
参考文档:http://redis.cn/commands.html#list
string
127.0.0.1:6379> set key value [EX seconds|PX milliseconds] [NX|XX] [KEEPTTL]
ex 5 # key 存活时间,单位是秒
px 5000 # key存活时间,单位是毫秒
# nx 如果key不存在则新建,key存在则返回none
127.0.0.1:6379> set age 18 nx
OK
127.0.0.1:6379> set age 18 nx
(nil)
# xx 如果key不存在,则返回None,key存在才可以操作
127.0.0.1:6379> set gender man xx
(nil)
127.0.0.1:6379> set gender man
OK
127.0.0.1:6379> set gender man xx
OK
# mset 同时设置多个key,会覆盖已经存在的key
# mget 批量获取多个key,若果key不存在这返回nil
#getset age 18
返回key原来的值,并设置新的值
# strlen 获取所获取的key value的长度
127.0.0.1:6379> STRLEN age
(integer) 2
# append 向key添加值,并返回长度,如果key不存在则会创建key
127.0.0.1:6379> APPEND age sui
(integer) 5
127.0.0.1:6379> get age
"18sui"
# INCR 自增,incr一次则自增1(只能是数字)
127.0.0.1:6379> incr num
(integer) 1
127.0.0.1:6379> get num
"1"
127.0.0.1:6379> incr num
(integer) 2
# DECR 自减,incr一次则自减1(只能是数字)
#INCRBY 自增 对 key 的value 自定义增加值
127.0.0.1:6379> INCRBY num 5
(integer) 7
127.0.0.1:6379> get num
"7"
# DECRBY 自减,并指定自减的值
127.0.0.1:6379> DECRBY num 5
(integer) 2
127.0.0.1:6379> get num
"2"
#GETRANGE 切片,和python类似,不能使用步长
127.0.0.1:6379> set name makuo
OK
127.0.0.1:6379> GETRANGE name 0 -1 #顾头也顾尾
"makuo"
127.0.0.1:6379> GETRANGE name -3 -1
"kuo"
#INCRBYFLOAT 自增浮点数
127.0.0.1:6379> INCRBYFLOAT num 1.2
"3.2"
127.0.0.1:6379> INCRBYFLOAT num -1.2 # 也可以通过加负数实现减的效果
"2"
list
# 将一个或多个 value 插入到列表的 头部
127.0.0.1:6379> LPUSH l1 a b c d e
(integer) 5
将一个或多个 value 插入到列表的 尾部
127.0.0.1:6379> RPUSH l1 ma
(integer) 5
# 查看列表指定的元素
127.0.0.1:6379> LRANGE l1 0 -1 #顾头也顾尾
1) "e"
2) "d"
3) "c"
4) "b"
5) "a"
# lopop 删除列表的第一个元素
127.0.0.1:6379> LPOP l1
"e"
# lopop 删除列表的最后一个元素
127.0.0.1:6379> RPOP l1
"ma"
# 获取key指定下标的值
127.0.0.1:6379> LINDEX l1 1
"c"
#指定插入位置
127.0.0.1:6379> LRANGE l1 0 -1
1) "d"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> LINSERT l1 before d the_one
(integer) 5
127.0.0.1:6379> LINSERT l1 after d the_three
(integer) 6
127.0.0.1:6379> LRANGE l1 0 -1
1) "the_one"
2) "d"
3) "the_three"
4) "c"
5) "b"
6) "a"
# llen 获取列表内元素的个数
127.0.0.1:6379> LLEN l1
(integer) 6
# lrem
count >0 从表头往表尾的方向查找,删除指定的个数
count =0 全部删除
count <0 从表尾位置往表头方式查找,删除指定的个数
127.0.0.1:6379> LRANGE l1 0 -1
1) "the_one"
2) "d"
3) "the_three"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> LREM l1 1 d
(integer) 1
127.0.0.1:6379> lrange l1 0 -1
1) "the_one"
2) "the_three"
3) "c"
4) "b"
5) "a"