This article gives you a thorough understanding of Redis basics, the most comprehensive in history [recommended for newbies]

1. Introduction to Redis

Introduction to redis (Remote dictionary Server remote dictionary server):

It is completely open source and free, written in C language, a high-performance (key/value) distributed memory database, running on memory, and supporting persistent Nosql database, also called "data structure server".

Spoilers ahead of time, redis' three key-value features:

  1. Redis supports persistence! We can control when and how to save the data to the disk, and load the file again every time it restarts to complete data recovery
  2. Redis not only supports simple key-value data types, but also provides storage for list, set, hash, zset and other data types
  3. Support data backup, mater-slave mode, that is, the current fashionable master-slave replication read-write separation

Application scenarios:

  • Persistence of memory storage: Although redis is implemented in a single thread, it supports asynchronously persisting data to the hard disk without affecting the continued service publication and subscription
  • Timer, timer: We can use this feature in the service of sending text messages. After each text message is sent, it will enter a countdown. Within the specified time, we will never send the second time, which effectively relieves the pressure on the SMS service. Throttling
  • The operation of extracting the latest N data: For example, the comment system of Sina Weibo, he wants to display the latest 10 comments, which is to put the IDs of the latest 10 comments in the redis list

Two, installation

Go to the official website to download the compressed package : https://redis.io/download

Install dependencies, compile and install:

[root@instance-lynj0v9k-19 redis-5.0.9]# yum install gcc-c++
[root@instance-lynj0v9k-19 redis-5.0.9]# make
[root@instance-lynj0v9k-19 redis-5.0.9]# make install

The programs installed by ourselves are all in the /usr/local/bin directory by default

Three, start redis

Copy the redis configuration file to the /usr/local/bin directory, and then use this configuration file to start

Modify the configuration file to start redis in the background

daemonize no # 修改成 yes

Specify the configuration file, start redis

Use redis-client to connect to redis

drop out

# 关闭redis-server
127.0.0.1:16379> shutdown

# 退出redis-cli
not connected> exit

# 重新尝试连接,因为redis-server被关闭了,所以会被拒绝
[root@instance-lynj0v9k-19 bin]# redis-cli -p 16379
Could not connect to Redis at 127.0.0.1:16379: Connection refused
not connected> exit

# 查看redis-server是否还在
[root@instance-lynj0v9k-19 bin]# netstat -luntp | grep 16379
[root@instance-lynj0v9k-19 bin]#

Four, common basic commands

# redis默认存在16个数据库,默认一开始使用的是第0个
[root@instance-lynj0v9k-19 bin]# cat redis.conf | grep  'databases'
databases 16

# 切换数据库
127.0.0.1:16379> select 2
OK
127.0.0.1:16379[2]>

# 查看数据库容量
127.0.0.1:16379[2]> DBSIZE
(integer) 0

# 查看所有的key
127.0.0.1:16379[2]> keys *
(empty list or set)

# 清空当前数据库中的值
127.0.0.1:16379[2]> flushdb
OK

# 清空所有库
127.0.0.1:16379[2]> flushall
OK

Insert picture description here
Insert picture description here

Five, Redis five basic data types

5.1、String

It is equivalent to Map<String,String> in java

  • String is the most basic data type in redis, a key corresponds to a value
  • The string is binary safe, which means that even if we set the image or serialized object to redis through the encryption algorithm, it helps us safely store the string with a maximum memory value of 512M

Common commands:
Insert picture description here

5.2、Hash

Equivalent to in java: Map<String,Map<String,String>>

  • The hash of redis is a collection of key-value pairs Map(string, Object)
  • The hash of redis is a mapping table of field and value of string type, which is especially suitable for storing objects

Similarly, hash-related instructions start with h
Insert picture description here

5.3、List

Equivalent to Map<String,List> in java

Redis's list is a simple list of string types. From a functional point of view, it is like a product of the marriage of a stack and a queue.

First of all: it will be sorted in the order we inserted, and then we can add/get elements from its head, or add/get elements from its tail, (the bottom layer is actually a linked list)

There are left and right distinctions in the list that are easier to confuse. I think of the entire list as a tube with the same two sides. If it is an operation that starts with L, I imagine using the left hand to erect the tube (I named it Left Press Stack), If it is the beginning of R, imagine that the pipe is erected with the right hand, so that it will not be confused with the
list-related instructions of the value that is taken out . The beginning of all is l, which means that the list
Insert picture description here
performance summary:

He is a string linked list, left and right can be inserted and added

  • If the key does not exist, create a new linked list
  • If the key already exists, add content
  • All values ​​are removed, key disappears
  • Because it is a linked list, its efficiency for head and tail operations is extremely high, but if it is an operation on the middle element, the efficiency is poor

5.4、Set

Equivalent to Map<String,Set> in java

  • When I first saw a set, did you remember that it does not allow duplicate elements? Yes, its bottom layer is implemented by hashTable, and all the instructions of the set are born to re-duplicate, all start with s
    Insert picture description here

5.5 、 Zset

(sorted set: ordered set) sort_set: sortable set

  • First of all: it also has the characteristics of set, de-duplication!
  • Second: The value of each element will be associated with a double type score before. Redis will sort the score members from small to large. (Scores can be repeated) It is said that the game score rankings we usually play are made by it.

Set value is k1 v1 k2 v2
zset value K1 score v1 k2 score v2

six

Three, big special data types

6.1、geospatial

Official website address: https://redis.io/commands/geoadd

Can be used to calculate the distance between two places, people within a radius

# 添加三个城市
127.0.0.1:16379[2]> geoadd china:city 116.40  39.99 beijing
(integer) 1
127.0.0.1:16379[2]> geoadd china:city 117.190 39.1255 tianjin
(integer) 1
127.0.0.1:16379[2]> geoadd china:city 120.36955 36.094 qingdao
(integer) 1
127.0.0.1:16379[2]>

# 获取指定key的经度和纬度
127.0.0.1:16379[2]> geopos china:city beijing
1) 1) "116.39999896287918091"
   2) "39.99000043587556519"
127.0.0.1:16379[2]>

# 获取两个给定位置的距离
127.0.0.1:16379[2]> geodist china:city beijing qingdao # 默认单位为米
"555465.2188"
127.0.0.1:16379[2]> geodist china:city beijing qingdao km
"555.4652"
127.0.0.1:16379[2]> geodist china:city beijing qingdao m
"555465.2188"
127.0.0.1:16379[2]> geodist china:city beijing qingdao mi # 英里
"345.1509"
127.0.0.1:16379[2]> geodist china:city beijing qingdao ft # 英尺
"1822392.4503"

# 查找附近的人
# 以给定的经纬度为中心,找出某一半径内的元素
127.0.0.1:16379[2]> georadius china:city 117.190 39.1255 200 km # 半径为200km
1) "tianjin"
2) "beijing"
127.0.0.1:16379[2]> georadius china:city 117.190 39.1255 200 km withdist # 指定显示距离
1) 1) "tianjin"
   2) "0.0001"
2) 1) "beijing"
   2) "117.6221"
127.0.0.1:16379[2]> georadius china:city 117.190 39.1255 200 km count 2 # 指定显示2个结果
1) 1) "tianjin"
   2) "0.0001"
2) 1) "beijing"
   2) "117.6221"

# 以指定的members为依据,找到它指定范围内的元素
127.0.0.1:16379[2]> GEORADIUSBYMEMBER china:city beijing 120 km
1) "beijing"
2) "tianjin"

# 返回一个或者多个位置的11位长度的hash串表示
127.0.0.1:16379[2]> geohash china:city beijing
1) "wx4g2xzyss0"
127.0.0.1:16379[2]> geohash china:city beijing tianjin
1) "wx4g2xzyss0"
2) "wwgqddx4sc0"

# geo底层使用 zset 实现
127.0.0.1:16379[2]> ZRANGE china:city 0 -1
1) "qingdao"
2) "tianjin"
3) "beijing"

# 可以通过zrem删除 geo添加的key中的member
127.0.0.1:16379[2]> ZREM china:city beijing
(integer) 1
127.0.0.1:16379[2]> ZRANGE china:city 0 -1
1) "qingdao"
2) "tianjin"

6.2、Hyperloglog

Generally we use Hyperloglog for base statistics.

What is the base? Is the number of unique numbers in a set

Set A: {1,3,5,7,9,7}

Set B: {1,3,5,7,9}

The base of the AB set is 5

Application : Statistics website visits (a person visits the website many times is still counted as one)

Advantages : The occupied memory is fixed, to find the base of the number of 2^64 power, only 12KB of memory is needed

Disadvantages : 0.81% error rate, which can be ignored

# PFCOUNT 计算出来的数量就是Set的基数
127.0.0.1:16379[2]> PFADD key4 q w e r q
(integer) 1
127.0.0.1:16379[2]> PFCOUNT key4
(integer) 4

# 添加key1和key对应的多个值
127.0.0.1:16379[2]> PFADD key1 q w e r
(integer) 1

# 统计key下有多少个值
127.0.0.1:16379[2]> PFCOUNT key1
(integer) 4

# 添加key2和key对应的多个值
127.0.0.1:16379[2]> PFADD key2 a s d f
(integer) 1

# 合并多个key成为一个key
127.0.0.1:16379[2]> PFMERGE key3 key1 key2
OK
127.0.0.1:16379[2]> PFCOUNT key3
(integer) 8

6.3、Bitmap

For example, use bitmap to record user login information from No. 1 to No. 3

# key为sign表示登陆
127.0.0.1:16379[2]> SETBIT sign 1 0 # 第一天没有登陆
(integer) 0
127.0.0.1:16379[2]> SETBIT sign 2 0
(integer) 0
127.0.0.1:16379[2]> SETBIT sign 3 1 # 第三天登陆了
(integer) 0

# 获取指定某位的值
127.0.0.1:16379[2]> GETBIT sign 3
(integer) 1

# 统计用户这三天登陆了几次,就是统计key下有多少位1
127.0.0.1:16379[2]> GETBIT sign 3
(integer) 1
127.0.0.1:16379[2]> bitcount sign
(integer) 1

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_50662680/article/details/110948426