Article directory
Why learn Redis
relieve io pressure
Early web 1.0 period
In the era of Web 1.0, the amount of data access is very limited, and most problems can be solved with a high-performance single-point server.
web2.0 stage
With the advent of the era of Web 2.0, the number of user visits has increased significantly, and a large amount of user data has been generated at the same time. Coupled with the subsequent popularity of smart mobile devices, all Internet platforms face huge performance challenges.
How Redis solves IO pressure
What is a NoSQL database
NoSQL (NoSQL = Not Only SQL), which means "not only SQL", generally refers to non-relational databases.
NoSQL does not rely on business logic to store, but is stored in a simple key-value mode. Therefore, the scalability of the database is greatly increased.
- Does not follow the SQL standard.
- ACID is not supported.
- Far exceeds the performance of SQL.
Redis is an excellent NoSQL database
How to Install Redis6 on Centos7
Installing Redis6 There are many tutorials on the Internet to find a suitable installation. Here is the installation with yum.
In CentOS and Red Hat systems, first add the EPEL repository, and then update the yum source:
sudo yum install epel-release
sudo yum update
Then install the Redis database:
sudo yum -y install redis
After installation, start the Redis service:
sudo systemctl start redis
Here you can also use redis-cli to enter the Redis command line mode operation.
In addition, in order to enable Redis to be connected remotely, the configuration file needs to be modified, and the path is
/etc/redis.conf
we go into this file
vi /etc/redis.conf
Where to modify:
First, comment this line:
#bind 127.0.0.1
In addition, it is recommended to set a password for Redis, uncomment this line:
#requirepass foobared
foobared is the current password, which can be changed to
requirepass password Setting password is very important. If you don't have a password, be careful that your server is used for mining by green forest heroes.
We need Redis to start in the background.
We need to put in the configuration file
daemonize no to yes
Then restart the Redis service with the following command:
sudo systemctl restart redis
systemctl start redis.service #Start redis server
systemctl stop redis.service #Stop redis server
systemctl restart redis.service #Restart the redis server
systemctl status redis.service #Get the running status of the redis server
systemctl enable redis.service #Start the redis server
systemctl disable redis.service #Disable redis server at startup
Five data types of Redis
Redis key (key)
keys * # 查看当前库所有key (匹配:keys *1)
exists key # 判断某个key是否存在
type key # 查看你的key是什么类型
del key # 删除指定的key数据
unlink key # 根据value选择非阻塞删除
仅将keys从keyspace元数据中删除,真正的删除会在后续异步操作。
expire key 10 # 10秒钟:为给定的key设置过期时间
ttl key # 查看还有多少秒过期,-1表示永不过期,-2表示已过期
select # 命令切换数据库
dbsize # 查看当前数据库的key的数量
flushdb # 清空当前库
flushall # 通杀全部库
Redis String (String)
Introduction to String
-
String is the most basic type of Redis, a key corresponds to a value.
-
The String type is binary safe. Means Redis string can contain any data. Such as jpg images or serialized objects.
-
The String type is the most basic data type of Redis, and the string value in a Redis can be up to 512M
-
When the space of String is not enough, it will double the space of String. If it exceeds 1M, it will increase by 1M at a time.
Common commands
set key value # 如果不存在则创建一个key value,如果key已存在修改value的值
get key # 获取key的value值
strlen key # 获取key的长度
setnx key value # 创建一个key value,如果key已存在报错
incr key # 将key中储存的值+1,value必须为数字类型
decr key # 将key中储存的值-1,value必须为数字类型
incr key 步长 # 将key中储存的值+步长,value必须为数字类型
decr key 步长 # 将key中储存的值-步长,value必须为数字类型
mset key value key value # 创建多个key value
mget key value key value # 获取多个key的value值
msetnx key value key vale # 创建多个key value值,key不存在情况下
getrange key 3 4 # 获取一个key的第三个字符到第四个字符,包前包后
setrange key 3 value # 在第三个字符后面插入一个value
setex key 过期时间 value # 规定一个key过期时间
getset key value # 查看一个key的value,但随后被替换为新的value
Redis List (List)
What is List?
- Redis lists are simple lists of strings, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list.
- In the case of fewer list elements, a contiguous piece of memory is used for storage. This structure is a ziplist, which is a compressed list. It stores all the elements next to each other, and allocates a contiguous piece of memory. When the amount of data is large, it will be changed to quicklist.
- Its bottom layer is actually a doubly linked list, and the operation performance on both ends is very high, and the performance of the node in the middle of the operation through the index subscript will be poor.
Common commands
lpush key value1 value2 valu3 # 在左边插入值,结果是 value3 value2 value1
rpush key value1 value2 valu3 # 在右边插入值,结果是 value1 value2 value3
rpoplpush key1 key2 # 从key1的右边吐出一个值,就像迭代器一样,每执行一次吐出一个值加到key2后面
lrange key value start stop # 取出这个key的start stop的值,0 -1是取出全部
lindex key index # 根据index取出value的值
llen key # 获取key的长度
linsert key before value newvalue # 在本来的value值后面插入一个值
lrem key n value # 从左边删除n个value
lset key index value # 将key下标为index的value替换为新的value
Redis collection (Set)
What is Set ?
- Redis's Set is an unordered collection of string type. Its bottom layer is actually a hash table whose value is null, so the complexity of adding, deleting, and searching is O(1).
- The special thing is that set can be automatically re-arranged. When you need to store a list of data and do not want duplicate data, set is a good choice, and set provides a way to determine whether a member is in a set collection. An important interface, which is also not provided by list.
Common commands
sadd key value value # 创建一个新的集合
smembers key # 取出该集合所有的值
sismember key value # 判断该集合是否含有value值 有1 无0
scard key # 返回该集合的元素个数
srem key value1 value2 # 删除集合中的value元素
spop key # 随机吐出集合的一个值,吐出就删除了
srandmember key n # 随机从集合中取出n个值,不删除
smove key1 key2 value # 从key1中取出value添加到key2中
sinter key1 key2 # 返回两个集合交集的元素,2个集合都有的值
sunion key1 key2 # 返回两个集合并集的元素,2个集合全部的值
sdiff key1 key2 # 返回两个集合差集的元素,key1中没有key2的值
Redis hash (Hash)
Introduction
- Redis hash is a mapping table of field and value of string type, and hash is especially suitable for storing objects. Similar to Map<String, Object> in Java
Common commands
hset key field value # 添加一个哈希集合,添加一个值 hset user id 1
hget key field # 获取key中field的value值
hmset key field value1 field value2 # 添加一个哈希结合,添加多个值 hmset user id 1 name zhangsan
hexists key field # 查看哈希表key中,field是否存在
hkeys key # 列出该hash集合所有的value
hincrby key field increment # 为哈希表key中field的value值加步长
hsetnx key field value # 在哈希表key中添加一列firld-value
Redis sorted set (sorted set)
Introduction
-
The Redis sorted set zset is very similar to the normal set set, it is a set of strings without repeated elements.
-
The difference is that each member of the sorted set is associated with a score, which is used to sort the members of the set from the lowest score to the highest score. Members of the set are unique, but ratings can be duplicated.
Common commands
zadd key 评分 value 评分 value # 创建一个有序集合,根据评分选择先后
zrange key start end withscores # 返回下标为几到几的,withscores显示评分
zrangebyscore key 评分~评分 [withscores] # 返回评分几到几,withscores显示评分
zrevrangebyscore key 评分~评分 [withscores] # 返回key中频分几到几的值,withscores显示评分,从大到小排列
zincrby key 步长 value # 为key中的value评分加步长
zrem key value # 删除指定的值
zcount key min max # 评分几到几的个数
zrank key value # 返回该值在集合中的排名,从0开始
Introduction to Redis configuration files
Units
Configure the size unit. Some basic units of measurement are defined at the beginning. Only bytes are supported, and bits
are not case-insensitive .
INCLUDES contains
Similar to the include in jsp, the common configuration file can be extracted in the case of multiple instances
Network related configuration
bind
By default, bind=127.0.0.1 can only accept access requests from the local machine.
If it is not written, it will accept unlimited access to any ip address. The
production environment must write the address of your application server; the server needs remote access, so it needs to be Comment it out
If protected-mode is turned on, then Redis only allows to accept the response of the machine if no bind ip and no password are set
protected-mode
Set native access protected mode to no
Port
Port number, default 6379
tcp-backlog
Set the backlog of tcp. The backlog is actually a connection queue. The sum of the backlog queues = the three-way handshake queue that has not been completed + the three-way handshake queue that has completed.
In a high concurrency environment you need a high backlog value to avoid slow client connection issues.
GENERALUniversal
daemonize
Whether it is a background process, set to yes
daemon process, start in the background
pidfile
The location where the pid file is stored, each instance will generate a different pid file
loglevel
Specify the logging level. Redis supports a total of four levels: debug, verbose, notice, and warning. The default is
four levels of notice, which are selected according to the usage stage, and notice or warning for the production environment.
databases 16
Set the number of libraries to 16 by default, and the default database to 0. You can use the SELECT command to specify the database id on the connection
LIMITS limit
maxclients
Set how many clients redis can connect to at the same time.
The default is 10000 clients.
If this limit is reached, redis will reject new connection requests and respond with "max number of clients reached" to those connection requesters.
maxmemory
It is recommended that it must be set, otherwise, the memory will be full and the server will be down.
Sets the amount of memory that redis can use. Once the memory usage limit is reached, redis will try to remove internal data. The removal rule can be specified by maxmemory-policy.
If redis cannot remove data in memory according to the removal rules, or if "removal not allowed" is set, then redis will return error messages for those instructions that need to apply for memory, such as SET, LPUSH, etc.
However, for commands without memory application, it will still respond normally, such as GET. If your redis is the master redis (indicating that your redis has slave redis), then when setting the upper limit of memory usage, you need to set aside some memory space in the system for the synchronization queue cache, only if you set "do not remove" In this case, this factor need not be considered.
maxmemory-policy
volatile-lru: Use the LRU algorithm to remove keys, only for keys with an expiration time set; (least recently used)
allkeys-lru: In all set keys, use the LRU algorithm to remove keys
volatile-random: Remove random keys from the expired set, only for keys with an expiration time set
allkeys-random: in all set keys, remove random keys
volatile-ttl: remove those keys with the smallest TTL value, that is, those keys that will expire recently
noeviction: Do not remove. For write operations, just return an error message
maxmemory-samples
Set the number of samples, the LRU algorithm and the minimum TTL algorithm are not exact algorithms, but estimates, so you can set the size of the sample, redis will check so many keys by default and choose the one with LRU.
Generally set the number from 3 to 7, the smaller the value, the less accurate the sample, but the smaller the performance consumption.
Redis publish and subscribe
What is publish and subscribe
Redis clients can subscribe to any number of
How to implement publish and subscribe
1. Open a client to subscribe to channel1
SUBSCRIBE channel1
2. Open another client and publish message hello to channel1
publish channel1 hello
The returned 1 is the number of subscribers.
3. Open the first client to see the sent message.
Note: The published message is not persistent. If the subscribed client does not receive hello, it can only receive the message published after the subscription.
New data types in Redis
BitMaps
Introduction
- Bitmaps itself is not a data type, in fact it is a string (key-value), but it can operate on the bits of the string
- Bitmaps is an array of bit units. Each unit of the array can only store 0 and 1. The array subscript bitmaps is called offset
Order
Setbit key offset value # Create a Bitmap with an offset value of 0 or 1
Example
Whether each independent user has visited the website is stored in Bitmaps, the accessed user is recorded as 1, the user who has not accessed is recorded as 0, and the offset is used as the user id.
Set the value of the offset bit of the key (counting from 0)
Assuming that there are now 20 users, users with userid=1 6 11 15 19 have visited the website,
then the current Bitmaps initialization result is shown in the figure
Getbit key offset # Get an offset in bitmaps
Get whether the user with id 8 has visited on a certain day, and return 0 to indicate that they have not visited:
Bitcount key # Get the number of 1s in this key
Bitcount key start end # Calculate the number of 1s in start 8 to end 8
Bitop and(or/not/xor) destkey key # bitop is a compound operation, it can do multiple Bitmaps and (intersection), or, (union), not (not), xor (exclusive or) operations and save the result in destkey.
Demonstration case:
Userid=1,2,5,9 for visiting the website on 2020-11-04.
setbit unique:users:20201104 1 1
setbit unique:users:20201104 2 1
setbit unique:users:20201104 5 1
setbit unique:users:20201104 9 1
Userid=0,1,4,9 for visiting the website on 2020-11-03.
setbit unique:users:20201103 0 1
setbit unique:users:20201103 1 1
setbit unique:users:20201103 4 1
setbit unique:users:20201103 9 1
Calculate the number of users who visited the site in both days
bitop and unique:users:and:20201104_03 unique:users:20201103 unique:users:20201104
Bitop union temporarily stored collection No. 20201103 No. 20201104
illustrate
Use bitmaps when there are many users, and use key-value when there are few users
HyperLogLog
Introduction
It is used to record non-repeated data. For example, if java is stored in a key in this type, it will fail to save java.
Order
pfadd key value 1 value 2 # Create a set similar data type
pfcount key [key...] # Count unique data in one or more keys
pfmerge key3 key2 key1 # Merge the values of key1 and key2 into key3
Geospatial
Introduction
Operations on geographic locations
Order
geoadd key latitude and longitude name # Add one or more geographic coordinates
The poles cannot be added directly. Generally, the city data will be downloaded and imported directly through the Java program at one time.
Valid longitudes are from -180 degrees to 180 degrees. Valid latitudes are from -85.05112878 degrees to 85.05112878 degrees.
When the coordinate position exceeds the specified range, the command will return an error.
Data that has already been added cannot be added to it again.
geopos key city name# Returns the longitude and latitude of the geographic location
geodist key Geographical location [m|km|ft|mi] # Get the straight-line distance between two locations
unit:
m means the unit is meters [default].
km indicates that the unit is kilometers.
mi indicates the unit is miles.
ft means the unit is feet.
If the user does not explicitly specify the unit parameter, then GEODIST uses meters as the default unit
georadius key Longitude and latitude distance unit # Find the city within the circle of longitude and latitude radius