Redis’s 8 data types are well written!

Time:2021-3-5

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:http://www.redis.cn/commands….

Redis-key

127.0.0.1:6379> keys *

Redis has the following five basic data types

1. String (string)

127.0.0.1:6379 > set key 1 V1 ා set value

Self increase and self decrease

127.0.0.1:6379> set views 0

String range

127.0.0.1:6379> set key1 "hello,world!"

Replacement:

127.0.0.1:6379> set key2 abcdefg
OK
127.0.0.1:6379> get key2
"abcdefg"
127.0.0.1:6379> setrange key2 1 xx
(integer) 7
127.0.0.1:6379> get key2
"axxdefg"
127.0.0.1:6379>

Setex (set with expiration): sets the expiration time

And setnx (set if not exist): no reset (often used in distributed locks)

127.0.0.1:6379 > setex? Key3? 30 "hello" ා set to expire after 30 seconds

Mset and mget

127.0.0.1:6379 > Mset ﹣ K1 ﹣ V1 ﹣ K2 ﹣ V2 ﹣ K3 ﹣ V3 ﹣ setting multiple values at the same time

object

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

127.0.0.1:6379 > 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.

127.0.0.1:6379 > lpush list one # insert one or more values into the top of the list (left)

Pop up

127.0.0.1:6379> lrange list 0 -1

Index lindex

127.0.0.1:6379> lrange list 0 -1

Llen length:

127.0.0.1:6379> llen list
(integer) 3
127.0.0.1:6379>

Remove the specified value:

127.0.0.1:6379> lrange list 0 -1

Trim truncation

127.0.0.1:6379> lrange mylist 0 -1

Rpoplbush: remove the last element of the list and move it to the new list.

127.0.0.1:6379> lrange mylist 0 -1

Lset: replace the specified subscript value in the list with another value to update the operation

127.0.0.1:6379 > exists list # determine whether the list exists

Linsert: insert a specific value before or after an element in the list

127.0.0.1:6379> lrange mylist 0 -1
1) "hello1"
2) "hello2"
127.0.0.1:6379> linsert mylist before "hello2" hello
(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1) "hello1"
2) "hello"
3) "hello2"
127.0.0.1:6379> linsert mylist after "hello2" hello
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "hello1"
2) "hello"
3) "hello2"
4) "hello"
127.0.0.1:6379>

Summary:

  • 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

127.0.0.1:6379 > add element to set
127.0.0.1:6379 > scar? Myset # get the number in the set
127.0.0.1:6379> smembers myset
127.0.0.1:6379> smembers myset
127.0.0.1:6379> smembers myset
127.0.0.1:6379> smembers key1

4. Hash (hash)

It’s also in the form of key – value, but value is a map.

127.0.0.1:6379 > hset myhash field XXX # set a key value
127.0.0.1:6379 > HDEL myhash field1 # delete the specified key, and the corresponding value will be lost
127.0.0.1:6379 > HLEN myhash # get length
127.0.0.1:6379> 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)

127.0.0.1:6379 > add a value

Implement sorting:

127.0.0.1:6379> zadd salary 2500 xiaohong
127.0.0.1:6379> zrange salary 0 -1
127.0.0.1:6379> zadd myset 1 hello

Three special data types of redis

1、geospatial

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

file:https://www.redis.net.cn/orde…

With the help of the website to simulate some data:http://www.jsons.cn/lngcode/

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:

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

Geopos obtains the current positioning coordinate value

127.0.0.1:6379> geopos  china:city Beijing # get the longitude and latitude of the specified city

Geogist gets the distance between two positions

Company:

  • 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.

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

“1067.3788”

127.0.0.1:6379> geodist china:city beijing chongqing km

“1464.0708”

127.0.0.1:6379>`

Geordius takes the given latitude and longitude as the center to find out the elements within a certain radius

127.0.0.1:6379> 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

127.0.0.1:6379> zrange  china:city  0 -1

2、hyperloglog

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.

127.0.0.1:6379 > 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

127.0.0.1:6379 > setbit sign 0.1 # clocked in on Monday

Check whether to clock in on a certain day:

127.0.0.1:6379> GETBIT sign 3
(integer) 1
127.0.0.1:6379> GETBIT sign 6
(integer) 0
127.0.0.1:6379>

Statistics: count the number of days to punch in

127.0.0.1:6379> BITCOUNT sign
(integer) 4
127.0.0.1:6379>

Source:https://www.cnblogs.com/itzho…

Redis's 8 data types are well written!