应用场景

  • 缓存
  • 计数 (incr)
  • 共享Session
  • 限速 (限定时间内能进行N次操作 expire)
  • 消息队列 lpush + brpop
  • 文章列表
  • 用户标签 (sadd)
  • 抽奖 (spop/srandmember)

常用全局命令

keys *  //线上环境禁止使用,时间复杂度为O(n)
dbsize

exists key
del key [key ...]
expire key seconds
ttl //显示Key过期时间
    - -1 没有过期时间
    - -2 key not exist
    - >=0 key剩余时间

type  key  //when key not exist, return none

rename key newkey

string

set key value
setnx  key value   //set when not exist, if exist, return 0
get key          //return nil when key not exist
mset a 1 b 2 c 3
mget a b c


incr key //if key is not exist, will create one and value is 1
incrby key  n

append key value

strlen key
getset key value//return current value, and set a new value, just like  var++

setrange key offset value 
getrange key start end

hash

hset key field value
hget key field
hdel key field [field...]

hlen key
hstrlen key field

hmget key field [field...]
hmset key field value [field value...]

hexists key field
hkeys key 

hvals key
hgetall key



list

lpush key value [ value....]
rpush key value [value...]

lpop key 
rpop key

ltrim key start end 

lset key index newValue

llen key

set

sadd key element [ele]
srem key element [ele]

scard key // get elements size O(1)

sismember key ele
srandmember key [count]

spop key  //random pop 
smemberrs key

//operations between sets

sinter key [key...] //交集
sunion key [key...] //并集
sdiff key [key...]  //差集
//store the result 

sinterstore dest key [key...]
sunionstore dest key [key...]
sdiffstore dest key [key...]

zset

zset 通过给每个成员打分进行排序

zadd key score member [score member...]
zcard key
zscore key member

//计算成员排名
zrank key member
zrevrank key member

//返回指定排名范围的成员
zrank key start end
zrevrank key start end

zrem key member [member...]
//加分
zincrby key increment member