命令查询中文文档 : http://www.redis.cn/commands.html
换库 : select [dbIndex]
一、总述 Key
查看key:
keys [pattern]
不建议使用keys *
查看所有Keys。建议正则匹配, 如keys t*
添加元素:
set [key] [value]
发生冲突则覆盖取得元素:
get [key]
判断key是否存在:
EXISTS [key]
存在返回 integer 1, 否则 0移动key到某个数据库:
move [key] [dbIndex]
设置key过期时间:
expire [key] [seconds]
秒为单位设置key的过期时间, 已过期则移除DB查看key还有多少秒过期:
ttl [key]
-1 表示永不过期, -2 表示已经过期查看key是什么类型:
type [key]
删除key :
del [key]
二、String
添加、获得、删除、长度:
set/get/del/append [key] [str]/strlen [key]
操作对应的value string 对象数字操作自加、自减、加某值、减某值:
incr [key]/decr [key]/incrby [key] [num]/decrby [key] [num]
(v为num生效,否则报错)范围操作:
getrange [key] [start] [end]/setrange [key] [start] [value]
类似substring的范围操作添加过期时间:
Setex [key] [second] [value]
如果不存在设置值:
setnx [key] [value]
省去一步判空合并操作,批量添加、修改、非空添加:
mset [k1] [v1] [k2] [v2]…/mget [k1] [k2].../msetnx [k1] [v1] [k2] [v2]…
先get再set:
getset [key] [value]
三、List
左插值、右插值、选取范围:
lpush [key] [values]/rpush [key] [values]/lrange [key] [start] [end]
0 ~ -1 则为全选左弹出、右弹出:
lpop/rpop [key]
取索引元素:
lindex [key]
list长度:
llen [key]
删除N个值value:
lrem [key] [N] [value]
截取范围值,赋值给key:
ltrim [key] [start] [end]
右出左压:
rpoplpush [key1] [key2]
反之亦然设置某下标值:
lset [key] [index] [value]
插入值 :
linsert [key] after/before [value] [insertValue]
四、Set
插值、得到值、判断是否在集合中:
sadd [key] [values]/smember [key] /sismember [key] [value]
获取集合元素个数:
scard [key]
删除元素:
srem [key] [value]
随机出N个整数:
srandmember [key] [N]
随机出栈:
spop [key]
将key1的某值加入到key2:
smove [key1] [key2] [key1_value]
差集:
sdiff [key1] [key2] [key3]...
key1 - (key2 + key3 + ...)交集:
sinter [key1] [key2] [key3]...
并集 :
sunion [key1] [key2] [key3]...
五、Hash (重要)
key-value关系存在,value 是一个键值对
插值、取值、批量插入、批量取出、得到key下所有kv关系、删除key:
hset [key] [value-k] [value-v]/hget [key] [value-k] /hmset [key] .../hmget [key] .../hgetall [key]/del [key]
长度:
hlen [key]
subkey 存在性:
hexists [key] [subkey]
得到value 的keys 和values:
hkeys [key] / hvals [key]
自增某某值:
hincrby [key] [subkey] [int]/ hincrbyfloat [key] [subkey] [float]
将key1的某值加入到key2:
smove [key1] [key2] [key1_value]
六、Sorted Set
zset :
---key1 :
---------------score1 : v1
-------------- score2 : v2
...
插值、取值、取带score值:
zadd [key] [score1] [value1].../ zrange [key] [start] [end]/zrange [key] [start] [end] withscores
key1 - (key2 + key3 + ...)分数范文取值:
zrangebyscore [key] [score1] [score2]
左闭右闭
zrangebyscore [key] [score1] ( [score2]
左闭右开
zrangebyscore [key] ([score1] ( [score2]
左开右开
zrangebyscore [key] [score1] [score2] limit [num1] [num2]
从score1- score2 结果中的索引num1位置,截取num2个结果
(类似分页操作)
删除 :
zrem [key] [value]
统计个数、统计范围个数:
zcard [key]/ zcount [key] [score1] [score2]
获得下标值:
zrank [key] [value]
获得对应分数:
zscore [key]
得到逆序排名:
zrevrank [key] [startindex] [endindex]
zrevrange 、zrevrangebyscore 同理不再赘述