Skip to content

Commit

Permalink
Refactor :: Notification Cache 적용 #346
Browse files Browse the repository at this point in the history
기존 schedules 랑 달리 생성, 수정, 삭제 시 cache 값 변동이 필요하여 기존과 다른 로직 변경
  • Loading branch information
yeseong0412 committed Nov 26, 2024
1 parent e057ff7 commit 14b5bf8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
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 14b5bf8

Please sign in to comment.