Try Redis

redis

Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。

save(存储)

set name redis

get(获取)

get name

del(删除)

del name

inrc(自增加一)

1
2
3
4
set number 10
incr number => 11
del number
incr number =>1

expire(存储时间)

expire name 120

ttl(查看剩余时间)

ttl name =>108

被删除后会输出-2,如果输出-1表示永不过期,如果重新set name value,ttl也会被reset

rpush(list末尾添加)

1
2
rpush friends "Jack"
rpush friends "Rose"

lpush(list头部添加)

1
lpush friends "Jesus"

lrange(获取list)

1
2
3
4
5
6
lrange friends 0 -1 =>
1) "Jesus"
2) "Jack"
3) "Rose"
lrange friends 0 1
lrange friends 1 2

-1表示取到list的最后一位

llen(获取list长度)

llen friends => 3

lpop(移除list第一个)

lpop friends =>"Jesus"

rpop(移除list最后一个)

rpop friends

sadd(给set添加数据)

1
2
3
4
sadd heroes "Iron Man"
sadd heroes "Transformers"
sadd heroes "Spider Man"
sadd heroes "Ant Man"

srem(从set移除数据)

srem heroes "Transfromers"

sismember(set中是否包含)

1
2
sismember heroes "Iron Man" => 1
sismember heroes "Flash Man" => 0

0表示不存在,1表示不存在

smembers(获取set所有值)

1
smembers heroes

sunion(合并set)

1
2
3
sadd beauties "Sophie"
sadd beauties "Sheeba"
sunion heroes beauties =>

Sorted Sets

1
2
3
4
5
zadd ranks 3 "Python"
zadd ranks 2 "Java"
zadd ranks 4 "Ruby"
zadd ranks 1 "JavaScript"
zadd ranks 5 "Php"

sorted sets多了一个sorce,根据score排序

zragne

1
2
3
4
zrange ranks 1 3 =>
1) "Java"
2) "Python"
3) "Ruby"

hset(存储对象)

1
2
3
hset user name "Jack"
hset user email "jack@gmail.com"
hset user password "rose"

hgetall(获取对象所有信息)

1
2
3
4
5
6
7
hgetall user =>
1) "name"
2) "Jack"
3) "email"
4) "jack@gmail.com"
5) "password"
6) "rose"

hmset(一次设置多个字段)

1
hmset user:002 name "Hodor" password "holdthedoor" email "hodor@gmail.com"

hget(获取单个字段)

1
hget user:002 name
1
2
3
4
5
HSET user:002 waittime 1000
HINCRBY user:002 waittime 1 => 1001
HINCRBY user:002 waittime 10 => 1011
HDEL user:002 waittime
HINCRBY user:002 waittime 1 => 1