Skip to content

Commit 2d04cd5

Browse files
committed
feat: Redis 캐싱 적용
1 parent ae92e64 commit 2d04cd5

File tree

5 files changed

+82
-4
lines changed

5 files changed

+82
-4
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
2828
implementation 'mysql:mysql-connector-java:8.0.33'
2929
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
30+
implementation 'org.springframework.boot:spring-boot-starter-cache'
3031
// implementation 'org.springframework.boot:spring-boot-starter-security'
3132
implementation 'org.springframework.boot:spring-boot-starter-web'
3233

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package FITDAY.community.repository;
2+
3+
import FITDAY.community.entity.Community;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface CommunityRepository extends JpaRepository<Community, Long> {
9+
long count();
10+
11+
void deleteById(Long id);
12+
}

src/main/java/FITDAY/community/service/impl/CommunityServiceImpl.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import FITDAY.community.entity.QCategory;
66
import FITDAY.community.entity.QCommunity;
77
import FITDAY.community.service.CommunityService;
8+
import FITDAY.redis.community.CountCacheService;
89
import com.querydsl.core.types.Projections;
910
import com.querydsl.jpa.impl.JPAQueryFactory;
1011
import lombok.RequiredArgsConstructor;
@@ -23,6 +24,7 @@
2324
public class CommunityServiceImpl implements CommunityService {
2425

2526
private final JPAQueryFactory jpaQueryFactory;
27+
private final CountCacheService cached;
2628
private final QCommunity qCommunity = QCommunity.community;
2729
private final QCategory qCategory = QCategory.category;
2830

@@ -74,10 +76,7 @@ public ResponseEntity<Page<CommunityResponseDto>> getCommunityList(Pageable page
7476
.limit(pageable.getPageSize())
7577
.fetch();
7678

77-
long total = jpaQueryFactory
78-
.select(qCommunity.id.count())
79-
.from(qCommunity)
80-
.fetchOne();
79+
long total = cached.getCount();
8180

8281
return ResponseEntity.ok(new PageImpl<>(content, pageable, total));
8382
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package FITDAY.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.data.redis.connection.RedisConnectionFactory;
6+
import org.springframework.data.redis.core.RedisTemplate;
7+
import org.springframework.data.redis.serializer.GenericToStringSerializer;
8+
import org.springframework.data.redis.serializer.StringRedisSerializer;
9+
10+
@Configuration
11+
public class RedisConfig {
12+
13+
@Bean
14+
public RedisTemplate<String, Long> redisTemplate(RedisConnectionFactory factory) {
15+
RedisTemplate<String, Long> template = new RedisTemplate<>();
16+
template.setConnectionFactory(factory);
17+
template.setKeySerializer(new StringRedisSerializer());
18+
template.setValueSerializer(new GenericToStringSerializer<>(Long.class));
19+
template.afterPropertiesSet();
20+
return template;
21+
}
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package FITDAY.redis.community;
2+
3+
import FITDAY.community.repository.CommunityRepository;
4+
import FITDAY.config.RedisConfig;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.boot.context.event.ApplicationReadyEvent;
7+
import org.springframework.context.event.EventListener;
8+
import org.springframework.data.redis.core.RedisTemplate;
9+
import org.springframework.stereotype.Service;
10+
11+
import java.util.concurrent.TimeUnit;
12+
13+
@Service
14+
@RequiredArgsConstructor
15+
public class CountCacheService {
16+
17+
private static final String KEY = "community-count";
18+
private final RedisTemplate<String, Long> redisTemplate;
19+
private final CommunityRepository communityRepository;
20+
21+
@EventListener(ApplicationReadyEvent.class)
22+
public void warmup() {
23+
Long count = communityRepository.count();
24+
redisTemplate.opsForValue().set(KEY, count, 1, TimeUnit.DAYS);
25+
}
26+
27+
public long getCount() {
28+
Long cached = redisTemplate.opsForValue().get(KEY);
29+
if(cached != null) {
30+
return cached;
31+
}
32+
long cnt = communityRepository.count();
33+
redisTemplate.opsForValue().set(KEY, cnt, 1, TimeUnit.DAYS);
34+
return cnt;
35+
}
36+
37+
public void increment() {
38+
redisTemplate.opsForValue().increment(KEY, 1);
39+
}
40+
41+
public void decrement() {
42+
redisTemplate.opsForValue().decrement(KEY, 1);
43+
}
44+
}

0 commit comments

Comments
 (0)