声明
本篇文章并没任何的技术含量。
只是本人的命令练习记录,以备以后查看。
[TOC]
1 redis 基础命令
在看基本数据类型之前,先来看看redis的key相关的知识点。
1.1 keys
127.0.0.1:6379> keys *
1) "counter:__rand_int__"
2) "mylist"
3) "key:__rand_int__"
4) "k1"
1.2 exists
判断某个key是否存在
127.0.0.1:6379> EXISTS k1
(integer) 1 # 1表示存在
127.0.0.1:6379> EXISTS k11
(integer) 0 # 0表示不存在
1.3 move
指定键,移动数据至指定的DB。
move key db-index
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> set k2 v2
OK
127.0.0.1:6379> keys *
1) "k2"
2) "k1"
127.0.0.1:6379> MOVE k1 6 # 将k1移动至6号库
(integer) 1
127.0.0.1:6379> keys *
1) "k2"
127.0.0.1:6379> SELECT 6
OK
127.0.0.1:6379[6]> keys *
1) "k1"
1.4 expire
为给定 key
设置生存时间,当 key
过期时(生存时间为 0
),它会被自动删除。
expire key seconds
127.0.0.1:6379[6]> set k1 v1
OK
127.0.0.1:6379[6]> set k2 v2
OK
127.0.0.1:6379[6]> EXPIRE k1 10 # 10秒之后,k1被删除
(integer) 1
1.5 ttl
返回key剩余的过期时间。
- 在Redis 2.6和之前版本
- 如果key不存在或者已过期时返回
-1
。 - 从Redis2.8开始
- 如果key不存在或者已过期,返回
-2
- 如果key没有设置过期时间(永久有效),返回
-1
。
1.6 type
返回key
所存储的value
的数据结构类型,比如string
, list
, set
, zset
和 hash
等不同的类型。
2 redis-五大数据类型-简介
- string
string具有其他语言中的字符串的特性。string类型是Redis最基本的数据类型,一个redis中字符串value最多可以是512M
string类型是二进制安全的。意思是redis的string可以包含任何数据。比如jpg图片或者序列化的对象 。
- list
list是一个字符串列表,按照插入顺序排序。
- hash
我的第一映像就是Java里的Map<String,Object>
。
很相似,但不尽相同。
- set
和Java的Set<String>
类似,是一个无序的字符串集合。
- zset
和set的不同之处是,zset的每个元素都会关联一个double类型的score。
3 string
3.1 set/get/append/strlen/del
127.0.0.1:6379[6]> FLUSHALL
OK
127.0.0.1:6379[6]> set k1 a
OK
127.0.0.1:6379[6]> get k1 # 为k1设置值为"a"
"a"
127.0.0.1:6379[6]> APPEND k1 bcd # 为k1追加"bcd"
(integer) 4
127.0.0.1:6379[6]> get k1
"abcd"
127.0.0.1:6379[6]> STRLEN k1 # 查看长度
(integer) 4
127.0.0.1:6379[6]> DEL k1
(integer) 1
127.0.0.1:6379[6]> keys *
(empty list or set)
3.2 incr/incrby/decr/decrby
127.0.0.1:6379[6]> FLUSHALL
OK
127.0.0.1:6379[6]> set k1 1
OK
127.0.0.1:6379[6]> get k1
"1"
127.0.0.1:6379[6]> INCR k1
(integer) 2
127.0.0.1:6379[6]> INCR k1
(integer) 3
127.0.0.1:6379[6]> INCRBY k1 5 # 指定递增的步径长度为5
(integer) 8
127.0.0.1:6379[6]> INCRBY k1 5 # 指定递减的步径长度为3
(integer) 13
127.0.0.1:6379[6]> DECR k1
(integer) 12
127.0.0.1:6379[6]> DECRBY k1 3
(integer) 9
127.0.0.1:6379[6]>
3.3 getrange/setrange
- getrange key start end
- 用来取子串的命令
- start和end的取值和
Python
中的切片
类似 - setrange key offset value
- 从指定的offset处开始,覆盖value的长度
- 如果
offset>strlen(key)
,则在key
对应的值后补offset-strlen(key)
个零
后继续追加key
- 不存在的
key
被认为是空字符串
- offset最大可以是2^29-1(536870911)
127.0.0.1:6379[6]> set k "abcdefgh"
OK
127.0.0.1:6379[6]> SETRANGE k 1 "x"
(integer) 8
127.0.0.1:6379[6]> get k
"axcdefgh"
127.0.0.1:6379[6]> SETRANGE k 1 "yy"
(integer) 8
127.0.0.1:6379[6]> get k
"ayydefgh"
127.0.0.1:6379[6]> SETRANGE k 1 "yyyyyyyyyy"
(integer) 11
127.0.0.1:6379[6]> get k
"ayyyyyyyyyy"
127.0.0.1:6379[6]> setrange k 15 "123x"
(integer) 19
127.0.0.1:6379[6]> get k
"ayyyyyyyyyy\x00\x00\x00\x00123x"
# 此处的k88本来不存在,视为空字符串处理
127.0.0.1:6379[6]> SETRANGE k88 3 "haha"
(integer) 7
127.0.0.1:6379[6]> get k88
"\x00\x00\x00haha" # 视为空字符串处理所以前补3个零
127.0.0.1:6379[6]>
3.4 setex/getnx
- setex(set with expire)
- set的同时,给key指定一个过期时间
- setnx(set if not exists)
- 如果key不存在,则set值
- return 1 如果key被设置成功
- return 0 如果key没有被设置成功
# 设置值的同时指定8秒后过期
127.0.0.1:6379[6]> SETEX k88 10 "haha"
OK
127.0.0.1:6379> SETNX k1 111 # 如果k1不存在则设置k1为111
(integer) 1
127.0.0.1:6379> get k1
"111"
127.0.0.1:6379> SETNX k1 222 # k1已经存在,故不生效
(integer) 0
127.0.0.1:6379> get k1
"111"
3.5 mset/mget/msetnx
- mset k1 v1 [k2,v2 ...]
- mget
- msetnx k1 v1 [k2,v2 ...]
- 只要有一个key已经存在,就会失败
- return 1,所有的key都被设置成功
- return 0,所有的key都没有被设置(至少一个key已经存在)
127.0.0.1:6379> MSET k1 v1 k2 v2 k3 v3
OK
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> MGET k1 k2 k3
1) "v1"
2) "v2"
3) "v3"
4 list
4.1 lpush/rpush/lrange
127.0.0.1:6379> LPUSH l1 1 2 3 4 5 6
(integer) 6
127.0.0.1:6379> LRANGE l1 0 2
1) "6"
2) "5"
3) "4"
127.0.0.1:6379> LRANGE l1 0 -1
1) "6"
2) "5"
3) "4"
4) "3"
5) "2"
6) "1"
127.0.0.1:6379> LSET l1 2 "abc"
OK
127.0.0.1:6379> LRANGE l1 0 -1
1) "6"
2) "5"
3) "abc"
4) "3"
5) "2"
6) "1"
127.0.0.1:6379>
4.2 lpop/rpop/lindex/llen
- llen
- 如果 key 不存在,那么就被看作是空list,并且返回长度为 0
- 存储在 key 里的值不是一个list的话,会返回error
127.0.0.1:6379> LPUSH l1 1 2 3 4 5
(integer) 5
127.0.0.1:6379> LPOP l1
"5"
127.0.0.1:6379> RPOP l1
"1"
127.0.0.1:6379> LRANGE l1 0 -1
1) "4"
2) "3"
3) "2"
127.0.0.1:6379> RPUSH l2 1 2 3 4 5
(integer) 5
127.0.0.1:6379> LPOP l2
"1"
127.0.0.1:6379> RPOP l2
"5"
127.0.0.1:6379> LRANGE l2 0 -1
1) "2"
2) "3"
3) "4"
127.0.0.1:6379> RPUSH l3 1 2 3 4
(integer) 4
127.0.0.1:6379> LINDEX l3 0
"1"
127.0.0.1:6379> LINDEX l3 2
"3"
127.0.0.1:6379> LLEN l3
(integer) 4
4.3 lrem/ltrim
- lrem key count value
- count>0 : 从头到尾移除count个值为value的元素
- count<0 : 从尾到头移除count个值为value的元素
- count==0: 删除所有值为value的元素
- ltrim key start end
127.0.0.1:6379> RPUSH l1 a b c a c d e a d a
(integer) 10
127.0.0.1:6379> LRANGE l1 0 -1
1) "a"
2) "b"
3) "c"
4) "a"
5) "c"
6) "d"
7) "e"
8) "a"
9) "d"
10) "a"
# 从头到尾删除两个值为a的元素
127.0.0.1:6379> LREM l1 2 a
(integer) 2
127.0.0.1:6379> LRANGE l1 0 -1
1) "b"
2) "c"
3) "c"
4) "d"
5) "e"
6) "a"
7) "d"
8) "a"
# 从尾到头删除一个值为d的元素
127.0.0.1:6379> LREM l1 -1 d
(integer) 1
127.0.0.1:6379> LRANGE l1 0 -1
1) "b"
2) "c"
3) "c"
4) "d"
5) "e"
6) "a"
7) "a"
# 删除所有值为c的元素
127.0.0.1:6379> LREM l1 0 c
(integer) 2
127.0.0.1:6379> LRANGE l1 0 -1
1) "b"
2) "d"
3) "e"
4) "a"
5) "a"
127.0.0.1:6379> RPUSH l2 a b c d a c e
(integer) 7
# 只保留索引为2-4的元素
127.0.0.1:6379> LTRIM l2 2 4
OK
127.0.0.1:6379> LRANGE l2 0 -1
1) "c"
2) "d"
3) "a"
127.0.0.1:6379>
4.4 rpoplpush
127.0.0.1:6379> RPUSH l1 1 2 3
(integer) 3
127.0.0.1:6379> RPUSH l2 a b c
(integer) 3
127.0.0.1:6379> RPOPLPUSH l1 l2
"3"
127.0.0.1:6379> LRANGE l1 0 -1
1) "1"
2) "2"
127.0.0.1:6379> LRANGE l2 0 -1
1) "3"
2) "a"
3) "b"
4) "c"
4.5 linsert
127.0.0.1:6379> RPUSH l1 1 2 3
(integer) 3
# 给l1中的元素3前面插入元素
127.0.0.1:6379> LINSERT l1 before 3 "newVal"
(integer) 4
127.0.0.1:6379> LRANGE l1 0 -1
1) "1"
2) "2"
3) "newVal"
4) "3"
# 给l1中的元素"newVal"后面插入元素
127.0.0.1:6379> LINSERT l1 after "newVal" "anotherNewVal"
(integer) 5
127.0.0.1:6379> LRANGE l1 0 -1
1) "1"
2) "2"
3) "newVal"
4) "anotherNewVal"
5) "3"
# 在值为"ha"的元素后面插入
127.0.0.1:6379> LINSERT l1 after "ha" "o..."
# "ha"不存在,返回-1
(integer) -1
127.0.0.1:6379> LRANGE l1 0 -1
1) "1"
2) "2"
3) "newVal"
4) "anotherNewVal"
5) "3"
127.0.0.1:6379>
5 set
5.1 sadd/smembers/sismember/srem/scard
127.0.0.1:6379> SADD s1 1 2 3 1 2 3 1
(integer) 3
127.0.0.1:6379> SMEMBERS s1
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> SISMEMBER s1 2 # contains
(integer) 1
127.0.0.1:6379> SISMEMBER s1 111
(integer) 0
127.0.0.1:6379> SCARD s1 # count
(integer) 3
127.0.0.1:6379> SREM s1 2 # del
(integer) 1
127.0.0.1:6379> SMEMBERS s1
1) "1"
2) "3"
5.2 srandmember/spop
127.0.0.1:6379> SADD s2 0 1 2 3 4 5 6 7 8 9
(integer) 10
# srandmember并不会删除元素
127.0.0.1:6379> SRANDMEMBER s2 3 # 随机获取三个成员
1) "8"
2) "7"
3) "6"
127.0.0.1:6379> SRANDMEMBER s2 3
1) "8"
2) "2"
3) "4"
127.0.0.1:6379> SRANDMEMBER s2 3
1) "2"
2) "7"
3) "4"
# spop会删除元素
127.0.0.1:6379> spop s2 2
1) "2"
2) "7"
127.0.0.1:6379> spop s2 2
1) "5"
2) "9"
127.0.0.1:6379> spop s2 2
1) "8"
2) "4"
127.0.0.1:6379> SMEMBERS s2
1) "0"
2) "1"
3) "3"
4) "6"
5.3 smove
127.0.0.1:6379> sadd s1 1 2 3
(integer) 3
127.0.0.1:6379> SADD s2 a b c
(integer) 3
# 将s1中的值为2的元素移动到集合s2中
127.0.0.1:6379> SMOVE s1 s2 2
(integer) 1 # return 1 ==> 成功
127.0.0.1:6379> SMEMBERS s1
1) "1"
2) "3"
127.0.0.1:6379> SMEMBERS s2
1) "2"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> SMOVE s1 s2 -1
(integer) 0 # return 0 ==> 移动失败
127.0.0.1:6379>
5.4 数学集合操作
127.0.0.1:6379> SADD s1 1 2 3
(integer) 3
127.0.0.1:6379> SADD s2 2 3 a b
(integer) 4
127.0.0.1:6379> SINTER s2 s1 # 交集
1) "2"
2) "3"
127.0.0.1:6379> SUNION s1 s2 # 并集
1) "3"
2) "2"
3) "b"
4) "1"
5) "a"
127.0.0.1:6379> SDIFF s1 s2 # 差集
1) "1"
127.0.0.1:6379> SDIFF s2 s1 # 差集
1) "a"
2) "b"
127.0.0.1:6379>
6 hash
6.1 hget/hset/hmset/hmget/hgetall
127.0.0.1:6379> HSET user id 1
(integer) 1
127.0.0.1:6379> HSET user name "tom"
(integer) 1
127.0.0.1:6379> HGET user id
"1"
127.0.0.1:6379> HGET user name
"tom"
# set user.name="apache",user.age=24,user.sex="male"
127.0.0.1:6379> HMSET user name "apache" age 24 sex "male"
OK
127.0.0.1:6379> HGET user age
"24"
# get user.id,user.name,user.age,user.sex
127.0.0.1:6379> HMGET user id name age sex
1) "1"
2) "apache"
3) "24"
4) "male"
127.0.0.1:6379> HGETALL user
1) "id"
2) "1"
3) "name"
4) "apache"
5) "age"
6) "24"
7) "sex"
8) "male"
127.0.0.1:6379>
6.2 hlen/hdel/hkeys/hvals
127.0.0.1:6379> HDEL user age sex # del user.age ,del user.sex
(integer) 2
127.0.0.1:6379> HGETALL user
1) "id"
2) "1"
3) "name"
4) "apache"
127.0.0.1:6379> HLEN user
(integer) 2
# user.keySet()
127.0.0.1:6379> HKEYS user
1) "id"
2) "name"
# user.values()
127.0.0.1:6379> HVALS user
1) "1"
2) "apache"
127.0.0.1:6379>
6.3 hincrby/hIncrByFloat
127.0.0.1:6379> HSET u age 1
(integer) 1
127.0.0.1:6379> HINCRBY u age 1 # u.age++
(integer) 2
127.0.0.1:6379> HINCRBY u age 1 # u.age++
(integer) 3
127.0.0.1:6379> HINCRBY u age 1 # u.age++
(integer) 4
127.0.0.1:6379> HGET u age
"4"
127.0.0.1:6379> HINCRBY u age 10 # u.age += 10
(integer) 14
127.0.0.1:6379> HGET u age
"14"
127.0.0.1:6379> HINCRBY u age -2 # u.age -= 2
(integer) 12
127.0.0.1:6379> HINCRBYFLOAT u age 1.2 # u.age += 1.2
"13.2"
127.0.0.1:6379> HINCRBYFLOAT u age 1.5 # u.age += 1.5
"14.7"
127.0.0.1:6379> HINCRBYFLOAT u age -1.5 # u.age -= 1.5
"13.2"
127.0.0.1:6379>
6.4 hsetnx
127.0.0.1:6379> FLUSHALL
OK
127.0.0.1:6379> HSET user id 1
(integer) 1
127.0.0.1:6379> HSETNX user name "tom" # 不存在,则设置值
(integer) 1
127.0.0.1:6379> HSETNX user name "tom" # 存在,则无效
(integer) 0
127.0.0.1:6379>
7 zset
7.1 zadd/zrange
127.0.0.1:6379> ZADD z1 1 v1 2 v2 3 v3
(integer) 3
127.0.0.1:6379> ZRANGE z1 0 -1
1) "v1"
2) "v2"
3) "v3"
127.0.0.1:6379> ZRANGE z1 0 -1 withscores
1) "v1"
2) "1"
3) "v2"
4) "2"
5) "v3"
6) "3"
127.0.0.1:6379>
7.2 zRangeByScore
# 1 <= score <= 2
127.0.0.1:6379> ZRANGEBYSCORE z1 1 2
1) "v1"
2) "v2"
# 1 <= score <= 2 同时获取score
127.0.0.1:6379> ZRANGEBYSCORE z1 1 2 withscores
1) "v1"
2) "1"
3) "v2"
4) "2"
# 1 <= score < 2
127.0.0.1:6379> ZRANGEBYSCORE z1 1 (2
1) "v1"
# 1 <= score < 2 同时获取score
127.0.0.1:6379> ZRANGEBYSCORE z1 1 (2 withscores
1) "v1"
2) "1"
# 再次添加元素v4,v5
127.0.0.1:6379> ZADD z1 4 v4 5 v5
(integer) 2
127.0.0.1:6379> ZRANGE z1 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
5) "v5"
# 2 <= score <= 4
127.0.0.1:6379> ZRANGEBYSCORE z1 2 4
1) "v2"
2) "v3"
3) "v4"
# 此处的limit和MySQL类似
127.0.0.1:6379> ZRANGEBYSCORE z1 2 4 limit 2 1
1) "v4"
127.0.0.1:6379> ZRANGEBYSCORE z1 2 4 limit 0 1
1) "v2"
127.0.0.1:6379>
7.3 zrem/zcard/zcount
127.0.0.1:6379> ZADD z2 1 v1 2 v2 3 v3
(integer) 3
127.0.0.1:6379> ZCARD z2 # size of z2
(integer) 3
127.0.0.1:6379> ZREM z2 v2 v3 # del v2,v3
(integer) 2
127.0.0.1:6379> ZRANGE z2 0 -1
1) "v1"
# 1 <= score <=5 的元素的个数
127.0.0.1:6379> ZCOUNT z2 1 5
(integer) 1
# 获取v1的score
127.0.0.1:6379> ZSCORE z2 v1
"1"
127.0.0.1:6379>
网友评论