Redis’s 8 data types are well written!


Redis's 8 data types are well written!

NoSQL development will be used more or less, but also the interview must ask knowledge. I have been asked about every interview in recent days. But I feel that the answer is not good, and there are still many knowledge points that need to be sorted out.

You can view the following commands on the official website:….

Redis-key> keys *

Redis has the following five basic data types

1. String (string) > set key 1 V1 ා set value

Self increase and self decrease> set views 0

String range> set key1 "hello,world!"

Replacement:> set key2 abcdefg
OK> get key2
"abcdefg"> setrange key2 1 xx
(integer) 7> get key2

Setex (set with expiration): sets the expiration time

And setnx (set if not exist): no reset (often used in distributed locks) > setex? Key3? 30 "hello" ා set to expire after 30 seconds

Mset and mget > Mset ﹣ K1 ﹣ V1 ﹣ K2 ﹣ V2 ﹣ K3 ﹣ V3 ﹣ setting multiple values at the same time


set  user:1  { name:zhangsan ,  age:3 }Set up a user:1 The value of object is "JSON" character to save an object

GetSet: get first and then set > GetSet > DB > redis # if there is no value, it returns "nil"

Usage scenarios of string: value can be a number in addition to a string

  • Counter
  • Count the number of multiple units
  • Number of fans
  • Object cache storage

2. List (list)

Basic data type, list.

In redis, list can be used as stack, queue and blocking queue.

Most of the list commands start with L. > lpush list one # insert one or more values into the top of the list (left)

Pop up> lrange list 0 -1

Index lindex> lrange list 0 -1

Llen length:> llen list
(integer) 3>

Remove the specified value:> lrange list 0 -1

Trim truncation> lrange mylist 0 -1

Rpoplbush: remove the last element of the list and move it to the new list.> lrange mylist 0 -1

Lset: replace the specified subscript value in the list with another value to update the operation > exists list # determine whether the list exists

Linsert: insert a specific value before or after an element in the list> lrange mylist 0 -1
1) "hello1"
2) "hello2"> linsert mylist before "hello2" hello
(integer) 3> lrange mylist 0 -1
1) "hello1"
2) "hello"
3) "hello2"> linsert mylist after "hello2" hello
(integer) 4> lrange mylist 0 -1
1) "hello1"
2) "hello"
3) "hello2"
4) "hello">


  • List is actually a linked list, which can be inserted before and after
  • If the key does not exist, create a new linked list
  • If all the values are removed, an empty linked list also means that it does not exist
  • Inserting or changing values on both sides is the most efficient.

3. Set > add element to set > scar? Myset # get the number in the set> smembers myset> smembers myset> smembers myset> smembers key1

4. Hash (hash)

It’s also in the form of key – value, but value is a map. > hset myhash field XXX # set a key value > HDEL myhash field1 # delete the specified key, and the corresponding value will be lost > HLEN myhash # get length> hset myhash field3 5  

Hash is suitable for storing frequently changing object information, while string is more suitable for storing strings.

5. Zset (ordered set) > add a value

Implement sorting:> zadd salary 2500 xiaohong> zrange salary 0 -1> zadd myset 1 hello

Three special data types of redis


Redis launched the geo type in 3.2, which can calculate the geographic location information and the distance between the two places.


With the help of the website to simulate some data:

Geoadd add geographic location

Rule: you can’t add it directly. Generally, you will download the city data and import it directly through Java program.

The effective longitude is from – 180 degrees to 180 degrees. The effective latitude ranges from – 85.05112878 degrees to 85.05112878 degrees. When the coordinate position exceeds the specified range, the command will return an error.

(error) ERR invalid longitude latitude pair xxx yyy

Add some simulation data:> geoadd china:city 116.40 39.90 beijing
(integer) 1> geoadd china:city 121.47 31.23 shanghai
(integer) 1> geoadd china:city 106.50 29.53 chongqing 114.05 22.52 shengzhen
(integer) 2> geoadd china:city 120.16 30.24 hangzhou 108.96 34.26 xian
(integer) 2>

Geopos obtains the current positioning coordinate value> geopos  china:city Beijing # get the longitude and latitude of the specified city

Geogist gets the distance between two positions


  • M is in meters.
  • Km is expressed in kilometers.
  • Mi is in miles.
  • FT is in feet.

If the user does not explicitly specify the unit parameter, geodist uses meters as the unit by default.

`> geodist  china:city Beijing Shanghai km view the direct distance between Beijing and Shanghai

“1067.3788”> geodist china:city beijing chongqing km


Geordius takes the given latitude and longitude as the center to find out the elements within a certain radius> georadius  china:city 110, 30, 1000 km # take the point of 110, 30 as the center, look for a city with a radius of 1000 km

The underlying implementation principle of geo is actually Zset, which can be used to operate Geo> zrange  china:city  0 -1


Cardinal number: the number of elements in a mathematical set, which cannot be repeated.

UV (unique visitor): refers to the natural person who visits and browses this web page through the Internet. A computer client visited is a visitor, and the same visitor is counted only once a day.

Redis version 2.8.9 updates the hyperloglog data structure, which is an algorithm based on cardinality statistics.

The advantage of hyperloglog is that it occupies less memory and is fixed. It only needs 12 KB to store the cardinality of 2 ^ 64 different elements. But there may be an error rate of 0.81%.

This data structure is often used to count the UVs of websites. The traditional way is to use set to save the user’s ID, and then count the number of elements in the set as the judgment standard. However, this method saves a large number of user IDs, which are generally long, space consuming and troublesome. Our goal is to count, not to save data, so there are drawbacks to doing so. But it’s more appropriate to use hyperlog. > pfadd MyKey a B C D E F G H I j

3. Bitmap

Bitmap is to set 0 or 1 by the smallest unit bit, which indicates the value or state of an element. The value of a bit is either 0 or 1; that is to say, the maximum information that a bit can store is 2.

Bitmap is often used to count user information, such as active fans and inactive fans, login and not login, clock in or not, etc.

Here is a case of one week clock in to illustrate its usage > setbit sign 0.1 # clocked in on Monday

Check whether to clock in on a certain day:> GETBIT sign 3
(integer) 1> GETBIT sign 6
(integer) 0>

Statistics: count the number of days to punch in> BITCOUNT sign
(integer) 4>


Redis's 8 data types are well written!