Redis 基本操作
参考:An introduction to Redis data types and abstractions
中文翻译:看云
Redis命令查询:Command Reference - Redis
Strings
# set and get
set key value # set value
get key
# multiple operation
mset key1 value1 [key2 value2...] # multiple set
mget key1 [key2...] # multiple get
# string operation
getrange key start end # get sub string
setrange key offset value # rewrite from specific offset
strlen key # get string length
append key value # append value to existing value(returns added value length)
# bit operation
setbit key offset value # set bit value by offset
getbit key offset # get bit value by offset
# expires
setex key expire # create a key with expire
set key ex expire # creat a key with expire
psetex key expire # expires after milie-seconds
# if exists
setex key value # set if exists
setnx key value # set if not exits(returns 1 on success, 0 on failed)
set key value nx # set if not exits(return OK on success, (nil) on failed)
# change self
incr key # increment on value
incrby key increment # increment on value by increment
incrbyfloat key increment # increment on value by float increment
decr key # same
decrby key decrement # same
getset # get set(can be used to count without omit)
Lists
#other
llen key # list length
# push
lpush key ele1 [ele2 ...] # push to the left(one by one)
rpush key ele1 [ele2 ...] # push to the right(one by one)
lpushx key value # push if exists
rpushx key value # push if exists
# pop(get and remove)
rpop mylist # pop one from the right, return null if empty
lpop mylist # same
# get
lindex index # get value by index
lrange key start end # get element[a, b], [-2,-1]
# remove
lrem key value count # default 0 remove all, <0 remove from tail
# Block pop (如果有多个client在wait, 先wait的client先被服务.)
brpop key1 [key2 ...] timeout # block until new element(0 wait forever)
blpop key1 [key2 ....] timeout # block until new element(0 wait forever)
# pop and push
rpoplpush source desti # rpop from source, lpsuh to desti
brpoplpush source desti # same but blocking
# slice
ltrim key start end # only keep elements in [start, end]
List的常用案例:
- 记忆用户最近的更新。
- 进程间通信。使用生产者消费者模型。
- Reliable queue (消费者取得数据后可能因为某种原因未完成)
- Circular list(sour和desti相同, 逐个遍历列表而非使用lrange一次取出所有元素, 在有多个client同时遍历,或者在遍历过程中加入新的元素时,都能依次遍历,此外,由于原子性操作,不会在丢失过程中丢失数据)
Hashes
# set
hset key filed value # hash set
hmset key filed1 value1 [field2 value2 ...] # hash multiple set
hsetnx key field value # set field if the filed not exists
# delete
hdel key filed1 [field2 ...] # delete multiple fields from hash
# other
hlen key # get the # of fields
hscan key cursor [pattern] [count] # similar to scan, but in hashes
hstrlen key filed # get length of value of field
hexists key filed # check existance
# get
hget key filed # get one filed
hmget key filed1 [filed2 ...] # get multiple field
hgetall key # get all fields with values
hkeys key # get all fields
hvals key # get all values
# change value
hincrby key filed increment # hash increment
hincrebyfloat key field increment # hash increment by float
Note:
- 尽管hash非常适合用于表示对象,但我们也可以将hash用在别的地方(因为hash里面的内容是没有限制的)
- 小哈希(例如,一些拥有小值的元素)在内存中以特殊的方式编码存储,以让内存存储变得更有效
Redis Sets
# get size (or cardinality 基数、集合的势)
scard key
# add
sadd key value1 [value2 ...] # add multple values to set
# get
smembers key # get all elements of set
srandmember key # randomly get one member
# check existance
sismenber key value # check existence
# pop
spop key # **randomly** pop one element
# UNION
sunionstore res key1 [key2 ...] # get union of sets and store result
Redis Sorted sets
# add
zadd key score1 mem1 [score2 mem2] # add multiple elements
# get by rank
zrange key start end [withscores] # get a range of elements
zrerange key start end [withscores] # reverse order
# operate on ranges
zrangebysore key min max # get all between min and max
zremrangebyscore key min max # remove all between min and max
# get rank
zrank key value # get rank
zrerank key value # reverse order
# lexicographical scores(字典序操作)
(可以用于自动完成)
zrangebylex
zrevrangebylex
zremrangebylex
zlexcount
Note: 重复调用zadd可以更新元素的score(可用于游戏排行榜)
Bitmaps
# operations
setbit key offset val # set bit
getbit key offset # get bit
bitop [and/or/xor/not] dest key1 [key2...] # bit operate and store to dest
bitcount
bitpos key [start end] # find the first bit of 0 or 1
可用于记录网站用户每日访问情况。
HyperLogLogs
一种类似set的数据结构,用于估计set的大小(更快地)。
对Key自身的操作(无关乎数据和数据类类型)
type key # get key type
del key # delete key
exists key # check existance
expire key # set an expire for a key
pexpire key # set an expire in millis for a key
persist key # remove expire for a key
ttl key # time to live
pttl key # time to live in milliseconds
遍历keys
keys # 一次返回所有的key(当keys特别多时应采用scan)
scan # 逐个遍历key (可以模糊匹配)