Redis数据结构与操作
1. 数据类型及操作
Redis所有单个操作都是原子操作,多个操作支持事务。
1.1 String
SET key value
GET key
MSET key value [key value ...] // 设置多个key、value
MGET key [key ...]
SETNX key value // 只有在key不存在时,才设置
SETEX key seconds value // 设置key,同时设置生存时间
INCR key
DECR key
INCRBY key increment
DECRBY key decrement
1.2 Hash
HSET key field value
HGET key field
HMGET key field [field ...]
HGETALL key
HDEL key filed [field ...]
HEXISTS key field
HINCRBY key field increment
HKEYS key // 返回哈希表key中的所有域
HLEN key // 返回哈希表key中域的数量
HVALS key
HMSET key field value [field value ...]
3.3 List
LPUSH key value [value ...]
LPUSHX key value
RPUSH key value [value ...]
RPUSHX key value
LPOP key
RPOP key
LRANGE key start stop // -1表示列表最后一个元素,-2表示列表倒数第二个元素
LREM key count value
LSET key index value
LINDEX key index
LINSERT key BEFORE|AFTER pivot value
LLEN key
LTRIM key start stop
RPOPLPUSH source destination
BLPOP key timeout // 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
BRPOP key timeout // 如果列表没有元素会阻塞列表直到等待超时或发现可弹出元素为止。
BRPOPLPUSH source destination timeout // 阻塞
3.4 Set
SADD key member [member ...]
SCARD key
SDIFF key [key ...] // 差集
SDIFFSTORE destination key [key ...]
SINTER key [key ...] // 交集
SINTER destination key [key ...]
SISMEMBER key member
SMEMBERS key
SMOVE source destination member
SPOP key [count]
SRANDMEMBER key [count]
SREM key member [member ...]
SUNION key [key ...]
SUNIONSTORE destination key [key ...]
3.5 SortedSort
ZADD key score member [[score member]...]
ZCARD key
ZCOUNT key min max
ZRANGE key start stop [WITHSCORES]
ZREVRANGE key start stop [WITHSCORES]
ZRANK key member
ZREVRANK key member
ZREM key member [member ...]
ZREMRANGEBYRANK key start stop
ZREMRANGEBYSCORE key min max
3.6 Key
DEL key [key ...]
EXISTS key
EXPIRE key seconds
EXPIREAT key timestamp
KEYS pattern
MIGRATE host port key destination-db timeout [COPY][REPLACE]
MOVE key db
PERSIST key
RANDOMKEY
RENAME key newkey
TTL key
PTTL key
TYPE key
3.7 系统相关命令
BGREWRITEAOF
BGSAVE
CLIENT KILL host:port
CLIENT LIST
CONFIG GET parameter
CONFIG RESETSTAT
CONFIG REWRITE
CONFIG SET parameter value
SELECT index
DBSIZE
DEBUG OBJECT key
FLUSHALL
FLUSHDB
INFO [section]
LASTSAVE
MONITOR
SHUTDOWN [SAVE|NOSAVE]
3.8 事务
WATCH
UNWATCH
MULTI
EXEC
DISCARD