Redis 的一些基本指令

这是我在用 try.redis.io 时的记录。

1. key 中可以有冒号

127.0.0.1:6379> set server:name "fido"
OK
127.0.0.1:6379> get server:name
"fido"

2. 删除后再访问

127.0.0.1:6379> del server:name
(integer) 1
127.0.0.1:6379> get server:name
(nil)http://try.redis.io/

3. incr, incrby, decr, decrby

incr 增一操作,如果该变量没定义过,直接使用 incr,会被赋值为1. incr 是原子操作

incr 增1

incrby 5 增加5

decr 减1

decrby 5 减少5

127.0.0.1:6379> set connections 10
OK
127.0.0.1:6379> incr connections
(integer) 11
127.0.0.1:6379> get connections
"11"
127.0.0.1:6379> del connections
(integer) 1
127.0.0.1:6379> get connections
(nil)
127.0.0.1:6379> incr connections
(integer) 1

4. expire, ttl

设置生存时间,ttl 可以了解剩余的生存时间,如果超期,tll 返回的值将是 -2

127.0.0.1:6379> set resource:local "Redis Demo"
OK
127.0.0.1:6379> expire resource:local 120
(integer) 1
127.0.0.1:6379> get resource:local
"Redis Demo"
127.0.0.1:6379> ttl resource:local
(integer) 6
127.0.0.1:6379> ttl resource:local
(integer) -2
127.0.0.1:6379> get resource:local
(nil)

5. list, rpush/lpush, lrange

list(其实就是队列)操作。rpush 将值从右端放入,lpush 将值从左端放入,

lrange m n 从左端显示第m个元素到第n个元素,

没有 rrange 指令

127.0.0.1:6379> rpush friehttp://try.redis.io/nds "Alice"
(integer) 1
127.0.0.1:6379> get friends
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> rpush friends "Bob
"Invalid argument(s)
127.0.0.1:6379> rpush friends "Bob"
(integer) 2
127.0.0.1:6379> lpush friends "Sam"
(integer) 3
127.0.0.1:6379> lrange friends --1
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> lrange friends 0-1
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> lrange friends 0 1 2
(error) ERR wrong number of arguments for 'lrange' command
127.0.0.1:6379> lrange friends 0 -1
1) "Sam"
2) "Alice"
3) "Bob"http://try.redis.io/
127.0.0.1:6379> lrange friends 0 1
1) "Sam"
2) "Alice"
127.0.0.1:6379> lrange friends 0 2
1) "Sam"
2) "Alice"
3) "Bob"

6. list opr: llen, lpop, rpop

list(其实就是队列)操作

llen 数组(队列)长度

lpop 从左端弹出

rpop 从右端弹出

127.0.0.1:6379> llen friends
(integer) 3
127.0.0.1:6379> lrange friends 0 -1
1) "Sam"
2) "Alice"
3) "Bob"
127.0.0.1:6379> lpop friends
"Sam"
127.0.0.1:6379> lrange friends 0 -1
1) "Alice"
2) "Bob"
127.0.0.1:6379> rpop friends
"Bob"
127.0.0.1:6379> lrange friends 0 -1
1) "Alice"

7. set 集合操作: sadd, srem, sismember

sadd, set add 给集合中添加元素

srem, set remove 删除集合中的元素

sismemeber, set is member 检查是不是集合中的元素,是的话返回1,不是返回0

127.0.0.1:6379> sadd helloset "hello world"
(integer) 1
127.0.0.1:6379> sadd helloset "hello hadoop"
(integer) 1
127.0.0.1:6379> sadd helloset "hello redis"
(integer) 1
127.0.0.1:6379> srem helloset "hello world"
(integer) 1
127.0.0.1:6379> sismember helloset "hello world"
(integer) 0
127.0.0.1:6379> sismember helloset "hello redis"
(integer) 1
127.0.0.1:6379> smembers helloset
1) "hello hadoop"
2) "hello redis"

8. sunion

并集,应该生成了一个新的集合,原先的集合还是保持原样。

127.0.0.1:6379> sadd hiset "hi word"
(integer) 1
127.0.0.1:6379> sadd hiset "hi world"
(integer) 1
127.0.0.1:6379> sunion helloset hiset
1) "hi word"
2) "hi world"
3) "hello hadoop"
4) "hello redis"
127.0.0.1:6379> smembers helloset
1) "hello hadoop"
2) "hello redis"
127.0.0.1:6379> smembers hiset
1) "hi word"
2) "hi world"

9. 集合中的元素不会重复

127.0.0.1:6379> smembers hiset
1) "hi word"
2) "hi world"
127.0.0.1:6379> sadd hiset "hi world"
(integer) 0
127.0.0.1:6379> smembers hiset
1) "hi word"
2) "hi world"
127.0.0.1:6379> 

10. sorted set, 排序集

zadd 添加

zrange 显示

127.0.0.1:6379> zadd family 2010 "lele"
(integer) 1
127.0.0.1:6379> zadd family 1923 "popo"
(integer) 1
127.0.0.1:6379> zadd family 1982 "wo"
(integer) 1
127.0.0.1:6379> zadd family 1960 "baba"
(integer) 1
127.0.0.1:6379> zrange 0 -1
(error) ERR wrong number of arguments for 'zrange' command
127.0.0.1:6379> zrange family 0 -1
1) "popo"
2) "baba"
3) "wo"
4) "lele"
127.0.0.1:6379> zrange family 2 4
1) "wo"
2) "lele"

11. hashes

和 map 比较相关的一个类型

hset 设置一个属性

hmset 批量设置属性

hgetall 显示一个元素的所有属性

hget 显示一个元素的一个属性

127.0.0.1:6379> hset user:1000 name "Zhang San"
(integer) 1
127.0.0.1:6379> hset user:1000 email "zhangsan@xmail.com"
(integer) 1
127.0.0.1:6379> hset user:1000 password "s3cret"
(integer) 1
127.0.0.1:6379> hgetall user:1000
1) "name"
2) "Zhang San"
3) "email"
4) "zhangsan@xmail.com"
5) "password"
6) "s3cret"
127.0.0.1:6379> hmset user:1001 name "Li si" email "lisi@xmail.com"
OK
127.0.0.1:6379> hgetall uuser:1001
(empty list or set)
127.0.0.1:6379> hgetall user:1001
1) "name"
2) "Li si"
3) "email"
4) "lisi@xmail.com"
127.0.0.1:6379> hget user:1001 email
"lisi@xmail.com"

12. hash 系统中的数值

hincrby 增加

注意没有 hincr 这个自增长指令

hdel 删除一个元素中的某个属性

127.0.0.1:6379> hset user:1000 visits 10
(integer) 1
127.0.0.1:6379> hincr user:1000 visits
(error) ERR unknown command 'hincr'
127.0.0.1:6379> hincrby user:1000 1 visits
(error) ERR value is not an integer or out of range
127.0.0.1:6379> hincrby user:1000 visits 1
(integer) 11
127.0.0.1:6379> hget user:1000 visits
"11"
127.0.0.1:6379> hincrby user:1000 visits 20
(integer) 31
127.0.0.1:6379> hdel user:1000 visits
(integer) 1
127.0.0.1:6379> hincrby user:1000 visits 1
(integer) 1

参:

http://try.redis.io/