频道栏目
首页 > 数据库 > 其他综合 > 正文
Redis常用五大数据类型解析
2018-08-13 16:06:55           
收藏   我要投稿

1.String(字符串)

string是redis最基本的类型,一个key对应一个value string类型是二进制安全的,意思是redis的string可以包含任何数据,如jpg图片或者序列化的对象。 string类型是redis最基本的数据类型,一个redis中字符串value最大允许512m

常见指令

1 set/get/del/append/strlen
set:设置键值 get:由键得到值 del:删除键值 append:为对应键的值后面添加数据 strlen:由键得到值的长度
127.0.0.1:6379> set k1 k1
OK
127.0.0.1:6379> get k1
"k1"
127.0.0.1:6379> get k1
"k1"
127.0.0.1:6379> key *
(error) ERR unknown command 'key'
127.0.0.1:6379> KEYS *
1) "k1"
127.0.0.1:6379> del k1
(integer) 1
127.0.0.1:6379> KEYS *
(empty list or set)
127.0.0.1:6379> set k1 123
OK
127.0.0.1:6379> append k1 456
(integer) 6
127.0.0.1:6379> get k1
"123456"
127.0.0.1:6379> strlen k1
(integer) 6
2 incr/decr/incrby/decrby
incr:值加一 decr:值减一 incrby:值增加指定大小 decrby:值减少指定大小
127.0.0.1:6379> set k2 1
OK
127.0.0.1:6379> INCR k2
(integer) 2
127.0.0.1:6379> INCR k2
(integer) 3
127.0.0.1:6379> INCR k2
(integer) 4
127.0.0.1:6379> DECR k2
(integer) 3
127.0.0.1:6379> DECR k2
(integer) 2
127.0.0.1:6379> DECR k2
(integer) 1
127.0.0.1:6379> INCRBY k2 3
(integer) 4
127.0.0.1:6379> INCRBY k2 3
(integer) 7
127.0.0.1:6379> INCRBY k2 3
(integer) 10
127.0.0.1:6379> INCRBY k2 3
(integer) 13
127.0.0.1:6379> DECRBY k2 2
(integer) 11
127.0.0.1:6379> DECRBY k2 2
(integer) 9
127.0.0.1:6379> DECRBY k2 2
(integer) 7
3 getrange/setrange
getrange:由键得到指定范围内的值 setrange:从指定开始,覆盖设置值
127.0.0.1:6379> set k3 123456789
OK
127.0.0.1:6379> get k3
"123456789"
127.0.0.1:6379> GETRANGE k3 0 -1
"123456789"
127.0.0.1:6379> GETRANGE k3 0 5
"123456"
127.0.0.1:6379> GETRANGE k3 3 7
"45678"
127.0.0.1:6379> set k3 0 xxx
(error) ERR syntax error
127.0.0.1:6379> SETRANGE k3 0 xxx
(integer) 9
127.0.0.1:6379> get k3
"xxx456789"
4 setex(set with expire)/setnx(set if not exist)
setex:设置键值,并指定存活时间 setnx:提供键存在返回0,并不设置键值;若键不存在返回1,并设置键值
127.0.0.1:6379> SETEX k4 10 v4
OK
127.0.0.1:6379> get k4
"v4"
127.0.0.1:6379> ttl k4
(integer) 0
127.0.0.1:6379> ttl k4
(integer) -2
127.0.0.1:6379> get 4k
(nil)
127.0.0.1:6379> get k4
(nil)
127.0.0.1:6379> setnx k3 v33
(integer) 0
127.0.0.1:6379> get k3
"xxx456789"
127.0.0.1:6379> SETNX k5 v5
(integer) 1
127.0.0.1:6379> get k5
"v5"
5 mset/mget/msetnx
mset:成批设置键值 mget:成批获得值 msetnx:成批查重设置键值
127.0.0.1:6379> mset kk1 vv1 kk2 vv2 kk3 vv3
OK
127.0.0.1:6379> mget kk1 kk2 kk3
1) "vv1"
2) "vv2"
3) "vv3"
127.0.0.1:6379> MSETNX kk1 vvv1 kk2 vvv2
(integer) 0
127.0.0.1:6379> MSETNX kk1 vvv1 kk4 vv4
(integer) 0
127.0.0.1:6379> MSETNX kk4 vv4 kk5 vv5
(integer) 1
6 getset
getset:先get再set,返回原来的值,没有返回nil,并覆盖设置新的值
127.0.0.1:6379> getset k6 v6
(nil)
127.0.0.1:6379> get k7
(nil)
127.0.0.1:6379> getset k7 v7
(nil)
127.0.0.1:6379> get k7
"v7"
127.0.0.1:6379> getset k7 v77
"v7"
127.0.0.1:6379> get k7
"v77"

2.Hash(哈希,类似java里的map)

redis hash是一个键值对集合 redis hash是一个string类型的field和value的映射表,hash特别适合用于储存对象。 类似java里面的map

常用指令

1 hset/hget/hmset/hmget/hgetall/hdel

hset key key1 value:设置一个Hash,是一个键值对,键为key;值又是一个键值对,键为key1,值为value
hget key key1:获得一个值。
hmset key key1 value1 key2 value2:设置一个Hash,键值对的键为key;值为多个键值对,键为key*,值为value*。
hmget:由主键与多个二级主键获得多个值。
hgetall:由主键获得所有的二级主键与对应的值。
hdel key:删除一个二级键值对。

127.0.0.1:6379> hset user id 11
(integer) 1
127.0.0.1:6379> hget user id
"11"
127.0.0.1:6379> hset user name lolo
(integer) 1
127.0.0.1:6379> hget user name
"lolo"
127.0.0.1:6379> hmset customer id 11 name linda age 12
OK
127.0.0.1:6379> hmget customer id name age
1) "11"
2) "linda"
3) "12"
127.0.0.1:6379> HGETALL customer
1) "id"
2) "11"
3) "name"
4) "linda"
5) "age"
6) "12"
127.0.0.1:6379> HDEL customer age
(integer) 1
127.0.0.1:6379> HGETALL customer
1) "id"
2) "11"
3) "name"
4) "linda"
2 hlen key 由主键获得二级键值对的个数
127.0.0.1:6379> HGETALL customer
1) "id"
2) "11"
3) "name"
4) "linda"
127.0.0.1:6379> hlen customer
(integer) 2
3 hexists key 检测是否存在主键为key的hash
127.0.0.1:6379> HGETALL customer
1) "id"
2) "11"
3) "name"
4) "linda"
127.0.0.1:6379> HEXISTS customer id
(integer) 1
127.0.0.1:6379> HEXISTS customer ids
(integer) 0
4 hkeys/hvals
hkeys key:由主键获得二级键值对的所有键。 hvals key:由主键获得二级键值对的所有值。
127.0.0.1:6379> HGETALL customer
1) "id"
2) "11"
3) "name"
4) "linda"
127.0.0.1:6379> HEXISTS customer id
(integer) 1
127.0.0.1:6379> HEXISTS customer ids
(integer) 0
5 hincrby/hincrbyfloat
hincrby key key1 value:主键加二级主键对应的值加value hincrbyfloat key key1 value:主键加二级主键对应的值加value(可以是小数)
127.0.0.1:6379> HGETALL customer
1) "id"
2) "11"
3) "name"
4) "linda"
5) "age"
6) "3"
127.0.0.1:6379> HINCRBY customer age 3
(integer) 6
127.0.0.1:6379> 
127.0.0.1:6379> HINCRBY customer age 3
(integer) 9
127.0.0.1:6379> 
127.0.0.1:6379> HINCRBY customer age 3
(integer) 12
127.0.0.1:6379> HINCRBYFLOAT customer age 2.5
"14.5"
127.0.0.1:6379> HINCRBYFLOAT customer age 2.5
"17"
127.0.0.1:6379> HINCRBYFLOAT customer age 2.5
"19.5"
6 hsetnx key key1 value:在key主键的值中增添一个新的键值对若二级主键key1已经存在,不会设置上去,不存在就设置上去
127.0.0.1:6379> HGETALL customer
1) "id"
2) "11"
3) "name"
4) "linda"
5) "age"
6) "19.5"
127.0.0.1:6379> HSETNX customer age 11
(integer) 0
127.0.0.1:6379> HSETNX customer email 111@yyyu.com
(integer) 1
127.0.0.1:6379> HGETALL customer
1) "id"
2) "11"
3) "name"
4) "linda"
5) "age"
6) "19.5"
7) "email"
8) "111@yyyu.com"

3.List(列表)

redis列表是简单的字符串列表,按照插入顺序排序。可以添加一个元素列表的头部(左边)或者尾部(右边)。它的底层实际是个链表。

常见指令

1 lpush/rpush/lrange
lpush:后面的数从左到右依次压栈。 rpush:后面的数从右到左依次压栈。 lrange:从栈顶到栈底依次遍历。
127.0.0.1:6379> LPUSH list1 1 2 3 4 5
(integer) 5
127.0.0.1:6379> LRANGE list1 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> RPUSH list2 1 2 3 4 5 
(integer) 5
127.0.0.1:6379> LRANGE list2 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
2 lpop/rpop
lpop:从栈顶出一个数 rpop:从栈底出一个数
127.0.0.1:6379> LPOP list1
"5"
127.0.0.1:6379> lpop list2
"1"
127.0.0.1:6379> LRANGE list1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> LRANGE list2 0 -1
1) "2"
2) "3"
3) "4"
4) "5"
127.0.0.1:6379> RPOP list1
"1"
127.0.0.1:6379> RPOP list2
"5"
127.0.0.1:6379> LRANGE list 0 -1
(empty list or set)
127.0.0.1:6379> LRANGE list1 0 -1
1) "4"
2) "3"
3) "2"
127.0.0.1:6379> LRANGE list2 0 -1
1) "2"
2) "3"
3) "4"
3 lindex 按照索引取数据,索引从0开始
127.0.0.1:6379> LRANGE list1 0 -1
1) "4"
2) "3"
3) "2"
4) "1"
127.0.0.1:6379> LINDEX list1 3
(nil)
127.0.0.1:6379> LINDEX list1 2
"2"
4 llen 获取list的长度
127.0.0.1:6379> LRANGE list1 0 -1
1) "4"
2) "3"
3) "2"
127.0.0.1:6379> LLEN list1
(integer) 3
5 lrem key n m,删除key中n个m
127.0.0.1:6379> LPUSH list3 1 1 1 2 2 2 3 3 3 4 4 4 5 6 6 7
(integer) 16
127.0.0.1:6379> LREM list3 2 4
(integer) 2
127.0.0.1:6379> LRANGE list3 0 -1
 1) "7"
 2) "6"
 3) "6"
 4) "5"
 5) "4"
 6) "3"
 7) "3"
 8) "3"
 9) "2"
10) "2"
11) "2"
12) "1"
13) "1"
14) "1"
6 itrim key n m,截取key索引从n到m,并保存到key中
127.0.0.1:6379> RPUSH list4 1 2 3 4 5 6 7 8 9 
(integer) 9
127.0.0.1:6379> LTRIM list4 0 5
OK
127.0.0.1:6379> LRANGE list4 0 -1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
127.0.0.1:6379> LTRIM list4 2 4
OK
127.0.0.1:6379> LRANGE list4 0 -1
1) "3"
2) "4"
7 rpoplpush key1 key2,将key栈底的数取出压栈到key2中
127.0.0.1:6379> LRANGE list1 0 -1
1) "4"
2) "3"
3) "2"
127.0.0.1:6379> LRANGE list2 0 -1
1) "2"
2) "3"
3) "4"
127.0.0.1:6379> RPOPLPUSH list1 list2
"2"
127.0.0.1:6379> LRANGE list1 0 -1
1) "4"
2) "3"
127.0.0.1:6379> LRANGE list2 0 -1
1) "2"
2) "2"
3) "3"
4) "4"
8 lset key index value,将key中索引的第index个替换成value
127.0.0.1:6379> LRANGE list2 0 -1
1) "2"
2) "2"
3) "3"
4) "4"
127.0.0.1:6379> LSET list2 2 10
OK
127.0.0.1:6379> LRANGE list2 0 -1
1) "2"
2) "2"
3) "10"
4) "4"
9 linsert key before/after value1 value2,在value1的前/后插入value2
127.0.0.1:6379> LINSERT list2 before 10 java
(integer) 5
127.0.0.1:6379> LINSERT list2 after 10 c++
(integer) 6
127.0.0.1:6379> LRANGE list2 0 -1
1) "2"
2) "2"
3) "java"
4) "10"
5) "c++"
6) "4"

4.Set(集合)

redis的set是string类型的无序集合,它是通过HashTable实现的

常见指令

1 sadd/smembers/sismember/del
sadd:新建一个set,重复的数据默认只添加一次 smembers:查看集合的数据 sismember:查看某个元素是否在集合中 del:删除一个集合
27.0.0.1:6379> SADD set 1 1 2 2 3 3 
(integer) 3
127.0.0.1:6379> SMEMBERS set
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> SISMEMBER 1
127.0.0.1:6379> SISMEMBER set 1
(integer) 1
127.0.0.1:6379> SISMEMBER set 4
(integer) 0
127.0.0.1:6379> del set
(integer) 1
2 scard 获得集合的元素的个数
127.0.0.1:6379> SCARD set
(integer) 3
3 srem key value 删除集合key中的元素value
127.0.0.1:6379> SMEMBERS set
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> srem set 2
(integer) 1
127.0.0.1:6379> SMEMBERS set
1) "1"
2) "3"
4 srandmember key value 从集合key中随机出value个元素,不写value默认出一个
127.0.0.1:6379> SADD set1 1 2 3 4 5 6 7 8 9
(integer) 9
127.0.0.1:6379> SRANDMEMBER set1
"9"
127.0.0.1:6379> SRANDMEMBER set1 5
1) "6"
2) "8"
3) "5"
4) "3"
5) "2"
5 spop key value 从集合key中随机出栈value个元素,不写value默认出栈一个
127.0.0.1:6379> SMEMBERS set1
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
7) "7"
8) "8"
9) "9"
127.0.0.1:6379> SPOP set1 4
1) "9"
2) "1"
3) "4"
4) "7"
127.0.0.1:6379> 
127.0.0.1:6379> SPOP set1 
"8"
6 smove set1 set2 value 将set1中的元素value移动到set2中
127.0.0.1:6379> SADD set2 1 2 3
(integer) 3
127.0.0.1:6379> SADD set3 x y z
(integer) 3
127.0.0.1:6379> SMOVE set2 set3 2
(integer) 1
127.0.0.1:6379> SMEMBERS set2
1) "1"
2) "3"
127.0.0.1:6379> SMEMBERS set3
1) "y"
2) "x"
3) "2"
4) "z"
7 sdiff/sinter/sunion
sdiff set1 set2:差集,取set1中有set2中没有的元素 sinter set1 set2:两集合的交集 sunion set1 set2:两集合的并集
127.0.0.1:6379> SADD set1 1 2 3 4 5 6
(integer) 6
127.0.0.1:6379> SADD set2 2 5 6 u t i
(integer) 6
127.0.0.1:6379> SDIFF set1 set2
1) "1"
2) "3"
3) "4"
127.0.0.1:6379> SDIFF set2 set1
1) "t"
2) "i"
3) "u"
127.0.0.1:6379> SINTER set1 set2
1) "2"
2) "5"
3) "6"
127.0.0.1:6379> SUNION set1 set2
1) "5"
2) "4"
3) "1"
4) "t"
5) "3"
6) "6"
7) "2"
8) "u"
9) "i"

5.Zset(sorted set:有序集合)

redis zset和set一样也是string类型元素的集合,且不允许重复的成员 不同的是每个元素都会关联一个double类型的分数 redis正是通过分数来为集合中的成员进行从小到大的排序,zset的成员是唯一的,但分数却可以重复。

常见指令

1 zadd/zrange/zrevrange
zadd key score1 value1 score2 value2:添加一组zset。 zrange key:获得set的值情况,如果带后缀withscores,会连值和分数一起打印。 zrevrange key:逆序获得set的值情况,如果带后缀withscores,会连值和分数一起打印。
127.0.0.1:6379> ZADD zset1 50 v1 60 v2 70 v3 80 v4 90 v5
(integer) 5
127.0.0.1:6379> ZRANGE zset1 0 -1
1) "v1"
2) "v2"
3) "v3"
4) "v4"
5) "v5"
127.0.0.1:6379> ZRANGE zset1 0 -1 withscores
 1) "v1"
 2) "50"
 3) "v2"
 4) "60"
 5) "v3"
 6) "70"
 7) "v4"
 8) "80"
 9) "v5"
10) "90"
127.0.0.1:6379> ZREVRANGE zset1 0 -1 withscores
 1) "v5"
 2) "90"
 3) "v4"
 4) "80"
 5) "v3"
 6) "70"
 7) "v2"
 8) "60"
 9) "v1"
10) "50"
2 zrangebyscore/zrevrangebyscore
zrangebyscore key score1 score2(score1score2) :倒叙查找分数在score1-score2之间的所有值 注:在score前加(表示这个分数范围内不包含score这个值 注:limit value1 value2表示查出的数据从下标value1开始打印value2个
127.0.0.1:6379> ZRANGE zset1 0 -1 withscores
 1) "v1"
 2) "50"
 3) "v2"
 4) "60"
 5) "v3"
 6) "70"
 7) "v4"
 8) "80"
 9) "v5"
10) "90"
127.0.0.1:6379> ZRANGEBYSCORE zset1 60 80
1) "v2"
2) "v3"
3) "v4"
127.0.0.1:6379> ZREVRANGEBYSCORE zset1 90 60
1) "v5"
2) "v4"
3) "v3"
4) "v2"
127.0.0.1:6379> ZRANGEBYSCORE zset1 60 (80
1) "v2"
2) "v3"
127.0.0.1:6379> ZRANGEBYSCORE zset1 (60 (80
1) "v3"
127.0.0.1:6379> ZRANGEBYSCORE zset1 60 80 limit 1 2
1) "v3"
2) "v4"
3 zrem key value:删除元素
127.0.0.1:6379> ZRANGE zset1 0 -1 withscores
 1) "v1"
 2) "50"
 3) "v2"
 4) "60"
 5) "v3"
 6) "70"
 7) "v4"
 8) "80"
 9) "v5"
10) "90"
127.0.0.1:6379> zrem zset1 v4
(integer) 1
127.0.0.1:6379> ZRANGE zset1 0 -1 withscores
1) "v1"
2) "50"
3) "v2"
4) "60"
5) "v3"
6) "70"
7) "v5"
8) "90"
4 zcard/zcount/zrank/zscore
zcard key:获得key有序集合的值个数。 zcount key score区间:获得key有序集合在score区间内的值个数。 zrank key value:获取key有序集合中value对应的下标。 zscore key value:获取key有序集合中value对应的分数。
127.0.0.1:6379> ZRANGE zset1 0 -1 withscores
1) "v1"
2) "50"
3) "v2"
4) "60"
5) "v3"
6) "70"
7) "v5"
8) "90"
127.0.0.1:6379> zcard zset1
(integer) 4
127.0.0.1:6379> ZCOUNT zset1 60 90
(integer) 3
127.0.0.1:6379> zrank zset1 v3
(integer) 2
127.0.0.1:6379> zscore zset1 v3
"70"

附:key(键)

常见指令

1.(keys 正则匹配)查找对应情况的键值。
127.0.0.1:6379> KEYS *
1) "k1"
2) "key:__rand_int__"
3) "counter:__rand_int__"
4) "myset:__rand_int__"
5) "mylist"
127.0.0.1:6379> set k2 hello
OK
127.0.0.1:6379> set k3 hello
OK
127.0.0.1:6379> KEYS k
1) "k3"
2) "k1"
3) "k2"
2.(exists key)判断key这个件在库中是否存在,0表示不存在,1表示存在。
27.0.0.1:6379> set k1 k1
OK
127.0.0.1:6379> set k2 k2
OK
127.0.0.1:6379> KEYS *
1) "k1"
2) "k2"
127.0.0.1:6379> EXISTS k1
(integer) 1
127.0.0.1:6379> EXISTS k3
(integer) 0
3.(move key db数)将键从当前库移动到对应库中。
27.0.0.1:6379> move k2 2
(integer) 1
127.0.0.1:6379> EXISTS k2
(integer) 0
127.0.0.1:6379> SELECT 2
OK
127.0.0.1:6379[2]> EXISTS k2
(integer) 1
127.0.0.1:6379[2]> select 0
OK
127.0.0.1:6379> KEYS *
1) "k1"
4.(ttl key)查看键还有多少秒过期,-1表示永不过期,-2表示已过期。
5.(expire key 秒钟)为键设置过期时间
27.0.0.1:6379> TTL k1
(integer) -1
127.0.0.1:6379> EXPIRE k1 10
(integer) 1
127.0.0.1:6379> ttl k1
(integer) 6
127.0.0.1:6379> ttl k2
(integer) -2
127.0.0.1:6379> ttl k1
(integer) -2
127.0.0.1:6379> get k1
(nil)
127.0.0.1:6379> KEYS *
(empty list or set)
6.(type key)查看键是对应的值是什么类型
127.0.0.1:6379> set k3 k3
OK
127.0.0.1:6379> type k3
string
127.0.0.1:6379> LPUSH mylist 1 2 3 4 5
(integer) 5
127.0.0.1:6379> LRANGE mylist 0 -1
1) "5"
2) "4"
3) "3"
4) "2"
5) "1"
127.0.0.1:6379> TYPE mylist
list
附:对一个键重新赋值,原值会被覆盖
127.0.0.1:6379> set k4 k4
OK
127.0.0.1:6379> set k4 v4
OK
127.0.0.1:6379> get k4
"v4"
点击复制链接 与好友分享!回本站首页
上一篇:PostgreSQL之允许远程访问的设置方法
下一篇:SQL学习之备份数据库
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站