Spring Boot integrates Redis

Introduction of depend on

Pom files introduce Redis dependency spring-boot-starter-data-redis

<! --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>Copy the code

Configuration file Configures Redis connection information

Spring: redis: host: localhost # redis server address database: 0 # redis database index (default: 0) port: 6379 # Redis server connection password (default null) jedis: pool: max-active: 8 -1ms # Maximum waiting time for connection pool blocking (negative value indicates no limit) max-idle: 8 # Maximum idle connection in connection pool min-idle: 0 # Minimum idle connection in connection pool timeout: 3000ms # Connection timeout time (ms)Copy the code

Inject the Redis template and use it

@Autowired
private StringRedisTemplate stringRedisTemplate;
Copy the code

Encapsulate a Redis utility class

@Component public class RedisTemplateUtil { @Autowired private StringRedisTemplate stringRedisTemplate; / * * * / stored data public void set (String key, String value) {stringRedisTemplate. OpsForValue (). The set (key, value); Retrieve data} / * * * / public String get (String key) {String value = stringRedisTemplate. OpsForValue (.) get (key); return value; } / * * set expiration time * / public Boolean expire (String key, long expire) {return stringRedisTemplate. Expire (key, expire, TimeUnit.SECONDS); Delete data} / * * * / public void remove (String key) {stringRedisTemplate. Delete (key); } /** * increment @param delta increment */ public Long increment(String key) long delta){ return stringRedisTemplate.opsForValue().increment(key, delta); } } @Autowired private RedisTemplateUtil redisTemplateUtil; . redisTemplateUtil.set(key, value); .Copy the code

More interview information, JDK8 Chinese document, Alibaba Java development manual, PDF books, video, public number “Java road”