Skip to content

Commit

Permalink
Merge pull request #350 from apeun-gidaechi/refactor/#346
Browse files Browse the repository at this point in the history
Refactor :: Notification N+1 이슈 및 캐싱 적용 #346
  • Loading branch information
yeseong0412 authored Nov 26, 2024
2 parents aa025e0 + 14b5bf8 commit b1bf1c3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class NotificationEntity(
var content: String,

@ElementCollection
@Column(nullable = false)
val emoji: MutableList<NotificationEmoji> = mutableListOf(),

@Column(nullable = false, updatable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,27 @@ class NotificationRepositoryCustomImpl(
override fun findByWorkspaceId(workspaceId: String, pageable: Pageable): List<NotificationEntity> {
val notice: QNotificationEntity = QNotificationEntity.notificationEntity

return jpaQueryFactory
.select(notice)
val noticeIdList = jpaQueryFactory
.select(notice.id)
.from(notice)
.where(
notice.workspaceId.eq(workspaceId),
notice.deleted.isFalse
)
.offset(pageable.offset)
.limit(pageable.pageSize.toLong())
.orderBy(notice.id.desc())
.fetch().orEmpty().toList()

return jpaQueryFactory
.select(notice)
.from(notice)
.leftJoin(notice.user).fetchJoin()
.leftJoin(notice.emoji).fetchJoin()
.where(notice.id.`in`(noticeIdList))
.orderBy(notice.id.desc())
.fetch().orEmpty().toList()

}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.seugi.api.domain.notification.presentation.dto.response

data class NotificationEmojiResponse(
val emoji: String,
val userList: List<Long>,
val emoji: String? = null,
val userList: List<Long>? = null,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.seugi.api.domain.notification.service

import com.seugi.api.domain.notification.presentation.dto.response.NotificationResponse
import com.seugi.api.domain.notification.domain.NotificationRepository
import com.seugi.api.domain.notification.domain.mapper.NotificationMapper
import org.springframework.cache.annotation.Cacheable
import org.springframework.cache.annotation.CacheEvict
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional

@Service
class NotificationCacheService(
private val noticeRepository: NotificationRepository,
private val noticeMapper: NotificationMapper
) {

@Transactional(readOnly = true)
@Cacheable(value = ["notifications"], key = "#workspaceId")
fun getNotices(workspaceId: String, pageable: Pageable): List<NotificationResponse> {
return noticeRepository.findByWorkspaceId(workspaceId, pageable).map {
noticeMapper.transferNoticeResponse(
noticeMapper.toDomain(it)
)
}
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
@CacheEvict(value = ["notifications"], key = "#workspaceId")
fun deleteCache(workspaceId: String) {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class NotificationServiceImpl(
private val noticeRepository: NotificationRepository,
private val noticeMapper: NotificationMapper,
private val fcmService: FCMService,
private val notificationCacheService: NotificationCacheService
) : NotificationService {

@Transactional(readOnly = true)
Expand Down Expand Up @@ -52,7 +53,7 @@ class NotificationServiceImpl(
noticeRepository.save(notice)

sendAlert(workspaceEntity, userId, createNoticeRequest.title)

notificationCacheService.deleteCache(createNoticeRequest.workspaceId)
}

@Transactional(readOnly = true)
Expand All @@ -61,12 +62,7 @@ class NotificationServiceImpl(
userId: Long,
pageable: Pageable,
): List<NotificationResponse> {

return noticeRepository.findByWorkspaceId(workspaceId, pageable).map {
noticeMapper.transferNoticeResponse(
noticeMapper.toDomain(it)
)
}
return notificationCacheService.getNotices(workspaceId, pageable)
}

@Transactional
Expand All @@ -76,9 +72,8 @@ class NotificationServiceImpl(
if (noticeEntity.user!!.id != userId) throw CustomException(NotificationErrorCode.FORBIDDEN)

noticeEntity.updateNotice(updateNoticeRequest)

noticeRepository.save(noticeEntity)

notificationCacheService.deleteCache(noticeEntity.workspaceId)
}

@Transactional
Expand All @@ -95,7 +90,7 @@ class NotificationServiceImpl(

notice.deleteNotice()
noticeRepository.save(notice)

notificationCacheService.deleteCache(workspaceId)
}

private fun addEmoji(
Expand Down Expand Up @@ -131,8 +126,6 @@ class NotificationServiceImpl(
}

noticeRepository.save(notification)

notificationCacheService.deleteCache(notification.workspaceId)
}


}
2 changes: 2 additions & 0 deletions src/main/kotlin/com/seugi/api/global/config/RedisConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ class RedisConfig(
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(GenericJackson2JsonRedisSerializer()))

val schedulesCacheConfig = defaultCacheConfig.entryTtl(Duration.ofDays(1))
val notificationCacheConfig = defaultCacheConfig.entryTtl(Duration.ofHours(1))

val cacheConfigurations: MutableMap<String, RedisCacheConfiguration> = mutableMapOf()
cacheConfigurations["schedules"] = schedulesCacheConfig
cacheConfigurations["notification"] = notificationCacheConfig

return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory)
.cacheDefaults(defaultCacheConfig)
Expand Down

0 comments on commit b1bf1c3

Please sign in to comment.