- 의존성
- 설정
- Repository
- Test
build.gradle
, 의존성gradle 설정은 lombok
과 spring data redis
를 추가했다.
plugins {
id 'org.springframework.boot' version '2.6.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'me.jinmin'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testCompileOnly group: 'org.assertj', name: 'assertj-core', version: '3.6.1'
}
test {
useJUnitPlatform()
}
RedisConfig
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
@Configuration
@EnableRedisRepositories
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new LettuceConnectionFactory(host, port);
}
@Bean
public RedisTemplate<?, ?> redisTemplate() {
RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory());
return template;
}
}
LettuceConnectionFactory()
사용application.yml
spring:
redis:
host: 127.0.0.1 # localhost
port: 6379
Redis Repository는 객체를 redis의 Hash 자료구조에 맞도록 변환.
secondary indexex
, ttl
적용 가능
Person
import lombok.Builder;
import lombok.Getter;
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
@RedisHash
@Getter
public class People {
@Id
private String id;
private String firstName;
private String lastName;
private Address address;
@Builder
public People(String id, String firstName, String lastName, Address address) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}
}
@RedisHash
: Spring data jpa에서의 @Entitiy
와 동일 (default : class name)Address
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public class Address {
private String city;
private String details;
}
PeopleRedisRepository
import org.springframework.data.repository.CrudRepository;
public interface PeopleRedisRepository extends CrudRepository<People, String> {
}
테스트를 시행하기 전에, Redis 서버를 가동하자 (127.0.0.1:6379
, default)
# 다운로드 한 redis 폴더
$ cd src
$ redis-server # defalut 실행
RedisDataApplicationTests
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
class RedisDataApplicationTests {
@Autowired
private PeopleRedisRepository peopleRedisRepository;
@Test
public void saveTest() {
//given
Address address = new Address("부천", "원미구");
People people = new People(null, "진민", "최", address);
//when
People savePeople = peopleRedisRepository.save(people);
//then
Optional<People> findPeople = peopleRedisRepository.findById(savePeople.getId());
assertThat(findPeople.isPresent()).isEqualTo(Boolean.TRUE);
assertThat(findPeople.get().getFirstName()).isEqualTo("진민");
}
}
결과
키 확인
...People
: People과 관련된 모든 key를 관리...People:6856f1d8-ba1e-48c3-b71d-2294e9f778c7
: People의 id와 매핑된 정보package.Class:{id}
의 형태null
로 설정했기 때문에 redis에서 자동 설정타입 확인
...People
: Set의 형태, 존재하는 지 확인할 때 유용...People:6856f1d8-ba1e-48c3-b71d-2294e9f778c7
: Hash의 형태데이터 확인
...People
: smembers
로 확인하면, id
가 들어 있음을 확인할 수 있다....People:6856f1d8-ba1e-48c3-b71d-2294e9f778c7
: hgetAll <key>
를 통해서 값들을 확인할 수 있다.참조