Skip to content

Commit

Permalink
Merge pull request #112 from TeamMiso/feat/redis-cache
Browse files Browse the repository at this point in the history
πŸ”€ :: 쑰회 λ‘œμ§μ— μΊμ‹œ 적용
  • Loading branch information
uuuuuuuk authored Apr 22, 2024
2 parents 7791661 + 25999d6 commit d0c3616
Show file tree
Hide file tree
Showing 22 changed files with 92 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package andreas311.miso.domain.environment.application.port.input.dto
import andreas311.miso.domain.environment.domain.TodayEnvironment

data class DetailEnvironmentDto(
val title: String,
val content: String,
val imageUrl: String?
val title: String = "",
val content: String = "",
val imageUrl: String? = ""
) {
constructor(todayEnvironment: TodayEnvironment): this(
title = todayEnvironment.title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import andreas311.miso.common.annotation.ReadOnlyRollbackService
import andreas311.miso.domain.environment.application.port.input.DetailEnvironmentUseCase
import andreas311.miso.domain.environment.application.port.input.dto.DetailEnvironmentDto
import andreas311.miso.domain.environment.application.port.output.QueryTodayEnvironmentPort
import org.springframework.cache.annotation.Cacheable

@ReadOnlyRollbackService
class DetailEnvironmentService(
private val queryTodayEnvironmentPort: QueryTodayEnvironmentPort
) : DetailEnvironmentUseCase {
@Cacheable(cacheNames = ["environment"], key = "'todayEnvironment'", cacheManager = "redisCacheManager")
override fun execute(): DetailEnvironmentDto {
val environment = queryTodayEnvironmentPort.findOne()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package andreas311.miso.domain.item.application.port.input.dto
import andreas311.miso.domain.item.domain.Item

data class DetailItemDto(
val id: Long,
val price: Int,
val amount: Int,
val name: String,
val content: String,
val imageUrl: String?
val id: Long = 0L,
val price: Int = 0,
val amount: Int = 0,
val name: String = "",
val content: String = "",
val imageUrl: String? = ""
) {
constructor(item: Item) : this(
id = item.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package andreas311.miso.domain.item.application.port.input.dto
import andreas311.miso.domain.item.domain.Item

data class ItemDto(
val id: Long,
val price: Int,
val amount: Int,
val name: String,
val imageUrl: String?
val id: Long = 0L,
val price: Int = 0,
val amount: Int = 0,
val name: String = "",
val imageUrl: String? = ""
) {
constructor(item: Item) : this(
id = item.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import andreas311.miso.domain.item.application.exception.ItemNotFoundException
import andreas311.miso.domain.item.application.port.input.DeleteItemUseCase
import andreas311.miso.domain.item.application.port.output.CommandItemPort
import andreas311.miso.domain.item.application.port.output.QueryItemPort
import org.springframework.cache.annotation.CacheEvict

@RollbackService
class DeleteItemService(
private val queryItemPort: QueryItemPort,
private val commandItemPort: CommandItemPort
) : DeleteItemUseCase {
@CacheEvict(cacheNames = ["item"], key = "#id", cacheManager = "redisCacheManager")
override fun execute(id: Long) {
val itemEntity = queryItemPort.findByIdOrNull(id)
?: throw ItemNotFoundException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import andreas311.miso.domain.item.application.exception.ItemNotFoundException
import andreas311.miso.domain.item.application.port.input.DetailItemUseCase
import andreas311.miso.domain.item.application.port.input.dto.DetailItemDto
import andreas311.miso.domain.item.application.port.output.QueryItemPort
import org.springframework.cache.annotation.Cacheable

@ReadOnlyRollbackService
class DetailItemService(
private val queryItemPort: QueryItemPort
) : DetailItemUseCase {
@Cacheable(cacheNames = ["item"], key = "#id", cacheManager = "redisCacheManager")
override fun execute(id: Long): DetailItemDto {
val item = queryItemPort.findByIdOrNull(id)
?: throw ItemNotFoundException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import andreas311.miso.domain.item.application.port.output.CommandItemPort
import andreas311.miso.domain.item.application.port.output.QueryItemPort
import andreas311.miso.domain.item.domain.Item
import andreas311.miso.thirdparty.aws.s3.util.S3Util
import org.springframework.cache.annotation.CacheEvict
import org.springframework.stereotype.Component
import org.springframework.web.multipart.MultipartFile

Expand All @@ -16,6 +17,7 @@ class EditItemService(
private val queryItemPort: QueryItemPort,
private val commandItemPort: CommandItemPort
) : EditItemUseCase {
@CacheEvict(cacheNames = ["item"], key = "#id", cacheManager = "redisCacheManager")
override fun execute(id: Long, editItemDto: EditItemDto, multipartFile: MultipartFile?) {
val item = queryItemPort.findByIdOrNull(id)
?: throw ItemNotFoundException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package andreas311.miso.domain.notification.application.port.input.dto
import andreas311.miso.domain.notification.domain.Notification

data class DetailNotificationDto(
val answer: String
val answer: String = ""
) {
constructor(notification: Notification) : this(
answer = notification.answer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import andreas311.miso.domain.notification.application.exception.NotificationNot
import andreas311.miso.domain.notification.application.port.input.DetailNotificationUseCase
import andreas311.miso.domain.notification.application.port.input.dto.DetailNotificationDto
import andreas311.miso.domain.notification.application.port.output.QueryNotificationPort
import org.springframework.cache.annotation.Cacheable

@ReadOnlyRollbackService
class DetailNotificationService(
private val queryNotificationPort: QueryNotificationPort
) : DetailNotificationUseCase {
@Cacheable(cacheNames = ["inquiryRespond"], key = "#id", cacheManager = "redisCacheManager")
override fun execute(id: Long): DetailNotificationDto {
val notification = queryNotificationPort.findByInquiryIdOrNull(id)
?: throw NotificationNotFoundException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import andreas311.miso.domain.purchase.application.port.input.PurchaseItemUseCas
import andreas311.miso.domain.purchase.application.port.output.CommandPurchasePort
import andreas311.miso.domain.purchase.domain.Purchase
import andreas311.miso.domain.user.application.port.output.CommandUserPort
import org.springframework.cache.annotation.CacheEvict
import java.time.LocalDateTime

@RollbackService
Expand All @@ -21,6 +22,7 @@ class PurchaseItemService(
private val userSecurityPort: UserSecurityPort,
private val commandPurchasePort: CommandPurchasePort
) : PurchaseItemUseCase {
@CacheEvict(cacheNames = ["userPoint"], key = "'userPoint'", cacheManager = "redisCacheManager")
override fun execute(id: Long) {
val user = userSecurityPort.currentUser()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import andreas311.miso.domain.recyclables.domain.Recyclables
import andreas311.miso.domain.recyclables.domain.RecyclablesType

data class DetailRecyclablesDto(
val id: Long,
val title: String,
val subTitle: String,
val recycleMethod: String,
val recycleTip: String,
val recycleCaution: String,
val imageUrl: String?,
val recyclablesType: RecyclablesType,
val recycleMark: String
val id: Long = 0L,
val title: String = "",
val subTitle: String = "",
val recycleMethod: String = "",
val recycleTip: String = "",
val recycleCaution: String = "",
val imageUrl: String? = "",
val recyclablesType: RecyclablesType = RecyclablesType.BAG,
val recycleMark: String = ""
) {
constructor(recyclables: Recyclables) : this(
id = recyclables.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import andreas311.miso.domain.recyclables.application.exception.RecyclablesNotFo
import andreas311.miso.domain.recyclables.application.port.input.DeleteRecyclablesUseCase
import andreas311.miso.domain.recyclables.application.port.output.CommandRecyclablesPort
import andreas311.miso.domain.recyclables.application.port.output.QueryRecyclablesPort
import org.springframework.cache.annotation.CacheEvict

@RollbackService
class DeleteRecyclablesService(
private val queryRecyclablesPort: QueryRecyclablesPort,
private val commandRecyclablesPort: CommandRecyclablesPort
) : DeleteRecyclablesUseCase {
@CacheEvict(cacheNames = ["recyclables"], key = "#id", cacheManager = "redisCacheManager")
override fun execute(id: Long) {
val recyclablesEntity = queryRecyclablesPort.findByIdOrNull(id)
?: throw RecyclablesNotFoundException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import andreas311.miso.domain.recyclables.application.port.input.DetailRecyclabl
import andreas311.miso.domain.recyclables.application.port.input.dto.DetailRecyclablesDto
import andreas311.miso.domain.recyclables.application.port.output.QueryRecyclablesPort
import andreas311.miso.domain.recyclables.domain.RecyclablesType
import org.springframework.cache.annotation.Cacheable

@ReadOnlyRollbackService
class DetailRecyclablesService(
private val queryRecyclablesPort: QueryRecyclablesPort
) : DetailRecyclablesUseCase {
@Cacheable(cacheNames = ["recyclables"], key = "#recyclablesType", cacheManager = "redisCacheManager")
override fun execute(recyclablesType: RecyclablesType): DetailRecyclablesDto {
val recyclables = queryRecyclablesPort.findByRecyclablesTypeOrNull(recyclablesType)
?: throw RecyclablesNotFoundException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import andreas311.miso.domain.recyclables.application.port.output.CommandRecycla
import andreas311.miso.domain.recyclables.application.port.output.QueryRecyclablesPort
import andreas311.miso.domain.recyclables.domain.Recyclables
import andreas311.miso.thirdparty.aws.s3.util.S3Util
import org.springframework.cache.annotation.CacheEvict
import org.springframework.web.multipart.MultipartFile

@RollbackService
Expand All @@ -16,6 +17,7 @@ class EditRecyclablesService(
private val queryRecyclablesPort: QueryRecyclablesPort,
private val commandRecyclablesPort: CommandRecyclablesPort
) : EditRecyclablesUseCase {
@CacheEvict(cacheNames = ["recyclables"], key = "#editRecyclablesDto.recyclablesType", cacheManager = "redisCacheManager")
override fun execute(id: Long, editRecyclablesDto: EditRecyclablesDto, multipartFile: MultipartFile?) {
val recyclables = queryRecyclablesPort.findByIdOrNull(id)
?: throw RecyclablesNotFoundException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package andreas311.miso.domain.user.application.port.input.dto
import andreas311.miso.domain.user.domain.User

data class PointDto(
val point: Int
val point: Int = 0
) {
constructor(user: User) : this(
point = user.point
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import andreas311.miso.domain.user.domain.User
import java.util.*

data class UserInfoDto(
val id: UUID,
val email: String,
val role: Role
val id: UUID = UUID.randomUUID(),
val email: String = "",
val role: Role = Role.ROLE_USER
) {
constructor(user: User) : this(
id = user.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import andreas311.miso.common.annotation.ReadOnlyRollbackService
import andreas311.miso.domain.auth.application.port.output.UserSecurityPort
import andreas311.miso.domain.user.application.port.input.GetPointUseCase
import andreas311.miso.domain.user.application.port.input.dto.PointDto
import org.springframework.cache.annotation.Cacheable

@ReadOnlyRollbackService
class GetPointService(
private val userSecurityPort: UserSecurityPort
) : GetPointUseCase {
@Cacheable(cacheNames = ["userPoint"], key = "'userPoint'", cacheManager = "redisCacheManager")
override fun execute(): PointDto {
val user = userSecurityPort.currentUser()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import andreas311.miso.common.annotation.RollbackService
import andreas311.miso.domain.auth.application.port.output.UserSecurityPort
import andreas311.miso.domain.user.application.port.input.GivePointUseCase
import andreas311.miso.domain.user.application.port.output.CommandUserPort
import org.springframework.cache.annotation.CacheEvict

@RollbackService
class GivePointService(
private val commandUserPort: CommandUserPort,
private val userSecurityPort: UserSecurityPort
) : GivePointUseCase {
@CacheEvict(cacheNames = ["userPoint"], key = "'userPoint'", cacheManager = "redisCacheManager")
override fun execute() {
val user = userSecurityPort.currentUser()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import andreas311.miso.common.annotation.ReadOnlyRollbackService
import andreas311.miso.domain.auth.application.port.output.UserSecurityPort
import andreas311.miso.domain.user.application.port.input.UserInfoUseCase
import andreas311.miso.domain.user.application.port.input.dto.UserInfoDto
import org.springframework.cache.annotation.Cacheable

@ReadOnlyRollbackService
class UserInfoService(
private val userSecurityPort: UserSecurityPort
) : UserInfoUseCase {
@Cacheable(cacheNames = ["user"], key = "'userInfo'", cacheManager = "redisCacheManager")
override fun execute(): UserInfoDto {
val user = userSecurityPort.currentUser()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package andreas311.miso.global.config

import org.springframework.cache.annotation.EnableCaching
import org.springframework.context.annotation.Configuration

@Configuration
@EnableCaching
class EnableCachingConfig
31 changes: 31 additions & 0 deletions src/main/kotlin/andreas311/miso/global/redis/RedisCacheConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package andreas311.miso.global.redis

import org.springframework.cache.CacheManager
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.serializer.GenericJackson2JsonRedisSerializer
import org.springframework.data.redis.serializer.RedisSerializationContext
import org.springframework.data.redis.serializer.StringRedisSerializer
import java.time.Duration

@Configuration
class RedisCacheConfig {
@Bean
fun redisCacheManager(redisConnectionFactory: RedisConnectionFactory): CacheManager {
val redisCacheConfiguration = RedisCacheConfiguration
.defaultCacheConfig()
.disableCachingNullValues()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(GenericJackson2JsonRedisSerializer()))
.entryTtl(Duration.ofMinutes(30))

return RedisCacheManager
.RedisCacheManagerBuilder
.fromConnectionFactory(redisConnectionFactory)
.cacheDefaults(redisCacheConfiguration)
.build()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class RedisConfig(
LettuceConnectionFactory(redisProperties.host, redisProperties.port)

@Bean
fun redisTemplate(): RedisTemplate<String, String> {
fun redisTemplateManager(): RedisTemplate<String, String> {
val redisTemplate = RedisTemplate<String, String>()
redisTemplate.keySerializer = StringRedisSerializer()
redisTemplate.valueSerializer = StringRedisSerializer()
Expand Down

0 comments on commit d0c3616

Please sign in to comment.