Common
keys *
keys n*e
keys n?
del my1 my2 my3
exists my1
rename a b
expire a 1000
ttl a
type a
String
set key value [EX seconds] [PX milliseconds] [NX(不存在才成功)|XX(已存在才成功)]
eg: set name imooc EX 10 NX
set name imooc
mset name imooc age 10
get name
mget name1 name2
getset name imooc
del name
incr a
decr a
incrby a 100
decrby a 100
append name zhangsan
setnx name imooc (如果存在key,则失败,返回0)
msetnx name imooc age 10 (有一个失败,整个都失败)
setex name 10 imooc (设置失效时间)
getrange name 0 5 (截取字符,从索引0到索引5)
Hash
hset myhash username liyong
hmset myhash a 1 b 2
hget myhash username
hmget myhash a b
hgetall myhash
hdel myhash a
hdel myhash a b
hincrby myhash a 100
hexists myhash a
hlen myhash
hkeys myhash
hvals myhash
List
lpush mylist a b c (insert by left side)
c index:0/-3
b index:1/-2
a index:2/-1
rpush mylist a b c (insert by right side)
a index:0/-3
b index:1/-2
c index:2/-1
lindex mylist 0
lrange mylist 0 -1 (show item from first to the end)
lrange mylist 0 -2 (show item from first to the reciprocal second)
lpop mylist (eject the the left item)
rpop mylist
llen mylist
lpushx mylist x (insert x into the left side when mylist has items)
rpushx mylist x
lrem mylist 0 a (remove all item is a in mylist)
lrem mylist 2 a (remove two a from the beginning)
lrem mylist -2 a (remove two a from the end)
lset mylist 3 x (insert x which the index is 3)
linsert mylist before a x (insert x before the first a)
linsert mylist after a x (insert x after the first a)
rpoplpush mylist mylist2 (eject the right item in mylist and insert it into the left side in mylist2)
Set
sadd myset a b c
sadd myset a (can not success)
srem myset a b
smembers myset
sismember myset a
sdiff myset1 myset2 (myset1 - myset2)
sinter myset1 myset2 (交集)
sunion myset1 myset2 (并集)
scard myset
srandmember myset
sdiffstore newset1 oldset1 oldset2
sinterstore newset1 oldset1 oldset2
sunionstore newset1 oldset1 oldset2
Sorted Set
zadd mysort 70 a 80 b 90 c
zadd mysort 100 a (replace the a's score)
zscore mysort a (show a's score)
zcard mysort (show count)
zrem mysort a b
zrange mysort 0 -1 (show all items by score asc)
zrange mysort 0 -1 withscores (从小到大)
zrevrange mysort 0 -1 withscores (从大到小)
zrangebyscore mysort 0 100 withscores limit 0 2
zrank mysort b (show rank of b,从小到大)
zrevrank mysort b (show rank of b,从大到小)
zremrangebyrank mysort 0 4 (remove by index)
zremrangebyscore mysort 80 100 (remove by score)
zincrby mysort 10 a (add 10 to item a)
zcount mysort 80 90
发布订阅
SUBSCRIBE channela
PUBLISH channela test_content , 返回收到消息的客户端数量
UNSUBSCRIBE channela , 不如不指定,则取消订阅所有消息
事务
MULTI
开始事务
....
EXEC
执行
DISCARD
清空命令并取消事务
WATCH key...
WATCH key...之后的第一个事务T1里,若对watch的key进行处理时,其他client进程对某一个key进行操作,则事务T1会执行失败。