Redis Data Type
Strings
set k v
get k
mset k v [k v ...]
mget k [k ...]
incr k
decr k
incrby k increment
incrbyfloat k floatincrement
Hashes
场景
- 购物车
hset k f v
hget k f
mhset k f v [f v ...]
mhget k f1 [f ...]
hkeys k #return all field names in the hash stored at key
hvals k #return all the values in the hash stored at key
hdel k f [f ...]
hgetall k #return all field and values of the hash stroed at key
hincrby k f increment # increment the number stored at field in the hash stored by increment
hlen k # return the number of fields contained in the hash stored at key
hexists k f # return if field is an existing field in the hash stored at key
List
场景
- 订阅公众号
# b means beginning e means ending k means key
lrange k b e # return the specified element of the list,0 being the first element of the list
# -1 is the last element of the list 1 is the second element
# no rrange
# el means element
rpush k el [el ...]#insert all the specified value at the tail of the list
lpush l el [el ...]#insert all the specified value at the head of the list
# c means count
rpop k [c]
lpop k [c]
# i means index
lindex k i
# O(N) where N is the number of elements to traverse too get to the element at index
llen k # return the length of the list stored at k
lrem k c el # remove count elements in the list which equals el
ltrim k b e #reserve the specified element which start at b and end at e
# s means source d means destination
rpoplpush s d # rpop source an el to lpush destination destination
lset k i el # set the list element at index to element O(n)
# insert el in the list stored at k either before or after the reference
#value pivot
linsert k <BEFORE | AFTER> pivot el
Set
场景
-
微信抽奖小程序
-
微信朋友圈点赞查看同赞朋友
-
qq内推可能认识的人
# k means key m means member
sadd k m [m ...] #add the specified members to the set stored at key
smembers k # returns all the number of the set value stored at k
sismember k m [m ...] # return whether each m is a member of the set stored at k
#for every member ,1 is returned if the value is a member of the set
#0 if m is not a member of the set or if k does not exist
srem k m [m ...] # remove the specified members from the set stored at k
scard k #return the set cardinality (number of elements )of the set stored at k
srandmember k c #return count random element from the set value stored at k
spop k [c] #return and remove count random elements from the set
#smove source destination member
smove s d m
# remove member at set source to set destination
sdiff k [k ...] #return the members of the set resulting from the difference
#between the first set and all the successive sets
sunion k [k ...]#return the numbers of the set resulting form the union of all the given sets
sinter k [k ...]#return the numbers of the set resulting from the intersection of all the given sets
Sorted set
场景
- 根据商品销售对商品进行排序显示(A包含B的排序属性)
#s means score m means member
zadd s m [s m ...] #adds all the specified members with the specified scorese
#to the sorted set stored at k.if a m has already exist ,update and reinsert
# to ensure the corret ordering
zcard k # return sorted set cardinality (number of elements)
zrem k m [m ...] #remover the specified m from k
zincrby k increment m # inbcrement the score of m in k by increment
zrange k b e # return the specified range of elements in k based on score size
zrevrange k b e # according to zrange command,return remaining elements
zscore k m #return m's score in k
zrangebyscore min max [WITHSCORES] [LIMIT offset count]
#return count elements in k with the score between min and max (including ==)
zcount k min max #return number of elements in k with the score between min and max
zrank k v #return the rank of member in k
zrevrank k v #obtain reverse index
zmpop numkeys k [k ...]<MIN | MAX> {COUNT count}
# the number of k1, k2 ,k3 == numkeys
#count = the number of every k return element(member + score)
#MIN means return from head to tail ,MAX else
BitMap
场景
-
存储考勤统计
-
电影广告是否被点击过
-
用户是否登录过
-
先将用户id和日期的bitMap索引建立映射,如hash
HSET uid:map 0 uid-092iol-lkj
-
再做bitop
BITOP AND K3 20231128 20231129
-
信息
- 用String类型作为底层数据结构实现的一种统计2值状态的数据类型,本质为数组,基于String数据类型的按位的操作,数组由2进制位组成,每个2进制位都对应一个偏移量,称为索引
#o means offset,v means value k means key
setbit k o v #set the bit at offset with v
getbit k o #return the bit v at offset in k
strlen k # 扩容以字节为单位 8bit
bitcount k # return count of members which equals 1
bitop <AND | OR | XOR | NOT> destkey k [k ...]
# destkey == output k
# op on k1 k2 ...位运算
HyperLogLog
场景
-
统计某个网站的UV(Unique Visitor独立访客,客户端ip去重考虑),统计某个文章的UV
-
用户搜索网站关键词的统计
-
统计用户每天搜索不同词条个数
信息
-
去重复统计功能的计数估计算法
-
HyperLoglog用于基数统计时,输入元素的数量或者体积庞大时,计数基数的空间固定(12KB),不会存储输入元素本身,存在误差0.81%
#el means element
pfadd k [el [el ...]]#add all el into HyerLogLog data structure-->estimation of the number of unique items
pfcount k #return the approximated cardinality
pfmerge destk k [k ...] #merger all k ,and return the estimation unique items of destk
Geospatial
场景
- LBS(Location-Based Services)
信息
- 中文乱码问题
redis-cli --raw
#lo means longitude ,la meand latitude m means member k means key
geoadd k lo la m [ lo la m ...]#based on sorted set
geopos k m [m ...]#return longitude && latitude in all m in k
geohash k m [m ...]#return all m by hash
geodist k m1 m2 [M | KM | FT | MI]#return two position distance
#FT for feet MI for miles KM for kilometers M for meters
georadius k lo la radius <M | KM | FT | MI> [WITHCOORD] [WITHDIST]
[WITHHASH] [COUNT count [ANY]] [ASC | DESC]
#return count points within the radius of this point
georadiusbymember k m radius <M | KM | FT | MI> [WITHCOORD] [WITHDIST]
[WITHHASH] [COUNT count [ANY]] [ASC | DESC]
#return count points within the radius of this point