Redis 是速度非常快的非关系型(NoSQL)内存键值数据库,可以存储键和五种不同类型的值之间的映射。键的类型只能为字符串,值支持五种数据类型:字符串、列表、集合、散列表、有序集合。
Redis 支持很多特性,例如将内存中的数据持久化到硬盘中,使用复制来扩展读性能,使用分片来扩展写性能。
在启动时,可以通过“redis-cli -n 1”指定启动那个数据库。
1.get 、set:
127.0.0.1:6379> set k1 abc
OK
127.0.0.1:6379> get k1
"abc"
2.列出所有的键:
127.0.0.1:6379> keys *
1) "hello"
2) "k1"
3.SETNX:
not exist只有在key不存在的时候才允许创建
127.0.0.1:6379> SETNX k1 www
(integer) 0
127.0.0.1:6379> get k1
"abc"
4. xx:
只有存在的时候,才能设置
127.0.0.1:6379> set k1 www xx
OK
127.0.0.1:6379> get k1
"www"
5.记录value原始类型:
127.0.0.1:6379> get k2
"99"
127.0.0.1:6379> OBJECT encoding k2
"int"
6.MSET设置多个键值:
127.0.0.1:6379> MSET k3 3 k4 4
OK
127.0.0.1:6379> keys *
1) "k3"
2) "k4"
3) "k1"
4) "hello"
5) "k2"
7.MSETNX:
键不存在的时候,设置多个键值
127.0.0.1:6379> MSETNX k3 3 k4 4
(integer) 0
8.keys ?
查找命令
127.0.0.1:6379> keys ?
(empty list or set)
127.0.0.1:6379> keys ??
1) "k3"
2) "k4"
3) "k1"
4) "k2"
9.键类型:
127.0.0.1:6379> type k3
string
10.长度:
127.0.0.1:6379> STRLEN k3
(integer) 1
11.FLUSHALL:
把所有的数据库全部清掉
127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> keys *
(empty list or set)
12.FLUSHDB:
只清除掉当前的数据库,一个6379端口最多可以有16个数据库
127.0.0.1:6379> keys *
1) "k"
127.0.0.1:6379> FLUSHDB
OK
13.键是否存在:
127.0.0.1:6379> EXISTS k
(integer) 0
14.返回旧值并设置新值:
127.0.0.1:6379> set k 1
OK
127.0.0.1:6379> get k
"1"
127.0.0.1:6379> GETSET k 2
"1"
127.0.0.1:6379> get k
"2"
15.MGET:
获取多个给定的键的值
127.0.0.1:6379> MGET k k1
1) "2"
2) "k1"
16.append追加:
127.0.0.1:6379> APPEND k www
(integer) 4
127.0.0.1:6379> get k
"2www"
17.获取子字符串:
127.0.0.1:6379> get k
"2www"
127.0.0.1:6379> GETRANGE k 0 2
"2ww"
18.步长1的增减:
127.0.0.1:6379> set k1 99
OK
127.0.0.1:6379> INCR k1
(integer) 100
127.0.0.1:6379> INCR k1
(integer) 101
127.0.0.1:6379> INCR k1
(integer) 102
19.指定数值增长:
127.0.0.1:6379> INCRBY k1 2
(integer) 104
127.0.0.1:6379> INCRBY k1 2
(integer) 106
127.0.0.1:6379> INCRBYFLOAT k1 1.5
"107.5"
127.0.0.1:6379> INCRBYFLOAT k1 1.5
"109"
20.设置比特位
前面偏移量,后面值
127.0.0.1:6379> SETBIT k2 1 1
(integer) 0
127.0.0.1:6379> get k2
"@"
21.获取某一位上的值:
127.0.0.1:6379> SETBIT k 9 1
(integer) 0
127.0.0.1:6379> get k
"@@"
127.0.0.1:6379> GETBIT k 9
(integer) 1
22.位操作:
进行的是与运算
127.0.0.1:6379> SETBIT k1 1 1
(integer) 0
127.0.0.1:6379> SETBIT k2 7 1
(integer) 0
127.0.0.1:6379> BITOP and k3 k1 k2
(integer) 1
127.0.0.1:6379> get k3
"\x00"
127.0.0.1:6379> BITOP or k4 k1 k2
(integer) 1
127.0.0.1:6379> get k4
"A"
1.先推再查:
127.0.0.1:6379> LPUSH l1 a
(integer) 1
127.0.0.1:6379> LPUSH l1 b c d e f g
(integer) 7
127.0.0.1:6379> LRANGE l1 0 -1
1) "g"
2) "f"
3) "e"
4) "d"
5) "c"
6) "b"
7) "a"
2.从左侧:
127.0.0.1:6379> LPOP l1
"g"
127.0.0.1:6379> LPOP l1
"f"
127.0.0.1:6379> LPOP l1
"e"
127.0.0.1:6379> LPOP l1
"d"
3.从右侧:
127.0.0.1:6379> RPOP l1
"a"
127.0.0.1:6379> RPOP l1
"b"
127.0.0.1:6379> RPOP l1
"c"
4.从一个列表尾部弹出元素压入到另一个列表的头部:
127.0.0.1:6379> LPUSH l1 b c d e f
(integer) 5
127.0.0.1:6379> RPOPLPUSH l1 l2
"b"
127.0.0.1:6379> LRANGE l1 0 -1
1) "f"
2) "e"
3) "d"
4) "c"
5.获取指定位置的元素:
1) "f"
2) "e"
3) "d"
4) "c"
127.0.0.1:6379> LINDEX l1 2
"d"
6.设置指定位置上元素的值:
127.0.0.1:6379> LSET l1 0 a
OK
127.0.0.1:6379> LRANGE l1 0 -1
1) "a"
2) "e"
3) "d"
4) "c"
7.从列表头部开始删除值等于value的元素count次,LIST 可以重复出现:
LREM listkey -1 ab就是倒着删,如果是0就全都删了
127.0.0.1:6379> RPUSH listkey c abc c ab 123 ab bj ab redis list
(integer) 10
127.0.0.1:6379> LRANGE listkey 0 -1
1) "c"
2) "abc"
3) "c"
4) "ab"
5) "123"
6) "ab"
7) "bj"
8) "ab"
9) "redis"
10) "list"
127.0.0.1:6379> LREM listkey 2 ab
(integer) 2
127.0.0.1:6379> LRANGE listkey 0 -1
1) "c"
2) "abc"
3) "c"
4) "123"
5) "bj"
6) "ab"
7) "redis"
8) "list"
8.去除指定范围 外 元素:
(integer) 10
127.0.0.1:6379> LTRIM listkey 0 0
OK
127.0.0.1:6379> LRANGE listkey 0 -1
1) "c"
9.在列表中某个存在的值前后插入元素:
127.0.0.1:6379> LPUSH l3 a b d
(integer) 3
127.0.0.1:6379> LRANGE l3 0 -1
1) "d"
2) "b"
3) "a"
127.0.0.1:6379> LINSERT l3 before b x
(integer) 4
127.0.0.1:6379> LRANGE l3 0 -1
1) "d"
2) "x"
3) "b"
4) "a"
127.0.0.1:6379> LINSERT l3 after b s
(integer) 5
127.0.0.1:6379> LRANGE l3 0 -1
1) "d"
2) "x"
3) "b"
4) "s"
5) "a"
10阻塞:
如果弹出的列表不存在或者为空,就会阻塞
超时时间设置为0,就是永久阻塞,直到有数据可以弹出
如果多个客户端阻塞在同一个列表上,使用First In First Service原则,先到先服务
127.0.0.1:6379> BLPOP l1 0
1) "l1"
2) "1"
(23.62s)
BLPOP l1 0中的0表示进入无限的时间等待状态,5表示5s后解除阻塞
127.0.0.1:6379> BLPOP l1 5
(nil)
(5.32s)
127.0.0.1:6379> HSET k1 f1 v1
(integer) 1
127.0.0.1:6379> HSET k1 name husky
(integer) 1
127.0.0.1:6379> HSET k1 age 10
(integer) 1
127.0.0.1:6379> HMSET k1 sex m name G
OK
2.获取value:
127.0.0.1:6379> HVALS k1
1) "v1"
2) "G"
3) "10"
4) "m"
3.拿到所有的field:
127.0.0.1:6379> HKEYS k1
1) "f1"
2) "name"
3) "age"
4) "sex"
4.返回字段个数:
127.0.0.1:6379> HMSET k1 name hushy age 1
OK
127.0.0.1:6379> HKEYS k1
1) "name"
2) "age"
127.0.0.1:6379> HLEN k1
(integer) 2
5.判断字段是否存在:
key或field不存在,返回0
127.0.0.1:6379> HEXISTS k1 name
(integer) 1
127.0.0.1:6379> HEXISTS k1 sex
(integer) 0
6.返回所有键值对:
127.0.0.1:6379> HGETALL k1
1) "name"
2) "hushy"
3) "age"
4) "1"
7.在字段对应的值上进行增量运算:
127.0.0.1:6379> HINCRBY k1 age 9
(integer) 10
127.0.0.1:6379> HINCRBYFLOAT k1 age 0.5
"10.5"
Hash用途:
不适合hash的情况:
1.添加一个或多个元素:
127.0.0.1:6379> SADD s1 mike marie peter ben marry
(integer) 5
127.0.0.1:6379> SMEMBERS s1
1) "ben"
2) "marie"
3) "mike"
4) "peter"
5) "marry"
2.移除一个或多个元素:
SREM s1 ben
SREM s1 ben mike
3.返回集合包含的所有元素:
SMEMBERS s1
4.检查给定的元素是否存在在集合中:
127.0.0.1:6379> SISMEMBER s1 ben
(integer) 1
5.随机返回集合中指定个数的元素:
127.0.0.1:6379> SRANDMEMBER s1 -3
1) "marie"
2) "mike"
3) "mike"
127.0.0.1:6379> SRANDMEMBER s1 4
1) "peter"
2) "marie"
3) "ben"
4) "mike"
127.0.0.1:6379> SRANDMEMBER s1 0
(empty list or set)
127.0.0.1:6379> SRANDMEMBER s1
"mike"
6.随机弹出一个值:
127.0.0.1:6379> SPOP s1
"peter"
127.0.0.1:6379> SMEMBERS s1
1) "marie"
2) "ben"
3) "marry"
4) "mike"
7.返回元素个数:
127.0.0.1:6379> SCARD s1
(integer) 4
127.0.0.1:6379> SADD s1 1 2 3 4 5
(integer) 5
127.0.0.1:6379> SADD s2 1 2 3 9 8
(integer) 5
127.0.0.1:6379> SDIFFSTORE s3 s1 s2
(integer) 2
127.0.0.1:6379> SMEMBERS s3
1) "4"
2) "5"
127.0.0.1:6379> SINTER s1 s2
1) "1"
2) "2"
3) "3"
1.添加
127.0.0.1:6379> ZADD fruits 3.2 香蕉
(integer) 1
127.0.0.1:6379> ZADD fruits 2.0 西瓜
(integer) 1
127.0.0.1:6379> ZADD fruits 4.0 番石榴 7.0 梨 6.8 芒果
(integer) 3
127.0.0.1:6379> quit
[root@node1 ~]# redis-cli --raw
127.0.0.1:6379> ZRANGE fruits 0 -1
西瓜
香蕉
番石榴
芒果
梨
2.从小到大
127.0.0.1:6379> ZRANGE fruits 0 -1 withscores
西瓜
2
香蕉
3.2000000000000002
番石榴
4
芒果
6.7999999999999998
梨
7
3.从大到小
127.0.0.1:6379> ZREVRANGE fruits 0 -1 withscores
梨
7
芒果
6.7999999999999998
番石榴
4
香蕉
3.2000000000000002
西瓜
2
4.移除一个或多个元素
127.0.0.1:6379> ZREM fruits 2.0 西瓜
1
127.0.0.1:6379> ZRANGE fruits 0 -1
香蕉
番石榴
芒果
梨
5.增减分值:
127.0.0.1:6379> ZINCRBY fruits 1.5 香蕉
4.7000000000000002
6.返回元素排名:
127.0.0.1:6379> ZRANK fruits 香蕉
1
7.返回指定区间范围内的元素:
127.0.0.1:6379> ZRANGE fruits 2 4 withscores
芒果
6.7999999999999998
梨
7
8.返回指定分值范围内的元素:
127.0.0.1:6379> ZRANGEBYSCORE fruits 3 5.5 withscores
番石榴
4
香蕉
4.7000000000000002
9.移除指定排名的元素:
127.0.0.1:6379> ZREMRANGEBYRANK fruits 1 2
2
127.0.0.1:6379> ZRANGE fruits 0 -1
番石榴
梨
10.移除指定分值范围内的元素:
127.0.0.1:6379> ZREMRANGEBYSCORE fruits 3 5
1
127.0.0.1:6379> ZRANGE fruits 0 -1 withscores
梨
7
11.返回集合元素个数:
127.0.0.1:6379> ZCARD fruits
1
12.返回指定范围内元素个数:
127.0.0.1:6379> ZCOUNT fruits 0 0
0
127.0.0.1:6379> ZCOUNT fruits 0 7
1
127.0.0.1:6379> ZCOUNT fruits (7 9
0
13.并集
和ZUNIONSTORE scores-all1 2 scores1 scores2 AGGREGATE SUM效果一样
127.0.0.1:6379> ZADD scores1 70 tom 80 peter 60 john
3
127.0.0.1:6379> ZADD scores2 90 peter 60 ben
2
127.0.0.1:6379> ZUNIONSTORE scores3 2 scores1 scores2
4
127.0.0.1:6379> ZRANGE scores3 0 -1 withscores
ben
60
john
60
tom
70
peter
170
127.0.0.1:6379> ZUNIONSTORE scores-all3 2 scores1 scores2 WEIGHTS 1 0.5 AGGREGATE SUM
4
127.0.0.1:6379> ZRANGE scores-all3 0 -1 withscores
ben
30
john
60
tom
70
peter
125