当前位置:网站首页>Beginner redis
Beginner redis
2022-07-06 00:42:00 【·Nanke Yimeng ·】
1.Redis install
( Ensure that the virtual machine is connected to the Internet )
Download address :https://redis.io/download Generally download the stable version (Stable).
1.1 Download installation package
- Download and install to /usr/local/src Catalog
wget -P /usr/local/src https://download.redis.io/releases/redis-6.0.9.tar.gz
- without wget Install first wget
yum install -y wget
1.2 decompression Redis
tar -zxvf redis-6.0.9.tar.gz
1.3 compile Redis
Get into redis Catalog
cd /usr/local/src/redis-6.2.6
because Redis Yes, it is C Language development , It depends on C Language environment , So we need to install dependencies first .
yum install -y gcc-c++ autoconf automake
1.3.1 upgrade GCC
Here's a description ,yum Installed GCC The version is very low , The default version is 4.8.5, When the version is too low, the compilation will report an error .
# see GCC edition
gcc -v
Low version compilation error :
So we need to upgrade GCC.
# install scl Language
yum install -y centos-release-scl scl-utils-build
# install 9 Version of gcc 、gcc-c++、gdb Tool chain
yum install -y devtoolset-9-toolchain
# Temporary coverage of the original system gcc quote
scl enable devtoolset-9 bash
# see gcc edition
gcc-v
recompile Redis, At this point, the compilation is successful
make
here Redis It has been installed successfully , But it will be installed in Centos The default directory of , Now for the installation Redis Change to the specified directory
1.4 install Redis
# Create directory file
mkdir -p /usr/local/redis
# Compile the previously compiled redis Install to specified directory
make PREFIX=/usr/local/redis install
This is the end of the installation .
1.5 start-up Redis
# Get into redis Of bin Catalog
cd /usr/local/redis/bin
# Direct operation redis
./redis-server
This method is pre start , But the console will display Redis Interface , No other operation
So let's run it another way , Run as a daemon . First Ctrl+C Exit this screen . because Redis When starting, record the start of the configuration file . Find this profile
# Enter profile directory
cd /usr/local/src/redis-6.0.9/
# Copy a copy of the configuration file to the newly created redis Directory
cp redis.conf /usr/local/redis/bin
Amendment No 224 That's ok , hold no Change it to yes, This represents : Whether to start as a daemon , The default is no.
start-up redis
# Start by specifying the path of the configuration file redis, Specify the configuration file directory as the directory of the configuration file just modified
./redis-server ./redis.conf
2. Redis Configuration of
2.1 Redis Configuration file for
Redis Support many parameters , All have default values .
- daemonize:Redis Whether to start in the background , The default is no.
- bind : redis Only accept from this Ip Request ( If it's not on your own virtual machine, don't match it 0.0.0.0, If you use a server, please configure it as specified IP, Otherwise, it will be used by brother black to mine .)
- port: Start port , Default 6379
- database: Set the number of databases , The default database is 0
- save: Set up Redis Frequency of database mirroring
- dbfilename: The file name of the image backup file .
- dir: The location path of the files backed up by the database image
- requirepass: Set the password that needs to be used before any other operation after the client connection ( Try to match , Prevent Heige from mining )
- maxclients: Limit the number of clients connected at the same time .
- maxmemory: Set up redis Maximum use of cache .
Modify the configuration file so that the client can connect
modify bind
Because I am a virtual machine on this machine Redis , So I configured 0.0.0.0
modify password( Try to make the configuration as complex as possible )
kill redis process , restart redis
kill -9 Port number (pid)
./redis-server /usr/local/redis/bin/redis.conf
Use Redis Desktop Manager Test connection
So the connection is successful
Use Redis Built in client connection
cd /usr/local/redis/bin
# Start and enter the client
./redis-cli
# If the password is set, you need a command after entering
auth password
3.Redis command
Reference documents :http://doc.redisfans.com/
1.Redis Command after connection
1.1 select
# Switch the current database
127.0.0.1:6379> select 2 ## Switch to 2 The database
127.0.0.1:6379[2]> select 3 ## Switch to 3 The database , Be careful Redis Now there are more command prompts [2]
1.2 quit
Request the server to close the connection with the current client .
# Launch the client
127.0.0.1:6379[2]> quit
2.Redis Basic data type
Redis As a data structure in memory, the storage system , It can be used as a database , cache , Message middleware , His value Support multiple types of data structures , Basic data types include : character string , hash , list , aggregate , Assemble five kinds of . These five data structures are often used in our work , During the interview, I am often asked about , So master this 5 The usage and application scenarios of the basic data structure in are Redis The most basic and important part .
2.1 character string (Strings)
2.1.1 Type introduction
The string is Redis The simplest type of storage , The stored value can be a string , Integers , Floating point numbers . The integer can be self incremented (increment) Self decrement (decrement) operation .
Redis A string is a sequence of bytes , Follow Java Of ArrayList A bit similar , Pre allocate redundant space to reduce frequent memory allocation , When the string length is less than 1MB when , Both doubled , If exceeded 1MB. Each expansion will only increase 1MB. The maximum length of the string is 512MB
2.1.2 Application scenarios
String type is widely used in work , It is mainly used to cache data ( Not much change , Frequently queried data ), Improve query performance , For example, store login user information , Store commodity information in e-commerce , It can be used as a counter ( For example, a IP Visited several times )
2.1.3 Common operation instructions
set key value # Set a key value pair , If key Existence will overwrite the content
get key value # Get a key value pair
mset key1 value1 key2 value2 .... # Set multiple key value pairs
mget key1 key2.... # Get multiple key value pairs .
incr key # Self increasing 1,key Must be numerical
decr key # Self reduction 1,key Must be numerical
incrby key step # Increase according to the specified length ,step: Specify the self increasing length
decrby key step # Subtract from the specified length ,step: Specify the self increasing length
del key # Delete a key value pair
2.2 hash (hashes)
2.2.1 Type introduction
Hashing is equivalent to Java Of HashMap, Inside is an unordered Dictionary , Realization principle and HashMap Agreement , A hash has multiple nodes , A node can only save one key value pair .
And Java Medium HashMap The difference is ,rehash In a different way , because Java Of HashMap When the dictionary is big ,rehash It's a time-consuming operation , All at once rehash,Redis For high performance , Do not block service , So it's incremental rehash The strategy of .
Progressive type rehash Will be in rehash At the same time , Keep the old and the new hash structure , Two will be queried at the same time hash structure , Then in the subsequent scheduled tasks and hash In operation instruction , Gradually replace the old hash A little bit of content migrated to the new hash In structure , When the move is done , Will use the new hash Structure takes the place of .
When hash After removing the last element , The data structure is automatically deleted , Memory is recycled .
2.2.2 Application scenarios
hash It can also be used for object storage , For example, storing user information , What's different from a string is , String needs to serialize the object ( such as json serialize ) Before you can save , and hash You can store each field of the user object separately , This saves time in serialization and deserialization .
In addition, it can also be used to save users' purchase records , such as key For the user ID,field For commodity id,value Is the quantity of goods . It can also be used to store shopping cart data , such as key For the user id,field For commodity id,value For the number of purchases and so on ...
2.2.3 Common operation instructions
hset key field value # Hash table key In the domain field The value of the set value . If key non-existent , A new hash table is created and executed HSET operation . If the domain field It already exists in the hash table , The old value will be overwritten .
hget key field # Return hash table key Given domain in field Value .
hincrby key field increment # Hash table key In the domain field Plus the increment increment, Increment can also be negative , Equivalent to subtracting a given field .
hgetall key # Return hash table key in , All fields and values .
hmget key field [field ...] # Return hash table key in , The value of one or more given domains .
hlen key # Return hash table key Number of domains .
2.3 list (lists)
2.3.1 Type introduction
List amount to Java Medium LinkedList, The implementation principle is a two-way linked list ( The bottom layer is a quick table ), Can support reverse search and traversal , Easier to operate . Insertion and deletion are very fast , Time complexity O(1), But index positioning is slow , Time complexity O(N)
2.3.2 Application scenarios
You can use List Achieve the top selling list , Implement work queues ( utilize lists Of push operation , Keep the task alive lists in , Then the worker thread uses pop Take out the operation creation task for execution ). Can achieve the latest list , Like the latest comments .
2.3.3 Common operation instructions
lpush key value [value ...] # Put one or more values value Insert into list key The header , Insert the header in sequence
rpush key value [value ...] # Put one or more values value Insert into list key At the end of the watch ( Far right ).
lpop key # Remove and return to list key The head element of .
rpop key # Remove and return to list key The tail element .
lrange key start stop # Returns a list of key The elements in the specified interval , The interval is offset by start and stop Appoint . This is a closed interval , Subscript values out of range do not cause errors .
llen key # Returns a list of key The length of .
2.4 aggregate (Set)
2.4.1 Type introduction
amount to Java Medium HashSet( A disorderly ), The internal implementation is a Value Always empty HashMap, In fact, by calculating hash The way to get rid of weight quickly , This is also set Can provide the reason why a member is in the set .
2.4.2 Application scenarios
redis Of set Types are constructed using hash tables , So the complexity is O(1) It supports addition, deletion, modification and query in the collection , And support the intersection of several sets , Combine , Difference set . These can be used to achieve : Calculate the independence of the website IP, User tags used in user portraits , Common friend function .
2.4.3 Common operation instructions .
sadd key member [member ...] # Put one or more member Elements are added to the collection key among , Existing in a collection member Elements will be ignored .
spop key # Randomly remove and return an element in the collection .
smembers key # Back to the assembly key All members of
Sinter key [key ...] # Return several sets key The intersection between .
sdiff key [key ...] # Return several sets key Difference set between .
sunion key [key ...] # Returns all members of a collection , This set is the union of all given sets .
srandmember key [count] # If count Is a positive number , And less than the set cardinality , Then the command returns a containing count Array of elements , The elements in the array are different . If count Greater than or equal to the set cardinality , So go back to the whole set . This operation and SPOP be similar , but SPOP Remove random elements from the collection and return , and SRANDMEMBER Then only random elements are returned , Without making any changes to the collection .
2.5 Ordered set (sorted Set)
2.5.1 Type introduction
Equivalent to Redis Of Set and HashMap The combination of , On the one hand, he is a Set, Guaranteed the interior Value Uniqueness , On the other hand, it can give each Value Allocate one Score, On behalf of the Value The weight of . For internal use HashMap And jump table to ensure the storage and order of data ,HashMap What's in it is members coming to score Mapping . And all the members in the jump table , The order is based on HashMap Li Cun's score, High query efficiency can be achieved by using jump table structure , And in the implementation is relatively simple ,sorted set Last of Value After being removed , Automatic deletion of data structure .
2.5.2 Application scenarios
Mainly used in Queue scenarios sorted by weight , such as : Game points ranking , Set priority task list , Student transcript, etc ...
2.5.3 Common operation instructions
zadd key score member [[score member] [score member] ...] # Put one or more member Elements and score Value added to ordered set key among . If member If it exists, it will be updated score
zcard key # Return to ordered set key The number of members of
zcount key min max # Statistics score stay min and max Number of members between .
zrange key start stop # Return to ordered set key in , Specify the members in the interval , Where the members are, press score Value increment ( From small to large ) Sort by .
zrevrange key start stop # Return to ordered set key in , Specify the members in the interval , Where the members are, press score Value increment ( From big to small ) Sort by .
zrank key member # return member Member press score Ranking results .
zrem key member # Delete members
Redis Of Java client
1. Client Introduction
Redis Clients that support many languages , It's also redis Reasons for popularity . file : https://redis.io/clients#java
2Java client
RedisJava Client documentation :https://redis.io/clients#java
Redis Of Java There are also many clients , The most popular one is jedis and lettuce
- Jedis In terms of implementation, it's direct connection redis server, If it is thread unsafe in a multithreaded environment , Only connection pool is used at this time , For each Jedis Instance add physical connection . The official recommendation
- lettuce Is based on Netty Of , Thread safety .
- SpringBoot Data Redis 1.X Used before Jedis , But the current version 2.X Change it to Lettuce.
- in general Jedis Better performance than the Lettuce.
2.1 Jedis client
First step : Import Jedis Dependency package .
The second step : Write test code .
First step : Import Jedis Dependency package
<dependencies> <!-- jedis client --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.3.0</version> </dependency> <!-- unit testing Juint --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies>
The second step : Write test code
package com.test;/* * @ClassName JedisTest * @Desc TODO * @Author 19637 * @Date 2022/2/12 0:12 * @Version 1.0 */
import org.junit.Test;
import redis.clients.jedis.Jedis;
public class JedisTest {
@Test
public void test(){
// establish Jedis Client connection Redis
Jedis jedis = new Jedis("192.168.152.129",6379);
// Password authentication
jedis.auth("0211");
// Verify connection , heartbeat
String pong = jedis.ping();
// The return result is pong The connection is successful
System.out.println(pong);
// operation Redis, Command and Redis The built-in client commands are the same .(String Command instance )
jedis.set("user","zhangsan");
String user = jedis.get("user");
System.out.println(user);
// close jedis
jedis.close();
}
}
Execution results
2.2Jedis Connection pool optimization
We all know Jedis It's direct operation Redis Of , When the concurrency is very large , that Jedis operation redis The connection may be abnormal . Therefore, in order to improve the operation efficiency , introduce Connection pool
- First step : Write a connection pool class
- The second step : Get connection through connection pool , Pass the test case
First step : Write a connection pool class
public class JedisPoolTest {
// This class is in Jedis Dependency
private static final JedisPool jedisPool ;
static{
// Create a connection pool configuration object
JedisPoolConfig config = new JedisPoolConfig();
// maximum connection , The default is 8
config.setMaxTotal(5);
// The maximum amount of free time , The default is 8
config.setMaxIdle(5);
// Minimum amount of free time , The default is 0
config.setMinIdle(0);
// Set the wait time (ms)
config.setMaxWaitMillis(100);
jedisPool = new JedisPool(config,"192.168.152.129",6379,100,"0211");
}
public Jedis getConnection(){
return jedisPool.getResource();
}
}
The second step : Get through connection pool Jedis object , Through the case .
import org.junit.Test;
import redis.clients.jedis.Jedis;
public class JedisTest {
@Test
public void test(){
// Get through connection pool Jedis
Jedis jedis = JedisPoolTest.getConnection();
// Verify connection , heartbeat
String pong = jedis.ping();
// The return result is pong The connection is successful
System.out.println(pong);
// operation Redis, Command and Redis The built-in client commands are the same .(String Command instance )
jedis.set("user","zhangsan");
String user = jedis.get("user");
System.out.println(user);
// close jedis
jedis.close();
}
}
SpringBoot Integrate Redis
- First step : Introduce dependencies
- The second step : To configure yml file
- The third step : Writing test classes .
First step : Introduce dependencies
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Jedis-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<version>2.3.4.RELEASE</version>
<artifactId>spring-boot-starter-parent</artifactId>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- Spring Integration of Redis client -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- Let the connection pool use lettuce-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
</dependencies>
</project>
The second step : To configure yml file
server:
port: 8088
spring:
redis:
port: 6379 # Port number
password: "0211" # password
host: 192.168.152.129 #Redis Host IP
lettuce: # Use lettuce Connection pool
pool:
max-wait: 100 # The longest waiting time
max-active: 5 # maximum connection
max-idle: 1 # The maximum amount of free time
min-idle: 1 # Minimum amount of free time
The third step : Writing test classes .
package com.nkym;/* * @ClassName SpringBootRedisTest * @Desc TODO * @Author 19637 * @Date 2022/2/12 1:33 * @Version 1.0 */
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootRedisTest {
@Autowired
RedisTemplate redisTemplate;
@Test
public void test(){
// Get the connection
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
// Test connection
String pong = connection.ping();
// A simple case Use (String type )
redisTemplate.opsForValue().set("hello","zhangsan");
// Get value
System.out.println(redisTemplate.opsForValue().get("hello"));
}
}
Running results .
But when we store data like this, we find that Redis I can't understand the data in
So we need to redefine RedisTemplate The default serializer for
Step four : Redefinition RedisTemplate The default serializer for
stay springboot in RedisTemplate The default is Java Local serialization (JdkSerializationRedisSerializer) The way
establish Redis Configuration class redefines serialization
package com.nkym.config;/* * @ClassName RedisConfig * @Desc TODO * @Author 19637 * @Date 2022/2/12 2:18 * @Version 1.0 */
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@SpringBootConfiguration
public class RedisConfig{
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
// Use Jackson2JsonRedisSerializer To serialize and deserialize redis Of value value ( By default JDK How to serialize )
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
// Use StringRedisSerializer To serialize and deserialize redis Of key value
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
}
Test class
package com.nkym;/* * @ClassName SpringBootRedisTest * @Desc TODO * @Author 19637 * @Date 2022/2/12 1:33 * @Version 1.0 */
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootRedisTest {
@Autowired
RedisTemplate<Object,Object> redisTemplate;
@Test
public void test(){
// Get the connection
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
// A simple case Use (String type )
redisTemplate.opsForValue().set(" Hello "," The world ");
}
}
Running results , It becomes something we can understand , Not hexadecimal .
边栏推荐
- 小程序技术优势与产业互联网相结合的分析
- Spark SQL null value, Nan judgment and processing
- 【文件IO的简单实现】
- KDD 2022 | 脑电AI助力癫痫疾病诊断
- NLP generation model 2017: Why are those in transformer
- How to use the flutter framework to develop and run small programs
- MCU realizes OTA online upgrade process through UART
- Spark-SQL UDF函数
- Why can't mathematics give machine consciousness
- Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
猜你喜欢
XML Configuration File
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
Keepalive component cache does not take effect
Atcoder beginer contest 258 [competition record]
XML配置文件
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
notepad++正則錶達式替換字符串
数据分析思维分析方法和业务知识——分析方法(三)
[designmode] Decorator Pattern
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
随机推荐
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
Idea远程提交spark任务到yarn集群
详细页返回列表保留原来滚动条所在位置
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
【线上小工具】开发过程中会用到的线上小工具合集
Room cannot create an SQLite connection to verify the queries
Comment faire votre propre robot
NLP basic task word segmentation third party Library: ICTCLAS [the third party library with the highest accuracy of Chinese word segmentation] [Chinese Academy of Sciences] [charge]
PHP determines whether an array contains the value of another array
Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code
Browser local storage
小程序容器可以发挥的价值
孤勇者
Single source shortest path exercise (I)
Extension and application of timestamp
FPGA内部硬件结构与代码的关系
时间戳的拓展及应用实例
notepad++正则表达式替换字符串
An understanding of & array names
Why can't mathematics give machine consciousness