Redis data types and commands

Redis data types and commands

Basic data type


I saved two data in advance k:v,z:c

Append string

Append a string, if it does not currently exist, it is equivalent to set

APPEND xxx value

Get string length


Automatically add 1


Automatically subtract 1


Set the step size, specify the increment


Set the step size, specify the decrement


Intercept string

GETRANGE k 0 3   #截取字符串[0,3] 

GETRANGE k 0 -1  #获得全部的字符串

Replace the string starting at the specified position


Set expiration time

setex (set with expire)
setex k 10 hello   #存入数据 k:"hello" 有效时间 10s

No more settings

setnx (set if not exist)
setnx k 11     #如果 k 不存在,存入数据 k:"11"

Set multiple values ​​at the same time

mset k1 v1 k2 v2 k3 v3

Get multiple values ​​at the same time

mget k1 k2 k3

Multiple values ​​do not exist and then set

msetnx k1 v1 k4 v4   # 该操作是一个原子性操作,一个失败 全部失败

Access object

user: {id}: {filed}
mset user:1:name zc user:1:age 20   

mget user:1:name user:1:age

Get first and then deposit

getset k v


In redis inside, we can listbecome, stacks, queues, blocking queue

Save data (head insertion)

LPUSH list one             # 存入list列表,数据为 one
LPUSH list two three       # 存入list列表,数据为 two、three

Save data (end insert)

RPUSH list right           # 存入list列表,数据为 right

Get data in a specified range

LRANGE list 0 1            # 获取list列表 0 1 数据
LRANGE list 0 -1           # 获取list列表全部数据

Fetch data (from the head)

LPOP xxx

Take out the data (from the tail)

RPOP xxx

Obtain value by subscript

lindex xxx index

Get list length

llen xxx          

Remove specified value

lrem list 2 two         # 移除list列表中 2 个 two , 移除数量多于现存数量的话,会全部移除 不报错
twenty one

Intercept data of specified length

Through the subscript, intercept the specified length data, it will directly change the original list
ltrim list 1 2         # 截取list列表,1-2 元素
twenty two

Remove elements from the list and move to a new list

rpoplpush list list1   # 移除list列表的最后一个元素,将他移动到list1列表中 
twenty three

Replace the value at the specified position

For this statement, to meet:
  • The list exists
  • There is data at the specified position in the list
lset list 0 test    #将list列表第0个位置的数据替换为test
twenty four

Insert specific value

LINSERT list before v3 v2   # 在list列表中的v3数据 前 插入v2

LINSERT list after v3 v4    # 在list列表中的v3数据 后 插入v4


The set cannot be read repeatedly

Save and get data

sadd set hello           #存入一个数据,存入hello到set集合中

sadd set v1 v2 v3        #存入多个数据,存入v1、v2、v3到set集合中

smembers set             #查看set集合中所有数据

Determine whether the value is in the set

sismember set hello    #判断hello是否在set集合中
sismember set v1       #判断v1是否在set集合中

Get the number of set collection data

scard set     

Remove the specified element of the set collection

srem set hello        #set集合中移除hello

Randomly get elements

srandmember set        #随机获取set集合1个元素

srandmember set 2      #随机获取set集合2个元素
twenty one

Randomly delete elements

spop set              #随机删除1个元素

spop set 2            #随机删除2个元素
twenty two

Move the specified value to another collection

smove set set1 v2    #将v2元素从set集合移动到set1集合
twenty three

Digital collection class

  • Subtraction
  • Intersection
  • Union
sdiff set1 set2     #set1集合和set2集合的差集(set1存在,set2不存在)
sinter set1 set2    #set1、set2集合中的交集,即两者都存在的元素
sunion set1 set2    #set1、set2并集
twenty four


Map collection, key-may, this value is a map collection, the essence is not much different from the string type, it is still a key-value
Hash is more suitable for storing objects, string is more suitable for string storage

Store an element

hset hash k v       #存入hash集合,k-v

Get an element

hget hash k         #获取hash集合 k元素对应v

Store multiple elements

hmset hash k1 v1 k2 v2   #存入hash集合 k1-v1 k2-v2

Get multiple elements

hmget hash k1 k2     #获取hash集合 k1、k2元素对应v

Get all the data

hgetall hash

Get the number of fields in the collection

hlen hash

Delete the corresponding element

hdel hash k          #删除hash集合,k字段

Get all k fields

hkeys hash

Get all value

hvals hash

Determine whether the specified field exists

hexists hash k1      #判断hash集合中k1字段是否存在

Increase specified increment

hincrby hash k 2       #hash集合k字段的v+2

hincrby hash k -1      #hash集合k字段的v-1

No more settings

hsetnx hash k4 v4     #hash集合中 k4字段不存在的话,存入k4-v4

Zset-ordered set

In the set collection, set k1 v1; it is an unordered non-repeating collection
In the zset collection, zset k1 score1 v1; it is an ordered non-repeated collection

Add the value in the collection

zadd zset 1 one          #添加zset集合元素one,顺序为1

zadd zset 2 two 3 three  #添加zset集合元素two、three,顺序为2、3

Get the set value in the range

zrange zset 0 -1         #获取全部集合值

zrange zset 0 1          #获取集合中0-1的值

Get the number in the set

zcard zset

From small to large, display all data

zrangebyscore zset -inf +inf    #从小到大,显示全部数据

zrangebyscore zset -inf 2       #从小到大,显示最小到2的数据

zrangebyscore zset -inf 2 withscores #从小到大,显示最小到2的数据并且带着排序数字

From big to small, display all data

zrevrange zset 0 -1

Get the number of elements in a specified interval

zcount zset 0 2         #获取0-2的元素数量

zcount zset 0 10        #获取0-10的元素数量,超过现存最大值,会显示全部数量,不会报错

Remove the specified element in the collection

zrem zset two           #移除zset集合中two元素

Special data types

Geospatial-geographic location

Six commands:
  • GEOADD: Add geographic location
  • GEOPOS: Get the accuracy and latitude of the specified city
  • GEODIST: View the straight-line distance between the two
  • GOERADIUS: Take the given latitude and longitude as the center, find the elements within a certain radius
  • GEORADIUSBYMEMBER: Find out other elements located around the specified element
  • GEOHASH: Returns the Geohash representation of one or more location elements
Longitude and latitude query of Chinese cities:

Add location

The two poles cannot be added directly, generally the city data will be downloaded and imported at one time through the program
The valid longitude is between -180 degrees and 180 degrees; the valid latitude is between -85.05112878 degrees and 85.05112878 degrees.
geoadd china:city 116.405285 39.904989 beijing              #加入中国城市地理位置,经、纬度,beijing

geoadd china:city 113.665412 34.757975 zhengzhou 125.14904 42.927 xian  #加入两条记录

Get the accuracy and latitude of the specified city

geopos china:city beijing

See the straight-line distance between the two

  • [ Default ] m: means the unit is meter
  • km: the unit is kilometers
  • mi: expressed in miles
  • ft: expressed in feet
geodist china:city beijing shanghai          #查看 北京到上海 的直线距离,默认单位为米

geodist china:city zhengzhou xian km         #查看 郑州到西安 的直线距离,单位为千米

Take the given latitude and longitude as the center, find the elements within a certain radius

georadius china:city 110 30 1500 km                   # 以110,30这个经纬度为中心,寻找1500km内的城市

georadius china:city 110 30 1500 km withdist          # 以110,30这个经纬度为中心,寻找1500km内的城市,并显示到中间距离的位置

georadius china:city 110 30 1500 km withdist count 2  # 以110,30这个经纬度为中心,寻找1500km内的城市,只显示两条,并显示到中间距离的位置

georadius china:city 110 30 1500 km withcoord         # 以110,30这个经纬度为中心,寻找1500km内的城市,并显示查出城市的定位信息

georadius china:city 110 30 1500 km withcoord count 2 # 以110,30这个经纬度为中心,寻找1500km内的城市,只显示两条,并显示查出城市的定位信息

View other elements around the specified element

georadiusbymember china:city beijing 1000 km     # 查看北京元素1000km以内的其他元素

georadiusbymember china:city shanghai 500 km     # 查看上海元素500km以内的其他元素

Return the Geohash representation of the position element

Convert the two-dimensional latitude and longitude into a one-dimensional string, if the two strings are closer, then the distance is closer
geohash china:city beijing shanghai xian

The underlying realization principle

The underlying implementation principle of GEO is actually Zset, we can use the Zset command to operate GEO> zrange china:city 0 -1      # 查看china:city集合中全部元素
1) "hangzhou"
2) "shanghai"
3) "zhengzhou"
4) "beijing"
5) "xian"> zrem china:city xian        # 移除china:city集合中xian元素
(integer) 1> zrem china:city shanghai    # 查看china:city集合中shanghai元素
(integer) 1> zrange china:city 0 -1      # 查看china:city集合中全部元素
1) "hangzhou"
2) "zhengzhou"
3) "beijing"

Hyperloglog-Cardinality Statistics

Base numbers can be compared
A = {1, 2, 3} = 3; B = {1, 2, 3, 4, 4} = 4
Cardinality {unique elements} = xxx
Advantages: memory occupied fixed, 2 ^ 64 (long) elements of different techniques, only 12KB of memory, from memory if the angle Hyperloglog preferred
0.81% error rate is negligible.

Create a set of elements> pfadd hype a b c d

Base number of statistical elements

pfcount hype 

Combine two sets of elements

pfmerge hype2 hype hype1           #将hype、hype1 合并成 hype2

Bitmaps-bitmap scene

Bit storage

Bitmaps bitmap, is the operation of binary bits to record, there are only two states of 0 and 1.

365 days = 365bit; 1 byte = 8bit; about 46 bytes

Simulate weekly check-in
setbit name date (0-6) Whether to check in (0 not check in, 1 check in)> setbit sign 0 1   #周一 已打卡
(integer) 0> setbit sign 1 1   #周二 已打卡
(integer) 0> setbit sign 2 0   #周三 未打卡
(integer) 0> setbit sign 3 0   #周四 未打卡
(integer) 0> setbit sign 4 1   #周五 已打卡
(integer) 0> setbit sign 5 1   #周六 已打卡
(integer) 0> setbit sign 6 1   #周日 已打卡
(integer) 0> getbit sign 0     #查询周一是否打卡
(integer) 1> getbit sign 3     #查询周四是否打卡
(integer) 0> bitcount sign     #查询这周一共打卡几天
(integer) 5
Personal blog is:
MoYu's HomePage
MoYu’s Gitee Blog