-
Notifications
You must be signed in to change notification settings - Fork 0
[Refactor] 보행등 코드 커버리지 높이기 #229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
The head ref may contain hidden characters: "refactor/215-\uBCF4\uD589\uB4F1-\uCF54\uB4DC-\uCEE4\uBC84\uB9AC\uC9C0-\uB192\uC774\uAE30"
Changes from 8 commits
f6d2d5b
5cd28e2
b50c8d3
ac3e9d3
586eed4
1b941f0
e87d422
0dffb17
df847bf
25000ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,10 @@ | |
import org.programmers.signalbuddyfinal.domain.trafficSignal.repository.CustomTrafficRepositoryImpl; | ||
import org.programmers.signalbuddyfinal.domain.trafficSignal.repository.TrafficRepository; | ||
import org.programmers.signalbuddyfinal.global.exception.BusinessException; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.programmers.signalbuddyfinal.domain.trafficSignal.repository.TrafficRedisRepository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
|
@@ -22,53 +20,66 @@ | |
@Transactional(readOnly = true) | ||
public class TrafficService { | ||
|
||
private static final String TRAFFIC_REDIS_KEY = "traffic:info"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용하지 않는 변수는 이제 필요없으니 삭제하면 좋을 거 같아요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 헉 그러게요..! 바로 삭제할게요! |
||
|
||
private final CustomTrafficRepositoryImpl customTrafficRepository; | ||
private final TrafficRedisRepository trafficRedisRepository; | ||
private final TrafficRepository trafficRepository; | ||
private final RedisTemplate<Object, Object> redisTemplate; | ||
|
||
public List<TrafficResponse> searchAndSaveTraffic(Double lat, Double lng, int radius){ | ||
|
||
log.debug("주변 보행등 정보 - lat = {}, lng = {}, radius = {}", lat, lng, radius); | ||
List<TrafficResponse> responseDB; | ||
|
||
boolean exists = Boolean.TRUE.equals(redisTemplate.hasKey("traffic:info")); | ||
if (exists) { | ||
if (trafficRedisRepository.isExist()) { | ||
double kiloRadius = (double) radius/1000; | ||
return trafficRedisRepository.findNearbyTraffics(lat, lng, kiloRadius); | ||
List<TrafficResponse> responseRedis = trafficRedisRepository.findNearbyTraffics(lat, lng, kiloRadius); | ||
|
||
log.debug("redis 주변 보행등 데이터 : redis data 갯수 = {} ", responseRedis.size()); | ||
return responseRedis; | ||
} | ||
|
||
try { | ||
responseDB = customTrafficRepository.findNearestTraffics(lat, lng, radius); | ||
|
||
log.debug("주변 보행등 정보 캐싱 : DB data 갯수 = {} ", responseDB.size()); | ||
for (TrafficResponse response : responseDB) { | ||
trafficRedisRepository.save(response); | ||
} | ||
|
||
log.debug("DB 주변 보행등 데이터 캐싱 성공"); | ||
return responseDB; | ||
|
||
} catch (NullPointerException e) { | ||
log.error("❌ traffic Not Found : {}", e.getMessage(), e); | ||
} catch (Exception e) { | ||
log.error("주변 보행등 조회 실패 : lat = {}, lng = {}, radius = {}, error = {}", lat, lng, radius, e.getMessage()); | ||
throw new BusinessException(TrafficErrorCode.NOT_FOUND_TRAFFIC); | ||
} | ||
} | ||
|
||
public TrafficResponse trafficFindById(Long id) { | ||
|
||
log.debug("보행등 세부정보 찾기 - id = {}", id); | ||
|
||
TrafficResponse responseRedis = trafficRedisRepository.findById( id ); | ||
|
||
if(responseRedis != null) { | ||
log.info("redis 보행등 세부정보 : redis data = {} ", responseRedis); | ||
return responseRedis; | ||
} | ||
|
||
try{ | ||
|
||
TrafficResponse responseDB = new TrafficResponse(trafficRepository.findByTrafficSignalId(id)); | ||
|
||
log.info("보행등 세부정보 : DB data = {} ", responseDB); | ||
|
||
trafficRedisRepository.save(responseDB); | ||
|
||
log.info("보행등 세부정보 캐싱 성공"); | ||
return responseDB; | ||
|
||
} catch (NullPointerException e) { | ||
log.error("❌ traffic Not Found : {}", e.getMessage(), e); | ||
} catch (Exception e) { | ||
log.error("보행등 세부정보 조회 실패 : {}", e.getMessage(), e); | ||
throw new BusinessException(TrafficErrorCode.NOT_FOUND_TRAFFIC); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redis에 Hash 구조로 저장된 데이터를 삭제할 때, 관련 데이터가 전부 삭제되는 것을 확인하셨을까요?
그리고
ValueOperations
를 사용하시면 객체가 통째로 직렬화가 되어 저장됩니다. 역직렬화도 마찬가지로 가능합니다.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
삭제할 때가 TTL이 모두 소진됐을 때를 말씀하시는거 맞죠? 그럼 삭제는 잘 됩니다!
ValueOperations 그래서 쓰는거군여..! 저도 그럼 코드 변경해볼게요! 감사합니다 ㅎㅎ