redis 五大数据类型的常用指令
STRING
192.168.1.66:6379> get k1
"v1"
192.168.1.66:6379> append k1 12345
(integer) 7
192.168.1.66:6379> get k1
"v112345"
append的使用
192.168.1.66:6379> STRLEN k1
(integer) 7
获取长度
192.168.1.66:6379> set k1 1
OK
192.168.1.66:6379> incr k1
(integer) 2
192.168.1.66:6379> incr k1
(integer) 3
192.168.1.66:6379> get k1
"3"
192.168.1.66:6379> type k1
string
incr 设置自增
192.168.1.66:6379> decr k1
(integer) 2
192.168.1.66:6379> decr k1
(integer) 1
192.168.1.66:6379> get k1
"1"
decr 自减
192.168.1.66:6379> get k1
"9"
192.168.1.66:6379> decrby k1 2
(integer) 7
192.168.1.66:6379> decrby k1 2
(integer) 5
192.168.1.66:6379> get k1
"5"
跨越式自减decrby
192.168.1.66:6379> get k1
"0123456"
192.168.1.66:6379> getrange k1 0 -1
"0123456"
192.168.1.66:6379> getrange k1 0 3
"0123"
getrange获取范围数据
192.168.1.66:6379> get k1
"0123456"
192.168.1.66:6379> setrange k1 0 xxx
(integer) 7
192.168.1.66:6379> get k1
"xxx3456"
setrange 设置内容
192.168.1.66:6379> setex k4 10 v4
OK
192.168.1.66:6379> ttl k4
(integer) 4
192.168.1.66:6379> ttl k4
(integer) 3
192.168.1.66:6379> ttl k4
(integer) 1
192.168.1.66:6379> ttl k4
(integer) -2
setex 设置key 同时设置存活周期
192.168.1.66:6379> setnx k1 v11
(integer) 0
192.168.1.66:6379> get k1
"xxx3456"
192.168.1.66:6379> setnx k11 v11
(integer) 1
192.168.1.66:6379> get k11
"v11"
setnx 只有不存在的时候,才能设置
192.168.1.66:6379[1]> mset k1 v1 k2 v2 k3 v3 k4 v4
OK
192.168.1.66:6379[1]> keys *
1) "k3"
2) "k2"
3) "k4"
4) "k1"
mset 批量设置
192.168.1.66:6379[1]> keys *
1) "k3"
2) "k2"
3) "k4"
4) "k1"
192.168.1.66:6379[1]> msetnx k4 v4 k5 v5
(integer) 0
192.168.1.66:6379[1]> keys *
1) "k3"
2) "k2"
3) "k4"
4) "k1"
msetnx 只要有一个失败,全部失败
192.168.1.66:6379[1]> keys *
1) "k3"
2) "k2"
3) "k4"
4) "k1"
192.168.1.66:6379[1]> msetnx k4 v4 k5 v5
(integer) 0
192.168.1.66:6379[1]> keys *
1) "k3"
2) "k2"
3) "k4"
4) "k1"
192.168.1.66:6379[1]> msetnx k5 v5 k6 v6
(integer) 1
192.168.1.66:6379[1]> keys *
1) "k3"
2) "k4"
3) "k5"
4) "k2"
5) "k6"
6) "k1"
msetnx 都不存在,才成功
LIST
192.168.1.66:6379> LPUSH list01 1 2 3 4 5
(integer) 5
192.168.1.66:6379> lrange list01 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
192.168.1.66:6379> RPUSH list02 1 2 3 4 5
(integer) 5
192.168.1.66:6379> lrange list02 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
先进先出队列(拉shi)
先进后出栈(喝多了,吐了)
192.168.1.66:6379> lrange list01 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
192.168.1.66:6379> lpop list01
"5"
192.168.1.66:6379> lrange list01 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
lpop 把左侧的先剔除了
老师语录:你自己敲一次,胜过听我讲十次。
192.168.1.66:6379> lrange list01 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
192.168.1.66:6379> lpop list01
"5"
192.168.1.66:6379> lrange list01 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
192.168.1.66:6379> RPOP list01
"1"
192.168.1.66:6379> lrange list01 0 -1
1) "4"
2) "3"
3) "2"
192.168.1.66:6379> LINDEX list01 2
"2"
LINDEX 获取特定的位置的数据
192.168.1.66:6379> lrange list01 0 -1
1) "4"
2) "3"
3) "2"
192.168.1.66:6379> llen list01
(integer) 3
llen 获取长度
192.168.1.66:6379> RPUSH list03 1 1 1 2 2 2 3 3 3 4 4 4 5 6 7
(integer) 15
192.168.1.66:6379> lrem list03 2 3
(integer) 2
192.168.1.66:6379> lrange list03 0 -1
1) "1"
2) "1"
3) "1"
4) "2"
5) "2"
6) "2"
7) "3"
8) "4"
9) "4"
10) "4"
11) "5"
12) "6"
13) "7"
lrem 移除多个值
192.168.1.66:6379> lrange list03 0 -1
1) "1"
2) "1"
3) "1"
4) "2"
5) "2"
6) "2"
7) "3"
8) "4"
9) "4"
10) "4"
11) "5"
12) "6"
13) "7"
192.168.1.66:6379> LTRIM list03 3 5
OK
192.168.1.66:6379> lrange list03 0 -1
1) "2"
2) "2"
3) "2"
LTRIM 从第几个开始,截取到第几个之后赋值给list
192.168.1.66:6379> lrange list01 0 -1
1) "4"
2) "3"
3) "2"
192.168.1.66:6379> lrange list02 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
192.168.1.66:6379> RPOPLPUSH list01 list02
"2"
192.168.1.66:6379> lrange list02 0 -1
1) "2"
2) "1"
3) "2"
4) "3"
5) "4"
6) "5"
RPOPLPUSH 将某个值压入
192.168.1.66:6379> lrange list02 0 -1
1) "2"
2) "1"
3) "2"
4) "3"
5) "4"
6) "5"
192.168.1.66:6379> lset list02 0 0
OK
192.168.1.66:6379> lrange list02 0 -1
1) "0"
2) "1"
3) "2"
4) "3"
5) "4"
6) "5"
lset 设定特定位置的list值
192.168.1.66:6379> LINSERT list02 before 2 java
(integer) 7
192.168.1.66:6379> lrange list02 0 -1
1) "0"
2) "1"
3) "java"
4) "2"
5) "3"
6) "4"
7) "5"
192.168.1.66:6379> LINSERT list02 after 2 php
(integer) 8
192.168.1.66:6379> lrange list02 0 -1
1) "0"
2) "1"
3) "java"
4) "2"
5) "php"
6) "3"
7) "4"
8) "5"
192.168.1.66:6379> LINSERT list03 after 2 php
(integer) 4
192.168.1.66:6379> lrange list03 0 -1
1) "2"
2) "php"
3) "2"
4) "2"
192.168.1.66:6379> LINSERT list03 before 2 php
(integer) 5
192.168.1.66:6379> lrange list03 0 -1
1) "php"
2) "2"
3) "php"
4) "2"
5) "2"
LINSERT 在特定位置插入内容,默认查找出第一个作为参照。
SET
192.168.1.66:6379> sadd set01 1 1 2 2 3 3
(integer) 3
192.168.1.66:6379> SMEMBERS set01
1) "1"
2) "2"
3) "3"
set 也是集合,但是不允许有重复的
192.168.1.66:6379> SISMEMBER set01 1
(integer) 1
192.168.1.66:6379> SISMEMBER set01 x
(integer) 0
sismember查看是否是成员
192.168.1.66:6379> SMEMBERS set01
1) "1"
2) "2"
3) "3"
192.168.1.66:6379> SCARD set01
(integer) 3
scard 查看集合中有多少个元素
192.168.1.66:6379> SMEMBERS set01
1) "1"
2) "2"
3) "3"
192.168.1.66:6379> SCARD set01
(integer) 3
192.168.1.66:6379> srem set01 3
(integer) 1
192.168.1.66:6379> SMEMBERS set01
1) "1"
2) "2"
srem 删除某个元素
192.168.1.66:6379> SMEMBERS set01
1) "1"
2) "2"
192.168.1.66:6379> sadd set01 1 2 3 4 5 6 7
(integer) 5
192.168.1.66:6379> SMEMBERS set01
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
set的好处就是自动去重,保证数据的唯一性
192.168.1.66:6379> SMEMBERS set01
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
192.168.1.66:6379> SRANDMEMBER set01 3
1) "6"
2) "7"
3) "2"
192.168.1.66:6379> SRANDMEMBER set01 3
1) "7"
2) "2"
3) "4"
192.168.1.66:6379> SRANDMEMBER set01 3
1) "3"
2) "7"
3) "1"
192.168.1.66:6379> SRANDMEMBER set01 3
1) "5"
2) "2"
3) "4"
SRANDMEMBER 随机出三个数字
192.168.1.66:6379> SMEMBERS set01
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
192.168.1.66:6379> spop set01 2
1) "4"
2) "3"
192.168.1.66:6379> spop set01 2
1) "5"
2) "6"
192.168.1.66:6379> spop set01 2
1) "8"
2) "9"
192.168.1.66:6379> SMEMBERS set01
1) "1"
2) "2"
3) "7"
spop 随机出栈,高并发,速度快
192.168.1.66:6379> SMEMBERS set01
1) "1"
2) "2"
3) "7"
192.168.1.66:6379> SMEMBERS set02
1) "z"
2) "y"
3) "x"
192.168.1.66:6379> smove set01 set02 1
(integer) 1
192.168.1.66:6379> SMEMBERS set01
1) "2"
2) "7"
192.168.1.66:6379> SMEMBERS set02
1) "z"
2) "y"
3) "x"
4) "1"
smove 移动值
192.168.1.66:6379> SMEMBERS set01
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
192.168.1.66:6379> SMEMBERS set02
1) "a"
2) "3"
3) "2"
4) "1"
5) "b"
192.168.1.66:6379> sdiff set01 set02
1) "4"
2) "5"
192.168.1.66:6379> sdiff set02 set01
1) "a"
2) "b"
sdiff 找出set01中比set02中多出的内容(差集)
192.168.1.66:6379> SMEMBERS set01
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
192.168.1.66:6379> SMEMBERS set02
1) "a"
2) "3"
3) "2"
4) "1"
5) "b"
192.168.1.66:6379> sdiff set01 set02
1) "4"
2) "5"
192.168.1.66:6379> sdiff set02 set01
1) "a"
2) "b"
192.168.1.66:6379> SINTER set01 set02
1) "1"
2) "2"
3) "3"
192.168.1.66:6379> SUNION set01 set02
1) "a"
2) "3"
3) "5"
4) "b"
5) "4"
6) "2"
7) "1"
SINTER 交集 SUNION 并集(去重)
万丈高楼平地起,肚子里要有货
学以致用,学在用前
Hash 哈希 (非常,及其重要,非常及其重要)
kv模式不变,但是v是一个键值对
192.168.1.66:6379> hset user id 11
(integer) 1
192.168.1.66:6379> hget user id
"11"
192.168.1.66:6379> keys *
1) "user"
hset hget基本使用
192.168.1.66:6379> hmset customer id 11 name li4 age 26
OK
192.168.1.66:6379> hmget customer id name age
1) "11"
2) "li4"
3) "26"
hmset hmget 的使用
192.168.1.66:6379> hgetall customer
1) "id"
2) "11"
3) "name"
4) "li4"
5) "age"
6) "26"
hgetall 获取全部的键值对数据
192.168.1.66:6379> hdel customer age
(integer) 1
192.168.1.66:6379> hgetall customer
1) "id"
2) "11"
3) "name"
4) "li4"
hdel 删除某个键值
192.168.1.66:6379> HLEN customer
(integer) 2
192.168.1.66:6379> HEXISTS customer id
(integer) 1
192.168.1.66:6379> HEXISTS customer email
(integer) 0
HLEN 查看长度,HEXISTS 查看是否存在
横向关联之后,其实发现没什么新技术,都是你抄我的,我抄你的
redis-cli -h 192.168.1.66 -a 123456
直接连接redis,密码也加入了,不需要再次密码验证了
192.168.1.66:6379> HKEYS customer
1) "id"
2) "name"
192.168.1.66:6379> HVALS customer
1) "11"
2) "li4"
HKEYS 获取共有多少个key,HVALS 获取值
192.168.1.66:6379> HVALS customer
1) "11"
2) "li4"
3) "18"
192.168.1.66:6379> HINCRBY customer age 2
(integer) 20
192.168.1.66:6379> HINCRBY customer age 2
(integer) 22
192.168.1.66:6379> HINCRBY customer age 2
(integer) 24
192.168.1.66:6379> HVALS customer
1) "11"
2) "li4"
3) "24"
HINCRBY 设置某个值增长
192.168.1.66:6379> hset customer score 92
(integer) 1
192.168.1.66:6379> HVALS customer
1) "11"
2) "li4"
3) "24"
4) "92"
192.168.1.66:6379> HINCRBYFLOAT customer score 0.5
"92.5"
192.168.1.66:6379> HVALS customer
1) "11"
2) "li4"
3) "24"
4) "92.5"
HINCRBYFLOAT 增加浮点数数值
ZSet(有序集合)
zset,在set的基础上加了一个score的值。
知识是基础,基础之上是业务,掌握知识,分析业务,开发项目。
192.168.1.66:6379> zadd zset01 60 v1 70 v2 80 v3 90 v4 100 v5
(integer) 5
192.168.1.66:6379> ZRANGE zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
5) "v5"
192.168.1.66:6379> ZRANGE zset01 0 -1 withscores
1) "v1"
2) "60"
3) "v2"
4) "70"
5) "v3"
6) "80"
7) "v4"
8) "90"
9) "v5"
10) "100"
zadd zrange
192.168.1.66:6379> ZRANGEBYSCORE zset01 60 90
1) "v1"
2) "v2"
3) "v3"
4) "v4"
ZRANGEBYSCORE 按分数范围查找
192.168.1.66:6379> ZRANGEBYSCORE zset01 60 (90
1) "v1"
2) "v2"
3) "v3"
192.168.1.66:6379> ZRANGEBYSCORE zset01 (60 (90
1) "v2"
2) "v3"
192.168.1.66:6379> ZREM zset01 v5
(integer) 1
192.168.1.66:6379> ZRANGE zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
ZREM 删除某个元素
192.168.1.66:6379> zcard zset01
(integer) 4
192.168.1.66:6379> zcount zset01 60 80
(integer) 3
192.168.1.66:6379> zrank zset01 v4
(integer) 3
192.168.1.66:6379> zscore zset01 v4
"90"
zcard获取数量,zcount获取 范围数量,zrank获取key zscore 获取分数
192.168.1.66:6379> ZREVRANK zset01 v4
(integer) 0
ZREVRANK 逆序获取下标值
192.168.1.66:6379> ZRANGE zset01 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
192.168.1.66:6379> ZREVRANGE zset01 0 -1
1) "v4"
2) "v3"
3) "v2"
4) "v1"
ZREVRANGE 逆序获取数据
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/8005290.html,如需转载请自行联系原作者