-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from moidot/chore/enhance-with-cache
chore: 캐싱 로직 추가 및 로그 파일 분리
- Loading branch information
Showing
12 changed files
with
261 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/com/moim/backend/domain/space/service/DirectionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.moim.backend.domain.space.service; | ||
|
||
import com.moim.backend.domain.space.config.OdsayProperties; | ||
import com.moim.backend.domain.space.entity.Groups; | ||
import com.moim.backend.domain.space.entity.Participation; | ||
import com.moim.backend.domain.space.response.BusGraphicDataResponse; | ||
import com.moim.backend.domain.space.response.BusPathResponse; | ||
import com.moim.backend.domain.space.response.CarMoveInfo; | ||
import com.moim.backend.domain.space.response.PlaceRouteResponse; | ||
import com.moim.backend.domain.subway.response.BestPlaceInterface; | ||
import com.moim.backend.domain.user.config.KakaoProperties; | ||
import com.moim.backend.global.aspect.TimeCheck; | ||
import com.moim.backend.global.util.LoggingUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.net.URI; | ||
import java.util.Optional; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class DirectionService { | ||
private final RestTemplate restTemplate = new RestTemplate(); | ||
private final OdsayProperties odsayProperties; | ||
private final KakaoProperties kakaoProperties; | ||
|
||
@TimeCheck | ||
public Optional<PlaceRouteResponse.MoveUserInfo> getBusRouteToResponse( | ||
BestPlaceInterface bestPlace, Groups group, Participation participation | ||
) { | ||
Optional<PlaceRouteResponse.MoveUserInfo> moveUserInfo = Optional.empty(); | ||
URI searchPathUri = odsayProperties.getSearchPathUriWithParams(bestPlace, participation); | ||
BusPathResponse busPathResponse = restTemplate.getForObject(searchPathUri, BusPathResponse.class); | ||
|
||
if (busPathResponse.getResult() == null) { | ||
LoggingUtil.builder() | ||
.title("버스 길찾기") | ||
.status("실패") | ||
.message("지역: " + bestPlace.getName() + ", url: " + searchPathUri) | ||
.build() | ||
.print(); | ||
} else { | ||
URI graphicDataUri = odsayProperties.getGraphicDataUriWIthParams(busPathResponse.getPathInfoMapObj()); | ||
BusGraphicDataResponse busGraphicDataResponse = restTemplate.getForObject( | ||
graphicDataUri, BusGraphicDataResponse.class | ||
); | ||
if (busPathResponse.getResult() == null) { | ||
LoggingUtil.builder() | ||
.title("버스 그래픽 데이터 조회") | ||
.status("실패") | ||
.message("지역: " + bestPlace.getName() + ", url: " + graphicDataUri) | ||
.build() | ||
.print(); | ||
} else { | ||
moveUserInfo = Optional.of(new PlaceRouteResponse.MoveUserInfo(group, participation, busGraphicDataResponse, busPathResponse)); | ||
} | ||
} | ||
return moveUserInfo; | ||
} | ||
|
||
@TimeCheck | ||
public Optional<PlaceRouteResponse.MoveUserInfo> getCarRouteToResponse( | ||
BestPlaceInterface bestPlace, Groups group, Participation participation | ||
) { | ||
Optional<PlaceRouteResponse.MoveUserInfo> moveUserInfo = Optional.empty(); | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("Authorization", "KakaoAK " + kakaoProperties.getClientId()); | ||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); | ||
|
||
URI searchCarPathUri = kakaoProperties.getSearchCarPathUriWithParams(bestPlace, participation); | ||
CarMoveInfo carMoveInfo = restTemplate.exchange( | ||
searchCarPathUri, HttpMethod.GET, new HttpEntity<>(headers), CarMoveInfo.class | ||
).getBody(); | ||
if (carMoveInfo.getRoutes() == null) { | ||
LoggingUtil.builder() | ||
.title("차 길찾기") | ||
.status("실패") | ||
.message("지역: " + bestPlace.getName() + ", url: " + searchCarPathUri) | ||
.build() | ||
.print(); | ||
} else { | ||
moveUserInfo = Optional.of(new PlaceRouteResponse.MoveUserInfo(group, participation, carMoveInfo)); | ||
} | ||
return moveUserInfo; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/moim/backend/global/aspect/TimeCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.moim.backend.global.aspect; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface TimeCheck { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.moim.backend.global.common; | ||
|
||
public class CacheName { | ||
public static final String group = "group"; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/moim/backend/global/config/RedisCacheConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.moim.backend.global.config; | ||
|
||
import org.springframework.cache.CacheManager; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.cache.RedisCacheConfiguration; | ||
import org.springframework.data.redis.cache.RedisCacheManager; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
@EnableCaching | ||
public class RedisCacheConfig { | ||
@Bean | ||
public CacheManager contentCacheManager(RedisConnectionFactory redisConnectionFactory) { | ||
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() | ||
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) | ||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); | ||
|
||
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory).cacheDefaults(redisCacheConfiguration).build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/com/moim/backend/global/filter/TimeCheckFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.moim.backend.global.filter; | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.filter.Filter; | ||
import ch.qos.logback.core.spi.FilterReply; | ||
|
||
public class TimeCheckFilter extends Filter<ILoggingEvent> { | ||
@Override | ||
public FilterReply decide(ILoggingEvent event) { | ||
return (event.getLoggerName().equals("com.moim.backend.global.aspect.PerformanceAspect")) | ||
? FilterReply.ACCEPT | ||
: FilterReply.DENY; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/moim/backend/global/util/LoggingUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.moim.backend.global.util; | ||
|
||
import lombok.Builder; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Builder | ||
public class LoggingUtil { | ||
private String title; | ||
private String status = ""; | ||
private String message; | ||
|
||
public void print() { | ||
log.info("[ {} {} ] ========================================", title, status); | ||
log.info(message); | ||
log.info("================================================================="); | ||
} | ||
} |
Oops, something went wrong.