Skip to content

Commit

Permalink
Merge pull request #64 from Prography-8th-team8/refactor-store-id
Browse files Browse the repository at this point in the history
redis 부착 성능개선
  • Loading branch information
govl6113 authored Nov 6, 2023
2 parents 3043ba1 + 4af803e commit 1188706
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ out/

### VS Code ###
.vscode/

### DATA ###
data
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ repositories {
dependencies {
//data
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-validation'
runtimeOnly 'com.h2database:h2'
compileOnly 'org.projectlombok:lombok'
Expand All @@ -37,6 +38,7 @@ dependencies {

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.testcontainers:testcontainers:1.17.2'

// Swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.4'
Expand Down
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: "3"

services:
redis:
image: redis:6
volumes:
- ./data/redis:/data
command: redis-server --port 6379
ports:
- "6379:6379"
networks:
- cakk

networks:
cakk:
labels:
- cakk
36 changes: 36 additions & 0 deletions src/main/java/prography/cakeke/server/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package prography.cakeke.server.config;

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.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

@Value("${spring.data.redis.host}")
private String host;

@Value("${spring.data.redis.port}")
private Long port;

@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName(host);
configuration.setPort(port.intValue());
return new LettuceConnectionFactory(configuration);
}

@Bean
public RedisTemplate<?, ?> redisTemplate() {
RedisTemplate<?, ?> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import com.amazonaws.services.s3.model.PutObjectRequest;

import lombok.RequiredArgsConstructor;
import prography.cakeke.server.image.application.port.out.UploadS3Port;
import prography.cakeke.server.image.exceptions.InvalidFileNameException;
import prography.cakeke.server.store.application.port.out.UploadS3Port;

@Component
@RequiredArgsConstructor
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package prography.cakeke.server.store.application.port.out;
package prography.cakeke.server.image.application.port.out;

import org.springframework.web.multipart.MultipartFile;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import lombok.RequiredArgsConstructor;
import prography.cakeke.server.image.application.port.in.ImageUseCase;
import prography.cakeke.server.image.application.port.out.UploadS3Port;
import prography.cakeke.server.image.exceptions.NotSupportedFileFormatException;
import prography.cakeke.server.store.application.port.out.UploadS3Port;

@Service
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package prography.cakeke.server.store.adapter.out.external;

import java.time.Duration;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Repository;

import lombok.RequiredArgsConstructor;
import prography.cakeke.server.store.application.port.out.LoadRedisPort;
import prography.cakeke.server.store.application.port.out.SaveRedisPort;

@Repository
@RequiredArgsConstructor
public class RedisAdapter implements LoadRedisPort, SaveRedisPort {
private final RedisTemplate<String, String> redisTemplate;
private final Integer TTL = 86400;

@Override
public String save(String key, String value) {
getOperations().set(key, value, Duration.ofMillis(TTL));
return value;
}

@Override
public String getByKey(String key) {
return getOperations().get(key);
}

private ValueOperations<String, String> getOperations() {
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(String.class));
return redisTemplate.opsForValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package prography.cakeke.server.store.application.port.out;

public interface LoadRedisPort {
String getByKey(String key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package prography.cakeke.server.store.application.port.out;

public interface SaveRedisPort {
String save(String key, String value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import prography.cakeke.server.store.adapter.in.web.response.StoreNaverLocalSearchApiResponse;
import prography.cakeke.server.store.application.port.in.StoreUseCase;
import prography.cakeke.server.store.application.port.out.LoadNaverSearchApiPort;
import prography.cakeke.server.store.application.port.out.LoadRedisPort;
import prography.cakeke.server.store.application.port.out.LoadStorePort;
import prography.cakeke.server.store.application.port.out.SaveRedisPort;
import prography.cakeke.server.store.domain.District;
import prography.cakeke.server.store.domain.Store;
import prography.cakeke.server.store.domain.StoreTag;
Expand All @@ -32,6 +34,8 @@ public class StoreService implements StoreUseCase {

private final LoadNaverSearchApiPort loadNaverSearchApiPort;
private final LoadStorePort loadStorePort;
private final SaveRedisPort saveRedisPort;
private final LoadRedisPort loadRedisPort;

/**
* 각 구별 가게의 개수를 반환합니다.
Expand Down Expand Up @@ -113,8 +117,27 @@ public Store getStore(Long storeId) {
*/
@Override
public StoreNaverLocalSearchApiResponse getNaverLocalApiByStore(Store store) {
final String storeName = store.getName();
return loadNaverSearchApiPort.getNaverLocalSearchResponse(storeName);
/**
* 1. redis에서 검색해보고 없으면 naver에서 link 정보 가져와 redis에 저장하고 반환.
* 2. redis에 있으면 바로 반환.
*/
String redisResponse = loadRedisPort.getByKey(String.valueOf(store.getId()));

// redis에 없으면
if (redisResponse == null) {
StoreNaverLocalSearchApiResponse naverResponse =
loadNaverSearchApiPort.getNaverLocalSearchResponse(store.getName());
saveRedisPort.save(String.valueOf(store.getId()), naverResponse.getLink());
return naverResponse;
}

// redis에 있으면
return StoreNaverLocalSearchApiResponse.builder()
.link(redisResponse)
.address("")
.phoneNumber("")
.description("")
.build();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ spring:
database: postgresql
hibernate:
ddl-auto: update
data:
redis:
host: ENC(k9JRvKt85GZzSZSHbXBXrA==)
port: 6379

management:
endpoints:
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/prography/cakeke/server/common/BaseMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class BaseMock {
protected final StoreType testStoreType = StoreType.CHARACTER;

protected final String testNaverStoreName = "끌레르 봉봉";
protected final String testNaverStoreAddress = "서울특별시 강남구 논현로114길 8 1층 103호 끌레르봉봉";
protected final String testNaverStoreLink = "http://pf.kakao.com/_busxnC";

protected Store buildStore(String storeName) {
return Store.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package prography.cakeke.server.config;

import org.junit.jupiter.api.DisplayName;
import org.springframework.context.annotation.Configuration;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.utility.DockerImageName;

@DisplayName("Redis Test Containers")
@Configuration
public class RedisTestContainers {

private static final String REDIS_DOCKER_IMAGE = "redis:5.0.3-alpine";

static { // (1)
GenericContainer<?> REDIS_CONTAINER =
new GenericContainer<>(DockerImageName.parse(REDIS_DOCKER_IMAGE))
.withExposedPorts(6379)
.withReuse(true);

REDIS_CONTAINER.start(); // (2)

// (3)
System.setProperty("spring.data.redis.host", REDIS_CONTAINER.getHost());
System.setProperty("spring.data.redis.port", REDIS_CONTAINER.getMappedPort(6379).toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void getNaverLocalApiTestSuccess() {
StoreNaverLocalSearchApiResponse testStoreNaverLocalSearchApiResponse =
storeService.getNaverLocalApiByStore(testStore);

assertThat(testStoreNaverLocalSearchApiResponse.getAddress()).isEqualTo(testNaverStoreAddress);
assertThat(testStoreNaverLocalSearchApiResponse.getLink()).isEqualTo(testNaverStoreLink);
}

@Test
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ spring:
database: postgresql
hibernate:
ddl-auto: create-drop
data:
redis:
host: ENC(pCIBlNunKRquFLxqCT3VgUicVCF6Z1si)
port: 6379

management:
endpoints:
Expand Down
8 changes: 8 additions & 0 deletions src/test/resources/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "3"

services:
redis:
image: redis:7
command: redis-server --port 6379
ports:
- "6380:6379"

0 comments on commit 1188706

Please sign in to comment.