127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> set test1 123 OK 127.0.0.1:6379> set test2 ab OK 127.0.0.1:6379> keys * 1) "test1" 2) "test2" 127.0.0.1:6379> get test1 "123" 127.0.0.1:6379> get test2 "ab" 127.0.0.1:6379> get test3 (nil) 127.0.0.1:6379>
127.0.0.1:6379> keys * 1) "test1" 2) "test2" 127.0.0.1:6379> type test1 string 127.0.0.1:6379> type test2 string
help
1 2 3 4 5 6 7 8 9 10 11 12
127.0.0.1:6379> help redis-cli 2.8.19 Type: "help @<group>" to get a list of commands in <group> "help <command>"forhelp on <command> "help <tab>" to get a list of possible help topics "quit" to exit 127.0.0.1:6379> helptype
TYPE key summary: Determine the type stored at key since: 1.0.0 group: generic
127.0.0.1:6379> keys * 1) "num" 2) "test1" 3) "test" 127.0.0.1:6379> get test "123" 127.0.0.1:6379> append test"abc" (integer) 6 127.0.0.1:6379> get test "123abc" 127.0.0.1:6379>
strlen
字符串长度,返回数据的长度,如果键不存在则返回0。注意,如果键值为空串,返回也是0。
语法:STRLEN key
1 2 3 4 5 6 7 8 9 10 11 12 13
127.0.0.1:6379> get test "123abc" 127.0.0.1:6379> strlen test (integer) 6 127.0.0.1:6379> strlen tnt (integer) 0 127.0.0.1:6379> set tnt "" OK 127.0.0.1:6379> strlen tnt (integer) 0 127.0.0.1:6379> exists tnt (integer) 1 127.0.0.1:6379>
mset/mget
同时设置/获取多个键值
语法:MSET key value [key value …]
MGET key [key …]
1 2 3 4 5 6 7 8 9 10 11
127.0.0.1:6379> flushall OK 127.0.0.1:6379> keys * (empty list or set) 127.0.0.1:6379> mset a 1 b 2 c 3 OK 127.0.0.1:6379> mget a b c 1) "1" 2) "2" 3) "3" 127.0.0.1:6379>
127.0.0.1:6379> hset user username chenchen (integer) 1 127.0.0.1:6379> hget user username "chenchen" 127.0.0.1:6379> hset user username chen (integer) 0 127.0.0.1:6379> keys user 1) "user" 127.0.0.1:6379> hgetall user 1) "username" 2) "chen" 127.0.0.1:6379> 127.0.0.1:6379> hset user age 18 (integer) 1 127.0.0.1:6379> hset user address "xi'an" (integer) 1 127.0.0.1:6379> hgetall user 1) "username" 2) "chen" 3) "age" 4) "18" 3) "address" 4) "xi'an" 127.0.0.1:6379>
HSET命令不区分插入和更新操作,当执行插入操作时HSET命令返回1,当执行更新操作时返回0。
(3)hincrby
1 2 3 4
127.0.0.1:6379> hdecrby article total 1 #执行会出错 127.0.0.1:6379> hincrby article total -1 #没有hdecrby自减命令 (integer) 1 127.0.0.1:6379> hget article total #获取值
(4)hmset/hmget
HMSET和HMGET设置和获取对象属性
1 2 3 4 5 6 7 8 9 10 11
127.0.0.1:6379> hmset person username tony age 18 OK 127.0.0.1:6379> hmget person age username 1) "18" 2) "tony" 127.0.0.1:6379> hgetall person 1) "username" 2) "tony" 3) "age" 4) "18" 127.0.0.1:6379>
注意:上面HMGET字段顺序可以自行定义
(5)hexists
属性是否存在
1 2 3 4 5 6 7 8 9
127.0.0.1:6379> hexists killer (error) ERR wrong number of arguments for'hexists'command 127.0.0.1:6379> hexists killer a (integer) 0 127.0.0.1:6379> hexists user username (integer) 1 127.0.0.1:6379> hexists person age (integer) 1 127.0.0.1:6379>
(6) hdel
删除属性
1 2 3 4 5 6 7 8 9 10 11
127.0.0.1:6379> hdel user age (integer) 1 127.0.0.1:6379> hgetall user 1) "username" 2) "chen" 127.0.0.1:6379> hgetall person 1) "username" 2) "tony" 3) "age" 4) "18" 127.0.0.1:6379>
(7)hkeys/hvals
只获取字段名HKEYS或字段值HVALS
1 2 3 4 5 6
127.0.0.1:6379> hkeys person 1) "username" 2) "age" 127.0.0.1:6379> hvals person 1) "tony" 2) "18"
(8)hlen
元素个数
1 2 3 4 5
127.0.0.1:6379> hlen user (integer) 1 127.0.0.1:6379> hlen person (integer) 2 127.0.0.1:6379>
127.0.0.1:6379> sadd name tony (integer) 1 127.0.0.1:6379> sadd name hellen (integer) 1 127.0.0.1:6379> sadd name rose (integer) 1 127.0.0.1:6379> sadd name rose (integer) 0
(2)smembers
获取内容
1 2 3 4
127.0.0.1:6379> smembers name 1) "hellen" 2) "rose" 3) "tony"
(3)spop
移除并返回集合中的一个随机元素
1 2 3 4 5 6 7 8 9 10 11 12
127.0.0.1:6379> smembers internet 1) "amoeba" 2) "redis" 3) "rabbitmq" 4) "nginx" 127.0.0.1:6379> spop internet "rabbitmq" 127.0.0.1:6379> spop internet "nginx" 127.0.0.1:6379> smembers internet 1) "amoeba" 2) "redis"
127.0.0.1:6379> set w 200 OK 127.0.0.1:6379> set z 700 OK 127.0.0.1:6379> mget w z 1) "200" 2) "700" 127.0.0.1:6379> multi OK 127.0.0.1:6379> decrby z 100 QUEUED #注意此命令根本没有执行,而是把其放在一个队列中 127.0.0.1:6379> incrby w 100 QUEUED 127.0.0.1:6379> mget w z QUEUED 127.0.0.1:6379> get w #同时,这些相关的变量也不能再读取 QUEUED 127.0.0.1:6379> get z QUEUED 127.0.0.1:6379> exec 1) (integer) 600 2) (integer) 300 3) 1) "300" 2) "600" 4) "300" 5) "600" 127.0.0.1:6379> mget w z 1) "300" 2) "600" 127.0.0.1:6379>
(4)如果有错误指令,自动取消
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
127.0.0.1:6379> mget w z 1) "300" 2) "600" 127.0.0.1:6379> multi OK 127.0.0.1:6379> get w QUEUED 127.0.0.1:6379> set w 100 QUEUED 127.0.0.1:6379> abc (error) ERR unknown command'abc' 127.0.0.1:6379> exec (error) EXECABORT Transaction discarded because of previous errors. 127.0.0.1:6379> mget w z #可以看出数据并未变化 1) "300" 2) "600" 127.0.0.1:6379>
(5)discard取消事务
注意redis事务太简单,没有回滚,而只有取消。
1 2 3 4 5 6 7 8 9 10 11 12 13
127.0.0.1:6379> mget z w 1) "600" 2) "300" 127.0.0.1:6379> multi OK 127.0.0.1:6379> incrby z 100 QUEUED 127.0.0.1:6379> discard OK 127.0.0.1:6379> get z "600" 127.0.0.1:6379> exec (error) ERR EXEC without MULTI
(6)秒杀抢票事务处理
客户端1:
1 2 3 4 5 6 7 8 9 10 11 12
127.0.0.1:6379> set ticket 1 OK 127.0.0.1:6379> set money 0 OK 127.0.0.1:6379> watch ticket #乐观锁,对值进行观察,改变则事务失败 OK 127.0.0.1:6379> multi #开启事务 OK 127.0.0.1:6379> decr ticket QUEUED 127.0.0.1:6379> incrby money 100 QUEUED
客户端2:还没等客户端1提交事务,此时客户端2把票买到了。
1 2 3 4
127.0.0.1:6379> get ticket "1" 127.0.0.1:6379> decr ticket (integer) 0