1. Introduction
Redis is a non-relational database, and its data structure is a key-value storage form; it can support multiple types of data storage, such as: string/list/map/object...etc. Springboot naturally integrates it, we only need to add dependencies and configuration.
2. Dependence
<!--redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
3. Configuration
Here we configure the IP/port of redis, etc., as well as the configuration of the connection pool. The opening of the connection pool needs to introduce the commons-pool2 project of apache, and check whether the dependencies exist before configuration. If not, then it needs to be added.
# Redis server address spring.redis.host=localhost # Redis server connection port spring.redis.port=6379 # Redis server connection password (default is empty) spring.redis.password= # The maximum number of connections in the connection pool (use a negative value to indicate no limit) spring.redis.lettuce.pool.max-active=8 # Connection pool maximum blocking wait time (use a negative value to indicate no limit) spring.redis.lettuce.pool.max-wait=-1 # Maximum idle connections in the connection pool spring.redis.lettuce.pool.max-idle=8 # Minimum idle connection in the connection pool spring.redis.lettuce.pool.min-idle=0
Fourth, RedisTemplate additions, deletions and changes
@Autowired private RedisTemplate redisTemplate; /** * new */ public void saveValue(){ redisTemplate.opsForValue().set("name", "lay"); } /** * Inquire */ public void getValue(){ Object value = redisTemplate.opsForValue().get("name"); System.out.println(value); } /** * delete */ public void delValue(){ redisTemplate.delete("name"); } /** * renew */ public void updateValue(){ redisTemplate.opsForValue().set("name", "marry"); }
In the above code, we inject RedisTemplate. The springboot automatic configuration mechanism will automatically inject the connectionFactory into RedisTemplate to generate Bean instances, so we can directly inject and use them.