redis基本操作
了解下未封装过的redis直接操作
redis趣味课程
redis支持的数据类型
string字符串型
list
set
zset(sorted-set)
hash
分别的操作
- 对于string类型
set server:name "fido"
get server:name
setnx server:name "test" //error
set server:name "test" //ok
del server:name
setnx server:name 1 //ok
incr server:name //(integer)2
incr: atomic operation
set cor "expire"
ttl cor // -1
expire cor 20
ttl cor
ttl cor //-2 after 20s
- 对于列表list
lpush friends "milk"
rpush friends "bread"
lrange friends 0, 0
lrange friends 0, 1
lrange friends 1, 1
lrange friends 0, -1
llen friends //2
lpop friends
lrange friends 0, -1
rpop friends
lrange friends 0, -1 // empty list or set
- 对于无序集合set
sadd super "tom"
sadd super "jim"
sadd super "kimmy"
srem super "jim"
sismember super "tom" // 1
sismember super "jim" // 0
smembers super
sadd lord "lord"
sunion super lord
- 对于可排序集合zset(
Redis 1.2 introduced Sorted Sets
)
zset
zadd val 2 "tom"
zadd val 8 "yiy"
zadd val 1 "pop"
zrange val 0 -1
zrange val 1 2
- 对于hash
hset user:1000 name "tom"
hset user:1000 age "22"
hgetall
hset user:1000 name "jimmy" age "14"
hgetall
hget user:1000 name
hset user:1000 num 1
hincrby user:1000 num 1
hincrby user:1000 num 10
hdel user:1000 num
hincrby user:1000 num 1