(3) Using Redis cache in SpringBoot

table of Contents

1. Add Redis dependency package

2. Configure Redis database connection

3. Write Redis operation tools

4. Test

5. Summary


1. Add Redis dependency package

Add the following in the pom.xml of the project:

<!-- redis依赖包 -->
<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2. Configure Redis database connection

Configure the redis database connection information in application.properties, as follows:

#redis配置
#Redis服务器地址
spring.redis.host=127.0.0.1
#Redis服务器连接端口
spring.redis.port=6379
#Redis数据库索引(默认为0)
spring.redis.database=0  
#连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=50
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=3000
#连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=20
#连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=2
#连接超时时间(毫秒)
spring.redis.timeout=5000

3. Write Redis operation tools

        Wrap the RedisTemplate instance into a tool class to facilitate data operations on redis.

com.xcbeyond.springboot.redis.RedisUtils.java

package com.xcbeyond.springboot.redis;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
 
/**
 * redis操作工具类.</br>
 * (基于RedisTemplate)
 * @author xcbeyond
 * 2018年7月19日下午2:56:24
 */
@Component
public class RedisUtils {
 
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
 
	/**
	 * 读取缓存
	 * 
	 * @param key
	 * @return
	 */
	public String get(final String key) {
		return redisTemplate.opsForValue().get(key);
	}
 
	/**
	 * 写入缓存
	 */
	public boolean set(final String key, String value) {
		boolean result = false;
		try {
			redisTemplate.opsForValue().set(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
 
	/**
	 * 更新缓存
	 */
	public boolean getAndSet(final String key, String value) {
		boolean result = false;
		try {
			redisTemplate.opsForValue().getAndSet(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
 
	/**
	 * 删除缓存
	 */
	public boolean delete(final String key) {
		boolean result = false;
		try {
			redisTemplate.delete(key);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}

4. Test

Write a test case class to complete the reading and writing of redis.

/springboot/src/test/java/com/xcbeyond/springboot/redis/RedisTest.java

package com.xcbeyond.springboot.redis;
 
import javax.annotation.Resource;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * 
 * @author xcbeyond
 * 2018年7月19日下午3:08:04
 */
 
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class RedisTest {
	@Resource
	private RedisUtils redisUtils;
 
	/**
	 * 插入缓存数据
	 */
	@Test
	public void set() {
		redisUtils.set("redis_key", "redis_vale");
	}
	
	/**
	 * 读取缓存数据
	 */
	@Test
	public void get() {
		String value = redisUtils.get("redis_key");
		System.out.println(value);
	}
 
}

After executing the test method set, you can log in to redis to check whether the data is inserted successfully.

(It is recommended to use the RedisDesktopManager visualization tool to view)

5. Summary

         This chapter simply introduces how to use Redis in SpringBoot. The use of Redis is far more than that. According to actual project requirements, it will become more complicated, and things can be processed by redis.

The code for this chapter has been submitted to Github ( https://github.com/xcbeyond/micro-service/tree/master/springboot ), download it yourself if necessary.

Guess you like

Origin blog.csdn.net/sanmi8276/article/details/108747577