Skip to content

Commit

Permalink
Merge pull request #418 from Team-Ampersand/fix/update-member-cache
Browse files Browse the repository at this point in the history
Fix 멤버 업데이트시 캐시 동기화
  • Loading branch information
esperar authored Sep 6, 2024
2 parents f75c97b + 6f29e5e commit 39d80ac
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.dotori.v2.domain.member.domain.entity

import com.dotori.v2.domain.member.enums.*
import com.dotori.v2.domain.rule.domain.entity.RuleViolation
import com.dotori.v2.domain.student.presentation.data.req.ModifyStudentInfoRequest
import com.dotori.v2.global.entity.BaseTimeEntity
import java.time.LocalDateTime
import javax.persistence.*
import java.util.*

@Entity
@Table(name = "member",
Expand All @@ -18,7 +20,7 @@ class Member(
val id: Long = 0,

@Column(name = "member_name", nullable = false)
val memberName: String,
var memberName: String,

@Column(name = "member_stu_num", nullable = false)
var stuNum: String,
Expand All @@ -30,13 +32,13 @@ class Member(

@Enumerated(EnumType.STRING)
@Column(name = "member_gender")
val gender: Gender,
var gender: Gender,

@Enumerated(EnumType.STRING)
@Column(name = "Role")
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "Role", joinColumns = [JoinColumn(name = "member_id")])
val roles: MutableList<Role>,
var roles: MutableList<Role>,

@OneToMany(mappedBy = "member")
val ruleViolation: MutableList<RuleViolation>,
Expand Down Expand Up @@ -108,4 +110,12 @@ class Member(
this.stuNum = period
return this
}

fun updateMemberInfo(request: ModifyStudentInfoRequest) : Member {
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
@@ -1,5 +1,6 @@
package com.dotori.v2.domain.student.presentation.data.res

import com.dotori.v2.domain.member.domain.entity.Member
import com.dotori.v2.domain.member.enums.Gender
import com.dotori.v2.domain.member.enums.Role
import com.dotori.v2.domain.member.enums.SelfStudyStatus
Expand All @@ -12,4 +13,16 @@ data class FindAllStudentResDto(
val role: Role,
val selfStudyStatus: SelfStudyStatus,
val profileImage: String?
)
) {
companion object {
fun of(member: Member): FindAllStudentResDto = FindAllStudentResDto(
id = member.id,
gender = member.gender,
memberName = member.memberName,
stuNum = member.stuNum,
role = member.roles[0],
selfStudyStatus = member.selfStudyStatus,
profileImage = member.profileImage
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.dotori.v2.domain.student.service.impl
import com.dotori.v2.domain.member.domain.entity.Member
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.FindAllStudentResDto
import com.dotori.v2.domain.student.service.ModifyStudentInfoService
Expand All @@ -22,29 +23,20 @@ class ModifyStudentInfoServiceImpl(
private val CACHE_KEY = "memberList"

override fun execute(modifyStudentInfoRequest: ModifyStudentInfoRequest) {
val member = memberRepository.findByIdOrNull(modifyStudentInfoRequest.memberId) ?: throw MemberNotFoundException()
updateMemberInfo(member, modifyStudentInfoRequest)
}
val member = memberRepository.findByIdOrNull(modifyStudentInfoRequest.memberId)
?: throw MemberNotFoundException()

private fun updateMemberInfo(member: Member, modifyStudentInfoRequest: ModifyStudentInfoRequest) {
val newMember = Member(
id = member.id,
memberName = modifyStudentInfoRequest.memberName,
stuNum = modifyStudentInfoRequest.stuNum,
email = member.email,
password = member.password,
gender = modifyStudentInfoRequest.gender,
roles = Collections.singletonList(modifyStudentInfoRequest.role),
ruleViolation = member.ruleViolation,
profileImage = member.profileImage
)
val updated = memberRepository.save(member.updateMemberInfo(modifyStudentInfoRequest))
updateCache(updated)
}

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

if (cachedData != null) {
redisCacheService.putToCache(CACHE_KEY, cachedData)
val content = cachedData.toMutableList()
content.add(FindAllStudentResDto.of(member))
redisCacheService.putToCache(CACHE_KEY, content)
}

memberRepository.save(newMember)
}
}

0 comments on commit 39d80ac

Please sign in to comment.