Skip to content

Commit

Permalink
feat(store): store id 조회를 redis에 cache처리
Browse files Browse the repository at this point in the history
  • Loading branch information
govl6113 committed Nov 2, 2023
1 parent e776f50 commit e999ce6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 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
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

0 comments on commit e999ce6

Please sign in to comment.