-
Notifications
You must be signed in to change notification settings - Fork 0
[Refactor] Redis 추가 #86
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
Changes from all commits
e067ec3
62eced1
4428384
25c110c
3d7d0b5
c3a51d8
59ad3d5
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 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||||||||||||||||||
| package com.blockguard.server.global.config; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import org.springframework.beans.factory.annotation.Value; | ||||||||||||||||||||||
| 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.connection.lettuce.LettuceConnectionFactory; | ||||||||||||||||||||||
| import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; | ||||||||||||||||||||||
| import org.springframework.data.redis.serializer.RedisSerializationContext; | ||||||||||||||||||||||
| import org.springframework.data.redis.serializer.StringRedisSerializer; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| import java.time.Duration; | ||||||||||||||||||||||
| import java.util.HashMap; | ||||||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @EnableCaching | ||||||||||||||||||||||
| @Configuration | ||||||||||||||||||||||
| public class RedisConfig { | ||||||||||||||||||||||
| @Value("${spring.data.redis.host}") | ||||||||||||||||||||||
| private String host; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @Value("${spring.data.redis.port}") | ||||||||||||||||||||||
| private int port; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @Bean | ||||||||||||||||||||||
| public RedisConnectionFactory redisConnectionFactory() { | ||||||||||||||||||||||
| return new LettuceConnectionFactory(host, port); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
Comment on lines
+23
to
+33
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. 직접 정의한 RedisConnectionFactory가 비밀번호/DB/SSL/타임아웃을 무시합니다 Spring Boot의 자동 설정을 덮어써서 적용 diff: - @Value("${spring.data.redis.host}")
- private String host;
-
- @Value("${spring.data.redis.port}")
- private int port;
-
- @Bean
- public RedisConnectionFactory redisConnectionFactory() {
- return new LettuceConnectionFactory(host, port);
- }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
| @Bean | ||||||||||||||||||||||
| public CacheManager cacheManager(RedisConnectionFactory cf) { | ||||||||||||||||||||||
| RedisCacheConfiguration defaultConfig = RedisCacheConfiguration.defaultCacheConfig() | ||||||||||||||||||||||
| .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())) | ||||||||||||||||||||||
| .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) | ||||||||||||||||||||||
| .entryTtl(Duration.ofMinutes(30)); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Map<String, RedisCacheConfiguration> cacheConfigs = new HashMap<>(); | ||||||||||||||||||||||
| cacheConfigs.put("news:list:v2", defaultConfig.entryTtl(Duration.ofHours(24))); // 뉴스는 하루 TTL | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return RedisCacheManager. | ||||||||||||||||||||||
| RedisCacheManagerBuilder. | ||||||||||||||||||||||
| fromConnectionFactory(cf) | ||||||||||||||||||||||
| .cacheDefaults(defaultConfig) | ||||||||||||||||||||||
| .withInitialCacheConfigurations(cacheConfigs) | ||||||||||||||||||||||
| .build(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
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.
🛠️ Refactor suggestion
상대시간 필드가 포함된 DTO를 장시간 캐시하면 UI가 stale 됩니다 — 시간 버킷 포함 또는 TTL 단축 권장
현재 DTO(NewsArticleResponse.publishedAt)가 “x분 전” 식으로 포맷된 문자열이라 서비스 레벨 캐시(요약상 24h)와 충돌합니다. 두 가지 경로 중 하나를 선택하세요.
빠른 적용용 키 변경 예:
추가로 빈 결과는 캐시하지 않도록 할 수 있습니다(선택):
📝 Committable suggestion