缓存redis常用命令1

最近拜读了redis指南,总结了redis的常用命令吧

redis-server Redis服务器

redis-cli Redis命令客户端

redis-benchmark Redis性能测试工具

redis-check-aof AOF文件修复工具

redis-check-dump RDB文件检查工具

redis-sentinel sentinel服务器(2.8版本以后)

./redis-server 即可启动 默认端口6379

./redis-server redis.conf 后端启动

状态回复

redis>ping
pong
错误回复
192.168.123.208:0>errorcommend
ERR unknown command ‘errorcommend’
整数回复
192.168.123.208:0>incr foo
1
字符串回复
192.168.123.208:0>incr foo
1
键值为空
192.168.123.208:0>get noexists
NULL
多行字符串回复
192.168.123.208:0>keys *
1) setc
2) letter
3) foo
建立一个bar的键
192.168.123.208:0>set bar 1
OK
192.168.123.208:0>keys *
1) bar
判断一个键是否存在
192.168.123.208:0>exists bar
1
192.168.123.208:0>exists noexists
0
删除键
192.168.123.208:0>del bar
1
192.168.123.208:0>del bar
0
获得键值的数据类型
192.168.123.208:0>set foo 1
OK

192.168.123.208:0>type foo
string

192.168.123.208:0>lpush bar 1
1

192.168.123.208:0>type bar
list
赋值与取值
192.168.123.208:0>set key hello
OK
192.168.123.208:0>get key
hello
递增数字
192.168.123.208:0>incr num
1

192.168.123.208:0>incr num
2

192.168.123.208:0>set foo lorem
OK

192.168.123.208:0>incr foo
ERR value is not an integer or out of range(键值不是整数)

增加指定的整数
192.168.123.208:0>del bar
1

192.168.123.208:0>incrby bar 2
2

192.168.123.208:0>incrby bar 3
5
减少指定的整数
192.168.123.208:0>decr bar
4

192.168.123.208:0>decrby bar -3
7
增加指定浮点数
192.168.123.208:0>incrbyfloat bar 2.7
9.7

192.168.123.208:0>incrbyfloat bar 5E+4
50009.69999999999999929
向尾部追加值
192.168.123.208:0>set key hello
OK

192.168.123.208:0>append key “world”
10

192.168.123.208:0>get key
helloworld
获取字符串长度
192.168.123.208:0>strlen key
10

192.168.123.208:0>set key 你好
OK

192.168.123.208:0>strlen key
6
同时获得/设置多个键值
192.168.123.208:0>mset key1 v1 key2 v2 key3 v3192.168.123.208:0>
OK
192.168.123.208:0>get key2
v2
192.168.123.208:0>mget key1 key3/字符串
1) v1
2) v3

位运算
192.168.123.208:0>set foo bar
OK

192.168.123.208:0>getbit foo 0
0

192.168.123.208:0>getbit foo 6
1

192.168.123.208:0>getbit foo 100000
0

192.168.123.208:0>setbit foo 6 0
1

192.168.123.208:0>setbit foo 7 1
0

192.168.123.208:0>get foo
aar
192.168.123.208:0>set foo1 bar
OK

192.168.123.208:0>set foo2 aar
OK

192.168.123.208:0>bitop or res foo1 foo2
3

192.168.123.208:0>get res
car

散列类型

192.168.123.208:0>hset car price 500
1

192.168.123.208:0>hset car name bmw
1

192.168.123.208:0>hget car name
bmw

192.168.123.208:0>hmget car price name
1) 500
2) bmw
192.168.123.208:0>hgetall car//散列
1) price
2) 500
3) name
4) bmw
判断字段是否存在
192.168.123.208:0>hexists car model
0

192.168.123.208:0>hset car model e350
1

192.168.123.208:0>hexists car model
1
增加数字
192.168.123.208:0>hincrby person score 60
60

192.168.123.208:0>hdel car price
1

192.168.123.208:0>hdel car price
0
只获取字段名或字段值
192.168.123.208:0>hkeys car
1) name
2) model
192.168.123.208:0>hvals car
1) bmw
2) e350
获得字段数量
192.168.123.208:0>hlen car
2
向列表两端增加元素
192.168.123.208:0>lpush numbers 1
1
1
192.168.123.208:0>lpush numbers 2 3
3
321
192.168.123.208:0>rpush numbers 0 -1
5
3210-1
从列表两端弹出元素

192.168.123.208:0>lpop numbers
3
210-1
192.168.123.208:0>rpop numbers
-1
210
获取列表中元素的个数
192.168.123.208:0>llen numbers
3
获取列表片段
192.168.123.208:0>lrange numbers 0 2//列表
1) 2
2) 1
3) 0
192.168.123.208:0>lrange numbers -2 -1
1) 1
2) 0
192.168.123.208:0>lrange numbers 1 999
1) 1
2) 0
(超出范围则返回最右边元素)
删除列表中指定的值
192.168.123.208:0>rpush numbers 2
4

192.168.123.208:0>lrange numbers 0 -1
1) 2
2) 1
3) 0
4) 2
192.168.123.208:0>lrem number -1 2
0

192.168.123.208:0>lrem numbers -1 2
1

192.168.123.208:0>lrange numbers 0 -1
1) 2
2) 1
3) 0
获得、设置指定索引的元素的值

192.168.123.208:0>lindex numbers 0
2

192.168.123.208:0>lindex numbers -1
0

192.168.123.208:0>lset numbers 1 7
OK

192.168.123.208:0>lindex numbers 1
7
保留列表判断
192.168.123.208:0>lrange numbers 0 -1
1) 2
2) 7
3) 0
192.168.123.208:0>lindex numbers 0
2

192.168.123.208:0>ltrim numbers 1 2
OK 删除

192.168.123.208:0>lrange numbers 0 1
1) 7
2) 0
192.168.123.208:0>lrange numbers 0 -1
1) 7
2) 0
向列表中插入元素

192.168.123.208:0>linsert numbers after 7 3
3

192.168.123.208:0>lrange numbers 0 -1
1) 7
2) 3
3) 0

192.168.123.208:0>linsert numbers before 2 1
-1

192.168.123.208:0>linsert numbers before 0 1
4

192.168.123.208:0>lrange numbers 0 -1
1) 7
2) 3
3) 1
4) 0

集合增加,删除
192.168.123.208:0>sadd letters a
1

192.168.123.208:0>sadd letters a b c
2

192.168.123.208:0>srem letters c d
1
获得集合中所有元素

192.168.123.208:0>smembers letters//集合
1) a
2) b
判断元素是否存在集合中
192.168.123.208:0>sismember letters a
1

192.168.123.208:0>sismember letters d
0

集合运算
192.168.123.208:0>sadd seta 1 2 3
3

192.168.123.208:0>sadd setb 2 3 4
3

192.168.123.208:0>sdiff seta setb
1) 1
192.168.123.208:0>sdiff setb seta
1) 4
192.168.123.208:0>sadd setc 2 3
2

192.168.123.208:0>sdiff seta setb setc
1) 1

192.168.123.208:0>sinter seta setb
1) 2
2) 3
192.168.123.208:0>sinter seta setb setc
1) 2
2) 3
192.168.123.208:0>sunion seta setb
1) 1
2) 2
3) 3
4) 4
192.168.123.208:0>sunion seta setb set c
1) 1
2) 2
3) 3
4) 4
获得集合元素个数
192.168.123.208:0>smembers letters
1) a
2) b
192.168.123.208:0>scard letters
2
获取随机集合元素
192.168.123.208:0>srandmember letters
a

192.168.123.208:0>srandmember letters
a

192.168.123.208:0>srandmember letters
b

192.168.123.208:0>srandmember letters
b

192.168.123.208:0>srandmember letters
a
192.168.123.208:0>sadd letters c d
2
192.168.123.208:0>srandmember letters 2
1) a
2) d
192.168.123.208:0>srandmember letters 2
1) d
2) c
192.168.123.208:0>srandmember letters 100
1) a
2) b
3) d
4) c
192.168.123.208:0>srandmember letters -2
1) c
2) c
192.168.123.208:0>srandmember letters -10
1) d
2) a
3) b
4) b
5) a
6) a
7) c
8) c
9) d
10) d
192.168.123.208:0>spop letters
a

192.168.123.208:0>smembers letters
1) d
2) b
3) c
有序集合
增加元素
192.168.123.208:0>zadd scoreboard 89 tom 67 peter 100 david
3

192.168.123.208:0>zadd scoreboard 76 peter
0

192.168.123.208:0>zadd testboard 17E+307 a
1
192.168.123.208:0>zadd testboard 1.5 b
1

192.168.123.208:0>zadd testboard +inf c
1

192.168.123.208:0>zadd testboard -inf d
1
获取元素的分数
192.168.123.208:0>zscore scoreboard tom
89
获得排名在某个范围的元素列表
192.168.123.208:0>zrange scoreboard 0 2
1) peter
2) tom
3) david
192.168.123.208:0>zrange scoreboard 1 -1
1) tom
2) david
192.168.123.208:0>zrange scoreboard 0 -1 withscores
1) peter
2) 76
3) tom
4) 89
5) david
6) 100
192.168.123.208:0>zadd chineseName 0 张三 0 李四 0 王五 0 赵六
4

192.168.123.208:0>zrange chineseName 0 -1
1) 张三
2) 李四
3) 王五
4) 赵六
获得指定分数范围的元素
192.168.123.208:0>zrangebyscore scoreboard 80 100
1) tom
2) david
192.168.123.208:0>zrangebyscore scoreboard 80 (100
1) tom
192.168.123.208:0>zrangebyscore scoreboard (80 +inf
1) tom
2) david
192.168.123.208:0>zadd scoreboard 56 jerry 92 wendy 67 yvonne
3

192.168.123.208:0>zrange scoreboard 0 -1 withscores
1) jerry
2) 56
3) yvonne
4) 67
5) peter
6) 76
7) tom
8) 89
9) wendy
10) 92
11) david
12) 100
192.168.123.208:0>zrangebyscore scoreboard 60 +inf limit 1 3
1) peter
2) tom
3) wendy

192.168.123.208:0>zrevrangebyscore scoreboard 100 0 limit 0 3
1) david
2) wendy
3) tom
增加某个元素分数
192.168.123.208:0>zincrby scoreboard 4 jerry
60

192.168.123.208:0>zincrby scoreboard -4 jerry
56
获得集合中元素的数量
192.168.123.208:0>zcard scoreboard
6
获得指定分数范围内元素个数
192.168.123.208:0>zcount scoreboard 90 100
2

192.168.123.208:0>zcount scoreboard (89 +inf
2

删除一个或多个元素
192.168.123.208:0>zrem scoreboard wendy
1

192.168.123.208:0>zcard scoreboard
5
按照排名范围删除

192.168.123.208:0>zadd testRem 1 a 2 b 3 c 4 d 5 e 6 f
6

192.168.123.208:0>zremrangebyrank testRem 0 2
3

192.168.123.208:0>zrange testRem 0 -1
1) d
2) e
3) f
按照分数范围删除
192.168.123.208:0>zremrangebyscore testRem (4 5
1

192.168.123.208:0>zrange testRem 0 -1
1) d
2) f
获得元素排名
192.168.123.208:0>zrank scoreboard peter
2

192.168.123.208:0>zrevrank scoreboard peter
2
计算有序集合交集
192.168.123.208:0>zinterstore sortedSetsRedult 2 sortedSets1 sortedSets2 aggregate min
2

192.168.123.208:0>zrange sortedSetsRedult 0 -1 withscores
1) a
2) 1
3) b
4) 2
192.168.123.208:0>zinterstore sortedSetsRedult 2 sortedSets1 sortedSets2 aggregate max
2

192.168.123.208:0>zrange sortedSetsRedult 0 -1 withscores
1) a
2) 10
3) b
4) 20
192.168.123.208:0>zinterstore sortedSetsRedult 2 sortedSets1 sortedSets2 weights 1 0.1
2

192.168.123.208:0>zrange sortedSetsRedult 0 -1 withscores
1) a
2) 2
3) b
4) 4
入门基础几乎总结全,是本人一个个敲的,希望给我好评,未经本人同意不得转发

你可能感兴趣的:(redis,缓存,性能测试,常用redis命令)