-
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.
[UNI-255] refactor: 캐시를 활용한 응답 시간 개선 & 직렬화 시간 개선 (#197)
* [UNI-255] feat: redis 환경 세팅 * [UNI-255] feat: heap 사이즈 500으로 조정 * [UNI-255] refactor: 경량 객체 구현 * [UNI-255] feat: 경량 객체에 사용되는 계산기 구현 * [UNI-255] feat: redis 서비스 구현 * [UNI-255] feat: 로컬 캐시 버전 구현 * [UNI-255] feat: 캐시 무효화 구현 * [UNI-255] feat: 롤백시 캐시 무효화 구현 * [UNI-255] refactor: fastJson 직렬화 구현 * [UNI-255] test * [UNI-255] fix: json 지원 * [UNI-255] refactor: 캐시 로직 적용 * [UNI-255] fix: redis 컨테이너 추가 * [UNI-255] fix: redis 컨테이너 추가 & 필요없는 설정 제거 * [UNI-255] test cd 삭제 * [UNI-255] fix: redis 컨테이너 설정 수정 * [UNI-255] fix: redis 컨테이너 설정 수정 해결 * [UNI-255] fix: 필요없는 설정 제거
- Loading branch information
Showing
16 changed files
with
449 additions
and
17 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
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
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
30 changes: 30 additions & 0 deletions
30
uniro_backend/src/main/java/com/softeer5/uniro_backend/common/redis/RedisConfig.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,30 @@ | ||
package com.softeer5.uniro_backend.common.redis; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
import com.alibaba.fastjson2.support.spring6.data.redis.FastJsonRedisSerializer; | ||
import com.softeer5.uniro_backend.map.service.vo.LightRoutes; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Bean | ||
public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { | ||
RedisTemplate template = new RedisTemplate<>(); | ||
template.setConnectionFactory(connectionFactory); | ||
|
||
// 키는 String, 값은 FastJson으로 변환된 JSON 문자열 저장 | ||
StringRedisSerializer serializer = new StringRedisSerializer(); | ||
FastJsonRedisSerializer<LightRoutes> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(LightRoutes.class); | ||
|
||
template.setKeySerializer(serializer); | ||
template.setValueSerializer(fastJsonRedisSerializer); | ||
|
||
return template; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
uniro_backend/src/main/java/com/softeer5/uniro_backend/common/redis/RedisService.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,52 @@ | ||
package com.softeer5.uniro_backend.common.redis; | ||
|
||
import java.time.Duration; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class RedisService { | ||
|
||
private final RedisTemplate redisTemplate; | ||
private final Map<String, Boolean> cacheMap = new HashMap<>(); | ||
|
||
public void saveData(String key, Object value) { | ||
long startTime = System.nanoTime(); | ||
redisTemplate.opsForValue().set(key, value, Duration.ofMinutes(1000)); // 10분 TTL | ||
long endTime = System.nanoTime(); | ||
|
||
long duration = TimeUnit.NANOSECONDS.toMicros(endTime - startTime); | ||
log.info("🔴🔴🔴🔴🔴🔴🔴 Redis 직렬화 및 저장 시간: {} 마이크로초", duration); | ||
|
||
cacheMap.put(key, true); | ||
} | ||
|
||
public Object getData(String key) { | ||
long startTime = System.nanoTime(); | ||
Object data = redisTemplate.opsForValue().get(key); | ||
long endTime = System.nanoTime(); | ||
|
||
long duration = TimeUnit.NANOSECONDS.toMicros(endTime - startTime); | ||
log.info("🟢🟢🟢🟢🟢🟢🟢 Redis 역직렬화 및 조회 시간: {} 마이크로초", duration); | ||
cacheMap.put(key, true); | ||
return data; | ||
} | ||
|
||
public boolean hasData(String key){ | ||
return cacheMap.containsKey(key); | ||
} | ||
|
||
public void deleteData(String key) { | ||
redisTemplate.delete(key); | ||
cacheMap.remove(key); | ||
} | ||
} |
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
Oops, something went wrong.