Redis常用命令
Redis-Key
ping
flushdb
flushall
keys *
set name zhangsan
get name
exists name
expire name 10
ttl name
move name 1 # 不常用
String
set key1 v1
append key1 “hello”
strlen key1
incr views
decr views
incrby views 10
decrby views 10
getrange key1 0 -1
setrange key1 1 xx
setex key seconds value
setnx key value
mset k1 v1 k2 v2 k3 v3
mget k1 k2 k3
msetnx k1 v1 k4 v4
getset k v
List
所有list命令都是l
开头的
Lpush list v1 v2 v3
Lrange list 0 1
Rpush list v6 v5 v4
Lpop list
Rpop list
Lindex list 0
Llen list
Lrem list 1 v1
Ltrim list 1 3
Rpoplpush list otherlist
Lset list 0 item # 列表/下标 不存在, 会报错
Linsert list before/after “v2” vvvv
消息队列(Lpush Rpop), 栈(Lpush Lpop)
Set
Sadd myset “hello”
Sadd myset “world”
Smembers myset
Sismember myset hello
Scard myset
Srem myset hello
无序不随机
Srandmember myset 1 随机返回一个值
Spop myset # 随机删除key
Smove myset myset2 world
Sdiff key1 key2 # 差集
Sinter key1 key2 # 交集
Sunion key1 key2 # 并集
Hash
Map集合 key-map
hset myhash k1 v1
hget myhash k1
hmset myhash k1 v1 k2 v2
hmget myhash k1 k2
hgetall myhash
hdel myhash k1
hlen myhash # 获取字段数量
hexists myhash k1
hkeys myhash
hvals myhash
hincrby myhash k1 1
Zset
zadd myset 1 one
zrange myset 0 -1
zrangebyscore salary -inf +inf # 小到大
zrevrange salary 0 -1 # 大到小
zrange
zrem salary one
zcard salary