Skip to content

Commit

Permalink
Merge pull request #420 from Team-Ampersand/fix/update-member-cache
Browse files Browse the repository at this point in the history
Fix/update member cache
  • Loading branch information
esperar authored Sep 6, 2024
2 parents 39d80ac + 14f4726 commit e64b7f4
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @uuuuuuuk @esperar @KimGyeongsuuu
* @uuuuuuuk @esperar @KimGyeongsuuu @m2ri1 @ta2ye0n @yena5511
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,10 @@ class Member(
return this
}

fun updateMemberInfo(request: ModifyStudentInfoRequest) : Member {
fun updateMemberInfo(request: ModifyStudentInfoRequest) {
this.memberName = request.memberName
this.stuNum = request.stuNum
this.gender = request.gender
this.roles = Collections.singletonList(request.role)
return this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class StuInfoController(
private val searchStudentService: SearchStudentService,
private val modifyStudentInfoService: ModifyStudentInfoService
) {

@GetMapping
fun findAllStudentInfo(): ResponseEntity<List<FindAllStudentResDto>> =
findAllMemberService.execute()
.let { ResponseEntity.ok().body(it) }
.let { ResponseEntity.ok().body(it.students) }

@GetMapping("/search")
fun searchStudent(searchRequestDto: SearchRequestDto): ResponseEntity<List<SearchStudentListResDto>> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ data class FindAllStudentResDto(
val selfStudyStatus: SelfStudyStatus,
val profileImage: String?
) {

companion object {
fun of(member: Member): FindAllStudentResDto = FindAllStudentResDto(
id = member.id,
Expand All @@ -25,4 +26,9 @@ data class FindAllStudentResDto(
profileImage = member.profileImage
)
}
}

}

data class FindAllStudentListResDto(
val students: List<FindAllStudentResDto>
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.dotori.v2.domain.student.service

import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentResDto
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentListResDto

interface FindAllMemberService {
fun execute(): List<FindAllStudentResDto>
fun execute(): FindAllStudentListResDto
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dotori.v2.domain.student.service.impl

import com.dotori.v2.domain.member.domain.repository.MemberRepository
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentListResDto
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentResDto
import com.dotori.v2.domain.student.service.FindAllMemberService
import com.dotori.v2.global.config.redis.service.RedisCacheService
Expand All @@ -18,27 +19,22 @@ class FindAllMemberServiceImpl(

val CACHE_KEY = "memberList"

override fun execute(): List<FindAllStudentResDto> {
override fun execute(): FindAllStudentListResDto {

val cachedData = redisCacheService.getFromCache(CACHE_KEY)

if (cachedData != null) {
return cachedData as List<FindAllStudentResDto>
return cachedData as FindAllStudentListResDto
}

val members = memberRepository.findAll(Sort.by(Sort.Direction.ASC, "stuNum")).map {
FindAllStudentResDto(
id = it.id,
gender = it.gender,
memberName = it.memberName,
stuNum = it.stuNum,
role = it.roles[0],
selfStudyStatus = it.selfStudyStatus,
profileImage = it.profileImage
)
FindAllStudentResDto.of(it)
}

redisCacheService.putToCache(CACHE_KEY, members)
val response = FindAllStudentListResDto(members)

redisCacheService.putToCache(CACHE_KEY, response)

return members
return response
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.dotori.v2.domain.member.domain.repository.MemberRepository
import com.dotori.v2.domain.member.exception.MemberNotFoundException
import com.dotori.v2.domain.music.presentation.data.res.MusicListResDto
import com.dotori.v2.domain.student.presentation.data.req.ModifyStudentInfoRequest
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentListResDto
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentResDto
import com.dotori.v2.domain.student.service.ModifyStudentInfoService
import com.dotori.v2.global.config.redis.service.RedisCacheService
Expand All @@ -25,18 +26,16 @@ class ModifyStudentInfoServiceImpl(
override fun execute(modifyStudentInfoRequest: ModifyStudentInfoRequest) {
val member = memberRepository.findByIdOrNull(modifyStudentInfoRequest.memberId)
?: throw MemberNotFoundException()

val updated = memberRepository.save(member.updateMemberInfo(modifyStudentInfoRequest))
updateCache(updated)
member.updateMemberInfo(modifyStudentInfoRequest)
initCache()
}

private fun updateCache(member: Member) {
val cachedData = redisCacheService.getFromCache(CACHE_KEY) as? List<FindAllStudentResDto>
private fun initCache() {
val cachedData =
redisCacheService.getFromCache(CACHE_KEY) as? FindAllStudentListResDto

if (cachedData != null) {
val content = cachedData.toMutableList()
content.add(FindAllStudentResDto.of(member))
redisCacheService.putToCache(CACHE_KEY, content)
redisCacheService.deleteFromCache(CACHE_KEY)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RedisCacheConfig {
BasicPolymorphicTypeValidator.builder()
.allowIfBaseType(Any::class.java)
.build(),
ObjectMapper.DefaultTyping.EVERYTHING
ObjectMapper.DefaultTyping.EVERYTHING,
)
}
}
Expand Down

0 comments on commit e64b7f4

Please sign in to comment.