Overview of Redis data types

Posted by rroc on Fri, 11 Feb 2022 19:49:51 +0100

Redis official website: https://redis.io
Redis Chinese official website: http://www.redis.cn

The following is an overview of Redis data types, which can be read intuitively by using code blocks.

1, Base point

1. There are 16 Redis databases by default, and the first one is used by default. Use select to switch databases.
2. The default port number of Redis is 6379.
3. Use flushdb and flushall to clear the data in the database.

2, Redis key

Master common redis key s.

3, Five data types

The following are some common command operations of String type, but not limited to these.


#  General operation of string content
#  set key value
#  get key
#  exists key #View expiration time
#  append key value #Additional value
#  keys * #View all key s> set name xiong
OK> get name
"xiong"> exists name
(integer) 1> append name bo
(integer) 7> get name
"xiongbo"> keys *
1) "name"> append age 18
(integer) 2> keys *
1) "age"
2) "name"

# incr key #Self plus one operation
# decr key #Self subtraction one operation
# incrby key num,#Assignment increase
# decrby key num,#Assignment reduction> set views 0
OK> get views
"0"> incr views
(integer) 1> incr views
(integer) 2> get views
"2"> decr views
(integer) 1> decr views
(integer) 0> decr views
(integer) -1> get views
"-1"> incrby step 10
(integer) 10> incrby step 5
(integer) 15> decrby step 8
(integer) 7> 

# getrange gets a range of values
# setrange replaces a range of values> set key1 "hello,world"
OK> get key1
"hello,world"> GETRANGE key1 0 3
"hell"> GETRANGE key1 1 -1
"ello,world"> set key2 abcdefg
OK> SETRANGE key2 1 xx
(integer) 7> get key2

# setex key num value #Set expiration time
# setnx key value  #If the key does not exist, the creation succeeds; otherwise, it fails> setex key3 30 hello
OK> ttl key3
(integer) 24> get key3
"hello"> setnx mykey tom 
(integer) 1> keys *
 1) "key2"
 2) "key3"
 3) "myke"> setnx mykey jack
(integer) 0> get mykey

# mset set multiple values
# mget get multiple values
# Msetnx sets multiple values, but msetnx is an atomic operation that either succeeds or fails together.
mset age1 10 age2 12 age3 14
mget age1 age2
msetnx age1 123 age6 21

# getset get first, then set
 If no value exists, return nil
 If the original value exists, get the new value


It can be used as stack, queue and blocking queue.

# lpush add element to the left
# Add elements to the right of rpush> lpush list one
(integer) 1> lpush list two
(integer) 2> lpush list three
(integer) 3> rpush list four
(integer) 4> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "four"

# lpop removes the first element of the list
# rpop removes the last element of the list> LPOP list 1
1) "three"> rpop list

# lindex obtains a value in the list by subscript> lindex list 1

# lrem removes the specified number of value s in the list set and matches them exactly
1) "three"
2) "three"
3) "two"
4) "one"> lrem list 2 three
(integer) 2> lrange list 0 -1
1) "two"
2) "one"> 

# ltrim truncates elements and retains elements in the specified range> LTRIM list  1 2

# Rpop lpush combines the command to remove the last element and add it to the new list> rpoplpush list newlist

# lset replaces the value of the specified subscript in the list with another value, and updates the operation. If it does not exist, an error is reported> lpush mylist tom age
(integer) 2> LRANGE mylist 0 -1
1) "age"
2) "tom"> lset mylist 0 jack
OK> LRANGE mylist 0 -1
1) "jack"
2) "tom"

# linsert inserts a specific value before or after an element in the list> linsert mylist before tom king
(integer) 3> LRANGE mylist 0 -1
1) "jack"
2) "king"
3) "tom"

1. List is actually a linked list, and values can be inserted on both sides
2. If the key does not exist, create a new linked list
3. If the key exists, new content will be added
If 4 in the linked list does not exist, it means that all values are removed
5. Insert or change values on both sides, the efficiency is the highest, and the relative efficiency of intermediate elements will be lower
It can be used as stack, queue and blocking queue.


# sadd adds elements to the set
# smembers view all elements in the set collection
# sismember determines whether a value exists in the set set
# scard gets the number of elements in the set set> sadd myset hello world
(integer) 2> smembers myset
1) "world"
2) "hello"> sismember myset hello
(integer) 1> sismember myset hello2
(integer) 0
1127.0.0.1:6379> scard myset
(integer) 2

#srem removes an element from the set set> srem myset hello
(integer) 1> smembers myset
1) "world"> 

# srandmember arbitrarily gets an element in the set set> srandmember myset

# spop randomly deletes some elements in the set set> spop myset

# smove moves a specified value from one set to another> smove myset myset2 xiong
(integer) 1

# For example, Weibo, the number of followers of station B and common friends.
# sdiff difference set
# sinter intersection
# sunion Union> sdiff myset myset2> sinter myset myset2> sunion myset muset2

4. Hash

The Map set is essentially no different from the String type. It is also a simple key value.

# New Hash data in hset
# hget get an element
# hgetall get all elements
# hdel delete an element> hset myhash name tom age 18
(integer) 2> hgetall muyhash
(empty array)> hgetall myhash
1) "name"
2) "tom"
3) "age"
4) "18"> hdel myhash age
(integer) 1> hgetall myhash
1) "name"
2) "tom"

# hlen gets the number of map elements> hlen myhash
(integer) 1

# hex gets whether an element in the map exists> hexists myhash name
(integer) 1

# hkeys gets all the keys in the map
# hvals gets all the value s in the map> hkeys myhash
1) "name"> hvals myhash
1) "tom"

# hincrby specifies that a variable is incremented automatically
# hsetnx can only be set if it does not exist, and cannot be set if it exists. It is the same as the setnx command in String
(integer) 1> hincrby myhash age 1
(integer) 21> hget myhash age
"21"> hsetnx myhash age 12
(integer) 0> hsetnx myhash email 192@qq.com
(integer) 1

The data changed by hash is user name and age, especially the frequently changed information such as user information. Hash is more suitable for object storage, and String is more suitable for String storage.

5. zset ordered set

# zadd add zset set element
# ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]> zadd salary 500 zhangsan
(integer) 1> zadd salary 800 lisi
(integer) 1> zadd salary 1000 wangwu
(integer) 1> zrangebyscore salary 0 20000
1) "zhangsan"
2) "lisi"
3) "wangwu"

# zrem removes an element from the zset set
# zcard gets the number of elements in the zset set> zrem salary zhangsan
(integer) 1> zcard salary
(integer) 2> 

# zcount returns the ordered set key, and the score value is between min and max
# (the default includes members whose score value is equal to min or max)> zcount salary 100 20000
(integer) 2

Case idea: set sorting, storing class grade table and salary sorting table
Ordinary message, 1, important message, 2, judgment with weight
Leaderboard application implementation, taking Top N, etc.

4, Three special data types

1. Geographical location

Six commands.

# geoadd add add geographic location> geoadd china:city 116.40 39.90 beijing
(integer) 1> geoadd china:city 121.47 31.32 shanghai
(integer) 1> geoadd china:city 106.50 29.53 chongqin 114.05 22.52 shenzhen
(integer) 2

# geopos view the longitude and dimension of a location> geopos china:city shenzhen
1) 1) "114.04999762773513794"
   2) "22.5200000879503861"

# geodist looks at the straight-line records of two positions, with units> geodist china:city chongqin beijing km

# georadius is a city with a radius of 500km, centered on the longitude and latitude of 110 and 30
# The premise is that geographic data needs to exist in redis data> georadius china:city 110 30 500 km
1) "chongqin"

# Displays the distance to the center> georadius china:city 110 30 500 km withdist
1) 1) "chongqin"
   2) "341.9374"

# Display other people's location information> georadius china:city 110 30 500 km withcoord
1) 1) "chongqin"
   2) 1) "106.49999767541885376"
      2) "29.52999957900659211"

# GEORADIUSBYMEMBER finds other elements around the specified element> GEORADIUSBYMEMBER china:city chongqin 1000 km
1) "chongqin"> GEORADIUSBYMEMBER china:city chongqin 2000 km
1) "chongqin"
2) "shenzhen"
3) "shanghai"
4) "beijing"

# GeoHash returns a GeoHash string of one or more location elements (rarely used)
# The closer two strings are, the closer they are> geohash china:city beijing shenzhen
1) "wx4fbxxfke0"
2) "ws10578st80"

The underlying implementation of geospatial is Zset. We can use Zset commands to operate geo.> zrange china:city 0 -1
1) "chongqin"
2) "shenzhen"
3) "shanghai"
4) "beijing"> zrem china:city chongqin
(integer) 1> zrange china:city 0 -1
1) "shenzhen"
2) "shanghai"
3) "beijing"> 


What is cardinality?
For non repeating elements, errors can be accepted.

Redis Hyperloglog cardinality statistics algorithm!
Advantages: the memory occupied is fixed. The cardinality of 2 ^ 64 different elements only needs 12KB of memory. If you want to compare from a memory perspective, Hyperloglog is the first choice.
Application scenario: for example, a website counts the number of visitors, and a person visits multiple times only once.
At first, set may be used to save the user's id and count the number of elements in set as the standard judgment.
However, this will store a large number of user IDs and occupy more space.
At this time, you can use the Hyperloglog of redis.
But Hyperloglog has an error rate of 0.81%. Statistical UV tasks can be ignored.

# pfadd add element> pfadd mykey a b c d e f g h i j
(integer) 1> pfadd mykey2 i j k n m 
(integer) 1> pfcount mykey2
(integer) 5> pfadd mykey3 i j i j k n m 
(integer) 1> pfcount mykey3
(integer) 5> pfmerge mykey4 mykey mykey2
OK> pfcount mykey4
(integer) 13> 

If fault tolerance is allowed, you can use Hyperloglog.
If fault tolerance is not allowed, use your own set or data type.


Bit storage.
Bitmaps bitmap data structures are recorded by operating binary bits. There are only two states: 0 and 1.
The occupied space is very small, such as clock in, 365 days = 365 bit, 1 byte = 8 bit, about 46 bytes.

Application scenarios, such as clock in.

# setbit key offset value sets the value. Offset is the subscript and value is 0 or 1
# getbit key offset
# bitcount key counts the number of bit s whose string is set to 1> setbit sign 0 1
(integer) 0> setbit sign 1 1
(integer) 0> setbit sign 2 0
(integer) 0> setbit sign 3 1
(integer) 0> setbit sign 4 0
(integer) 0> getbit sign 3
(integer) 1> bitcount sign
(integer) 3> 

Topics: Java Redis Cache