목차
1. Redis 종속성 패키지 추가
프로젝트의 pom.xml에 다음을 추가하십시오.
<!-- redis依赖包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2. Redis 데이터베이스 연결 구성
다음과 같이 application.properties에서 redis 데이터베이스 연결 정보를 구성합니다.
#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. Redis 운영 도구 작성
RedisTemplate 인스턴스를 도구 클래스로 래핑하여 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. 테스트
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);
}
}
테스트 메소드 세트를 실행 한 후 redis에 로그인하여 데이터가 성공적으로 삽입되었는지 확인할 수 있습니다.
(보려면 RedisDesktopManager 시각화 도구를 사용하는 것이 좋습니다.)
5. 요약
이 장에서는 SpringBoot에서 Redis를 사용하는 방법을 간단히 소개합니다 .Redis의 사용은 그 이상입니다. 실제 프로젝트 요구 사항에 따라 더 복잡해지고 redis로 처리 할 수 있습니다.
이 장의 코드는 Github ( https://github.com/xcbeyond/micro-service/tree/master/springboot ) 에 제출되었으며 필요한 경우 직접 다운로드하십시오.