Skip to content

Commit

Permalink
Merge pull request #347 from GSM-MSG/feature/346-redis-used-caching-a…
Browse files Browse the repository at this point in the history
…pply

🔀 :: 레디스 캐시 적용
  • Loading branch information
Umjiseung authored Sep 18, 2024
2 parents c77ed4a + ffd00fa commit f314ecc
Show file tree
Hide file tree
Showing 16 changed files with 83 additions and 17 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {
testImplementation(Dependencies.SPRING_SECURITY_TEST)
implementation(Dependencies.SPRING_AOP)
implementation(Dependencies.SPRING_ACTUATOR)
implementation(Dependencies.SPRING_CACHE)

// kotlin
implementation(Dependencies.JACKSON_MODULE_KOTLIN)
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ object Dependencies {
const val SPRING_SECURITY_TEST = "org.springframework.security:spring-security-test"
const val SPRING_AOP = "org.springframework.boot:spring-boot-starter-aop"
const val SPRING_ACTUATOR = "org.springframework.boot:spring-boot-starter-actuator"
const val SPRING_CACHE = "org.springframework.boot:spring-boot-starter-cache"

// jackson
const val JACKSON_MODULE_KOTLIN = "com.fasterxml.jackson.module:jackson-module-kotlin"
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/msg/gauth/GauthBackendApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package com.msg.gauth
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.ConfigurationPropertiesScan
import org.springframework.boot.runApplication
import org.springframework.cache.annotation.EnableCaching
import java.util.*

@SpringBootApplication
@ConfigurationPropertiesScan
@EnableCaching
class GauthBackendApplication

fun main(args: Array<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.apache.tika.Tika
import org.springframework.cache.annotation.CacheEvict
import org.springframework.web.multipart.MultipartFile

@TransactionalService
class ExcelParsingService(
private val userRepository: UserRepository,
) {

@CacheEvict(value = ["AcceptedUsers"], allEntries = true, cacheManager = "redisCacheManager")
fun execute(file: MultipartFile) {
val tika = Tika()
val detect = tika.detect(file.bytes)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.msg.gauth.domain.client.presentation.dto.response

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.msg.gauth.domain.client.Client
import com.msg.gauth.domain.client.enums.ServiceScope

data class SingleClientResDto(
val id: Long,
val clientId: String,
val serviceName: String,
val serviceUri: String,
val serviceScope: ServiceScope,
val serviceImgUrl: String
data class SingleClientResDto @JsonCreator constructor(
@JsonProperty("id") val id: Long,
@JsonProperty("clientId") val clientId: String,
@JsonProperty("serviceName") val serviceName: String,
@JsonProperty("serviceUri") val serviceUri: String,
@JsonProperty("serviceScope") val serviceScope: ServiceScope,
@JsonProperty("serviceImgUrl") val serviceImgUrl: String
) {

constructor(client: Client) : this(
id = client.id,
clientId = client.clientId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.msg.gauth.domain.client.exception.UserNotMatchException
import com.msg.gauth.domain.client.repository.ClientRepository
import com.msg.gauth.domain.user.util.UserUtil
import com.msg.gauth.global.annotation.service.TransactionalService
import org.springframework.cache.annotation.CacheEvict
import org.springframework.data.repository.findByIdOrNull

@TransactionalService
Expand All @@ -13,6 +14,7 @@ class DeleteClientService(
private val userUtil: UserUtil,
) {

@CacheEvict(value = ["Clients"], allEntries = true, cacheManager = "redisCacheManager")
fun execute(id: Long) {
val client = clientRepository.findByIdOrNull(id)
?: throw ClientNotFindException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package com.msg.gauth.domain.client.service
import com.msg.gauth.domain.client.repository.ClientRepository
import com.msg.gauth.domain.user.util.UserUtil
import com.msg.gauth.global.annotation.service.TransactionalService
import org.springframework.cache.annotation.CacheEvict

@TransactionalService
class DeleteClientsService(
private val clientRepository: ClientRepository,
private val userUtil: UserUtil
) {

@CacheEvict(value = ["Clients"], allEntries = true, cacheManager = "redisCacheManager")
fun execute(ids: List<Long>) {
val user = userUtil.fetchCurrentUser()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import com.msg.gauth.domain.client.enums.ServiceScope
import com.msg.gauth.domain.client.presentation.dto.response.SingleClientResDto
import com.msg.gauth.domain.client.repository.ClientRepository
import com.msg.gauth.global.annotation.service.ReadOnlyService
import org.springframework.cache.annotation.Cacheable

@ReadOnlyService
class GetAllClientsService(
private val clientRepository: ClientRepository
) {

@Cacheable(value = ["Clients"], cacheManager = "redisCacheManager")
fun execute(): List<SingleClientResDto> =
clientRepository.findByServiceScope(ServiceScope.PUBLIC)
.map { SingleClientResDto(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.msg.gauth.domain.client.presentation.dto.response.ClientRegisterResDt
import com.msg.gauth.domain.client.repository.ClientRepository
import com.msg.gauth.domain.user.util.UserUtil
import com.msg.gauth.global.annotation.service.TransactionalService
import org.springframework.cache.annotation.CacheEvict
import java.util.UUID

@TransactionalService
Expand All @@ -13,6 +14,7 @@ class RegisterClientService(
private val userUtil: UserUtil,
) {

@CacheEvict(value = ["Clients"], allEntries = true, cacheManager = "redisCacheManager")
fun execute(clientRegisterDto: ClientRegisterReqDto): ClientRegisterResDto {
val (clientSecret, clientId) = createUUID() to createUUID()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import com.msg.gauth.domain.client.exception.ClientNotFindException
import com.msg.gauth.domain.client.presentation.dto.request.ClientUpdateReqDto
import com.msg.gauth.domain.client.repository.ClientRepository
import com.msg.gauth.global.annotation.service.TransactionalService
import org.springframework.cache.annotation.CacheEvict
import org.springframework.data.repository.findByIdOrNull

@TransactionalService
class UpdateAnyClientService(
private val clientRepository: ClientRepository
) {

@CacheEvict(value = ["Clients"], allEntries = true, cacheManager = "redisCacheManager")
fun execute(id: Long, clientUpdateReqDto: ClientUpdateReqDto) {
val client: Client = clientRepository.findByIdOrNull(id)
?: throw ClientNotFindException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import com.msg.gauth.domain.client.presentation.dto.request.ClientUpdateReqDto
import com.msg.gauth.domain.client.repository.ClientRepository
import com.msg.gauth.domain.user.util.UserUtil
import com.msg.gauth.global.annotation.service.TransactionalService
import org.springframework.cache.annotation.CacheEvict

@TransactionalService
class UpdateClientService(
private val clientRepository: ClientRepository,
private val userUtil: UserUtil
) {

@CacheEvict(value = ["Clients"], allEntries = true, cacheManager = "redisCacheManager")
fun updateClient(id: Long, clientUpdateReqDto: ClientUpdateReqDto) {
val client = clientRepository.findByIdAndCreatedBy(id, userUtil.fetchCurrentUser())
?: throw ClientNotFindException()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.msg.gauth.domain.user.presentation.dto.response

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import com.msg.gauth.domain.user.User

data class SingleAcceptedUserResDto(
val id: Long,
val name: String,
val email: String,
val grade: Int?,
val classNum: Int?,
val num: Int?,
val profileUrl: String?
data class SingleAcceptedUserResDto @JsonCreator constructor(
@JsonProperty("id") val id: Long,
@JsonProperty("name") val name: String,
@JsonProperty("email") val email: String,
@JsonProperty("grade") val grade: Int?,
@JsonProperty("classNum") val classNum: Int?,
@JsonProperty("num") val num: Int?,
@JsonProperty("profileUrl") val profileUrl: String?
) {

constructor(user: User) : this(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import com.msg.gauth.domain.user.presentation.dto.request.AcceptUserReqDto
import com.msg.gauth.domain.user.repository.UserRepository
import com.msg.gauth.domain.user.repository.UserRoleRepository
import com.msg.gauth.global.annotation.service.TransactionalService
import org.springframework.cache.annotation.CacheEvict

@TransactionalService
class AcceptUserSignUpService(
private val userRepository: UserRepository,
private val userRoleRepository: UserRoleRepository
) {

@CacheEvict(value = ["AcceptedUsers"], allEntries = true, cacheManager = "redisCacheManager")
fun execute(id: Long, acceptUserReqDto: AcceptUserReqDto) {
val user = userRepository.findByIdAndState(id, UserState.PENDING)
?: throw UserNotFoundException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import com.msg.gauth.domain.user.presentation.dto.request.AcceptedUserRequest
import com.msg.gauth.domain.user.presentation.dto.response.SingleAcceptedUserResDto
import com.msg.gauth.domain.user.repository.UserRepository
import com.msg.gauth.global.annotation.service.ReadOnlyService
import org.springframework.cache.annotation.Cacheable

@ReadOnlyService
class GetAcceptedUsersService(
private val userRepository: UserRepository,
) {

@Cacheable(value = ["AcceptedUsers"], condition = "#request.grade == 0 && #request.classNum == 0 && #request.keyword.equals('') && #request.role.equals('ROLE_STUDENT')", cacheManager = "redisCacheManager")
fun execute(request: AcceptedUserRequest): List<SingleAcceptedUserResDto> =
when (request.role) {
UserRoleType.ROLE_STUDENT -> userRepository.search(request.grade, request.classNum, request.keyword)
Expand Down
36 changes: 36 additions & 0 deletions src/main/kotlin/com/msg/gauth/global/redis/RedisCacheConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.msg.gauth.global.redis

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

@Configuration
@EnableCaching
class RedisCacheConfig {

@Bean
fun redisCacheManager(factory: RedisConnectionFactory?): CacheManager {
val cacheConfig = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(StringRedisSerializer()))
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(
GenericJackson2JsonRedisSerializer()
)
)
.entryTtl(Duration.ofMinutes(10))

return RedisCacheManager
.RedisCacheManagerBuilder
.fromConnectionFactory(factory!!)
.cacheDefaults(cacheConfig)
.build()
}
}
5 changes: 5 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ spring:
password: ${DB_PASSWORD}
driver-class-name: ${DB_DRIVER_CLASS}

cache:
type: redis
cache-names:
- Clients
- AcceptedUsers

redis:
host: ${REDIS_HOST}
Expand Down

0 comments on commit f314ecc

Please sign in to comment.