Redis資料結構類型介紹,redis資料結構類型

來源:互聯網
上載者:User

Redis資料結構類型介紹,redis資料結構類型
-------------------------------------------Redis資料結構類型-------------------------------------------------------------------
一、string類型
1.設定值
set key value (如果key存在:key的類型是string,那麼對值進行覆蓋;key的類型不是string,報錯;如果key不存在,會建立key並賦值value)


2.擷取值
get key (如果key存在,key的類型不是string會報錯,是string類型的返回對應的值;如果key不存在返回nil)


3.刪除值
del key


4.數值增減
遞增、遞減(key必須是數位,key不存在,建立一個key並賦預設值0,然後進行遞增、遞減)
遞增:incr key
遞減:decr key


增減指定數值
增:incrby key count
減:decrby key count


字串拼接
append key value


二、hash類型(它被string類型+json資料格式所取代)
1.設定值
設定單個值
hset key filed value
設定多個值
hmset key filed1 value1 filed2 value2 ……


2.擷取值
擷取單個值
hget key filed
擷取多個值
hmget key filed1 filed2……
擷取所有值
hgetall key


3.刪除值
刪除指定屬性值
hdel key filed
刪除所有
del key




三、list類型
1.設定值:
從左側添加資料:
lpush key value1 value2 value3 ……
執行指令:lpush mylist a b c d
結果:d c b a


0 12 3 4 5 67
d cb a


從右側添加資料:
rpush key value1 value2 value3 ……
執行指令:rpush mylist 1 2 3 4
結果:d c b a 1 2 3 4

-8 -7-6 -5 -4 -3 -2-1
0 12 3 4 5 67
d cb a 1 2 34




2.擷取值:
lrange key startIndex endIndex
執行指令:
lrange mylist 0 7
結果:d c b a 1 2 3 4


擷取所有的資料:lrange key 0 -1




3.刪除值:
從左側刪除值:(key存在,刪除最左側的一個元素;不存在返回nil)
lpop key


從右側刪除值:(key存在,刪除最右側的一個元素;不存在返回nil)
rpop key


刪除所有:
del key


4.擴充指令:
lpushx key value  (一次只能從左側添加一個資料,key存在才能添加成功)
rpushx key value  (一次只能從右側添加一個資料,key存在才能添加成功)

lrem key count value (刪除指定個數<count>的元素value)
初始化資料:
rpush mylist a b c d 1 2 3 a c d 1 c 2 a b 1 2


執行指令:(從左側刪除2個c)
lrem mylist 2 c
結果;
a b d 1 2 3 a d 1 c 2 a b 1 2


再次執行指令:(從右側刪除2個1)
lrem mylist -2 1
結果:
a b d 1 2 3 a d c 2 a b 2

再次執行指令:
lrem mylist 0 a
結果:
b d 1 2 3 d c 2 b 2




lset key index value(將制定索引位置index的元素替換為value)
初始化資料:b d 1 2 3 d c 2 b 2
執行指令:
lset mylist 3 k
結果:
b d 1 k 3 d c 2 b 2


linsert key before|after pivot value (在pivot的前面或者後面插入元素value)
初始化資料:b d 1 k 3 d c 2 b 2
執行指令:
linsert mylist before d m
結果:
b m d 1 k 3 d c 2 b 2


rpoplpush key1 key2 (將key1右側的元素刪除,添加到key2的左側)
初始化資料:
mylist a b c d 1 2 3 4
mylist1 
執行指令:
rpoplpush mylist mylist1
結果:
mylist: a b c d 1 2 3
mylist1:4

四、set類型
1.設定值
sadd key value1 vlaue2 value3……
執行指令:
sadd myset a b c d 1 a c
結果:
a b c d 1 


2.擷取值                  
smemebers key
執行指令:
smembers myset
結果:
a b c d 1


3.刪除值:
刪除指定的元素:
srem key value1 value2……
執行指令:
srem myset 1
結果:
a b c d


刪除所有:
del key


4.判斷某個元素是否存在
sismemebr key value
執行指令:
sismember myset a  結果:1
sismember myset k  結果:0


5.彙總操作_差集
sdiff key1 key2
初始化資料:
myset: a b 1 2
myset1: a c 1 3
執行指令:
sdiff myset myset1  結果:2 b
sdiff myset1 myset  結果:3 c


規律:最終顯示(key1中存在,但是不在key2中存在的資料)




6.彙總操作_交集
sinter key1 key2
初始化資料:
myset: a b 1 2
myset1: a c 1 3
執行指令:
sinter myset myset1  結果:1 a
sinter myset1 myset  結果:1 a


規律:最終顯示(key1和key2都有的資料)




7.彙總操作_並集
sunion key1 key2
初始化資料:
myset: a b 1 2
myset1: a c 1 3
執行指令:
sunion myset myset1  結果:a b c 1 2 3
sunion myset1 myset  結果:a b c 1 2 3


規律:最終顯示(key1+key2-重複元素   )


五、sortedset類型
1.設定值(member1、member2、member3不允許重複;score1、score2、score3可以重複)
zadd key score1 member1 score2 member2 score3 member3 ……
執行指令:
zadd mysort 30 zhangan 24 lisi  35 wagnwu  76 zhaoliu
結果:
lisi zhagnsan wangwu zhaoliu




2.擷取值
擷取指定成員的分數
zscore key member
執行指令:
zscore mysort zhaoliu
結果:
76


擷取所有成員的個數
zcard key
執行指令:
zcard mysort
結果:
4


擷取指定的成員(範圍)預設是按照分數從低到高
zrange key startIndex endIndex [withscores]
執行指令:
zrange mysort 0 -1
結果:
lisi zhagnsan wangwu zhaoliu


從高到低
zrevrange key startIndex endIndex [withscores]            
執行指令:
zrevrange mysort 0 -1
結果:
zhaoliu wangwu zhangsan lisi


3.刪除值
刪除指定的成員
zrem key member1 member2 ……


刪除所有
del key


4.擴充指令
zremrangebyrank key startIndex endIndex(按照分數從低到高排序,然後刪除指定位置的資料)
初始化資料:(顯示:30 zhangsan 44 lisi 56 zhaoliu 65 wangwu 79 fenjiu 87 tianqi 98 houba)
zadd mysort 30 zhangsan 44 lisi 65 wangwu 56 zhaoliu 87 tianqi 98 houba 79 fenjiu
執行指令:
zremrangebyrank mysort 2 4
結果:
zhangsan lisi tianqi houba




zremrangebyscore key min max(刪除指定分數之間的資料,包括指定的分數)
初始化資料:(顯示:30 zhangsan 44 lisi 56 zhaoliu 65 wangwu 79 fenjiu 87 tianqi 98 houba)
zadd mysort 30 zhangsan 44 lisi 65 wangwu 56 zhaoliu 87 tianqi 98 houba 79 fenjiu
執行指令:
zremrangebyscore mysort 48 87
結果:
zhangsan lisi houba


zrangebyscore key min max [withscores] [limit offset count]
返回分數在[min,max]的成員並按照分數從低到高排序


初始化資料:(顯示:30 zhangsan 44 lisi 56 zhaoliu 65 wangwu 79 fenjiu 87 tianqi 98 houba)
zadd mysort 30 zhangsan 44 lisi 65 wangwu 56 zhaoliu 87 tianqi 98 houba 79 fenjiu
執行指令:
zrangebyscore mysort 44 88 withscores limit 2 2  【擷取44到88之間從索引號為2的位置的2個元素】
結果:
wangwu fenjiu 著作權聲明:本文為博主原創文章,未經博主允許不得轉載。 http://blog.csdn.net/qq_41507845/article/details/79034837

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.