Skip to content

Commit

Permalink
Merge pull request #438 from Team-Ampersand/437-feat/member-cache
Browse files Browse the repository at this point in the history
회원가입/탈퇴, 유저 프로필 설정, 자습 관련 코드에 캐시 관련 기능 추가
  • Loading branch information
uuuuuuuk authored Sep 23, 2024
2 parents 009a0f7 + 8144192 commit f7243d1
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.dotori.v2.domain.email.exception.EmailAuthNotFoundException
import com.dotori.v2.domain.email.exception.EmailNotBeenException
import com.dotori.v2.domain.member.domain.repository.MemberRepository
import com.dotori.v2.domain.member.exception.MemberAlreadyException
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentListResDto
import com.dotori.v2.global.config.redis.service.RedisCacheService
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -17,7 +19,11 @@ class SignUpServiceImpl(
private val emailCertificateRepository: EmailCertificateRepository,
private val memberRepository: MemberRepository,
private val passwordEncoder: PasswordEncoder,
private val redisCacheService: RedisCacheService
): SignUpService {

private val CACHE_KEY = "memberList"

override fun execute(signUpReqDto: SignUpReqDto) {
val emailCertificate = emailCertificateRepository.findByEmail(signUpReqDto.email)
?: throw EmailAuthNotFoundException()
Expand All @@ -34,5 +40,15 @@ class SignUpServiceImpl(
val member = signUpReqDto.toEntity(encodedPassword)

memberRepository.save(member)
initCache()
}

private fun initCache() {
val cachedData =
redisCacheService.getFromCache(CACHE_KEY) as? FindAllStudentListResDto

if (cachedData != null) {
redisCacheService.deleteFromCache(CACHE_KEY)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.dotori.v2.domain.member.service.impl

import com.dotori.v2.domain.member.service.DeleteProfileImageService
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentListResDto
import com.dotori.v2.global.config.redis.service.RedisCacheService
import com.dotori.v2.global.thirdparty.aws.s3.S3Service
import com.dotori.v2.global.util.UserUtil
import org.springframework.stereotype.Service
Expand All @@ -10,13 +12,25 @@ import org.springframework.transaction.annotation.Transactional
@Transactional(rollbackFor = [Exception::class])
class DeleteProfileImageServiceImpl(
private val userUtil: UserUtil,
private val s3Service: S3Service
private val s3Service: S3Service,
private val redisCacheService: RedisCacheService
) : DeleteProfileImageService {

private val CACHE_KEY = "memberList"

override fun execute() {
val member = userUtil.fetchCurrentUser()
s3Service.deleteFile(member.profileImage!!)
member.updateProfileImage(null)
initCache()
}

private fun initCache() {
val cachedData =
redisCacheService.getFromCache(CACHE_KEY) as? FindAllStudentListResDto

if (cachedData != null) {
redisCacheService.deleteFromCache(CACHE_KEY)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.dotori.v2.domain.member.service.impl
import com.dotori.v2.domain.member.domain.entity.Member
import com.dotori.v2.global.util.ProfileImageService
import com.dotori.v2.domain.member.service.UpdateProfileImageService
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentListResDto
import com.dotori.v2.global.config.redis.service.RedisCacheService
import com.dotori.v2.global.util.UserUtil
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -12,10 +14,24 @@ import org.springframework.web.multipart.MultipartFile
@Transactional(rollbackFor = [Exception::class])
class UpdateProfileImageServiceImpl(
private val userUtil: UserUtil,
private val profileImageService: ProfileImageService
private val profileImageService: ProfileImageService,
private val redisCacheService: RedisCacheService
): UpdateProfileImageService {

private val CACHE_KEY = "memberList"

override fun execute(multipartFiles: MultipartFile?) {
val member: Member = userUtil.fetchCurrentUser()
profileImageService.imageUpload(member = member, multipartFiles = multipartFiles)
initCache()
}

private fun initCache() {
val cachedData =
redisCacheService.getFromCache(CACHE_KEY) as? FindAllStudentListResDto

if (cachedData != null) {
redisCacheService.deleteFromCache(CACHE_KEY)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.dotori.v2.domain.member.service.impl
import com.dotori.v2.domain.member.domain.entity.Member
import com.dotori.v2.global.util.ProfileImageService
import com.dotori.v2.domain.member.service.UploadProfileImageService
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentListResDto
import com.dotori.v2.global.config.redis.service.RedisCacheService
import com.dotori.v2.global.util.UserUtil
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -12,10 +14,24 @@ import org.springframework.web.multipart.MultipartFile
@Transactional(rollbackFor = [Exception::class])
class UploadProfileImageServiceImpl(
private val userUtil: UserUtil,
private val profileImageService: ProfileImageService
private val profileImageService: ProfileImageService,
private val redisCacheService: RedisCacheService
): UploadProfileImageService {

private val CACHE_KEY = "memberList"

override fun execute(multipartFiles: MultipartFile?) {
val member: Member = userUtil.fetchCurrentUser()
profileImageService.imageUpload(member = member, multipartFiles = multipartFiles)
initCache()
}

private fun initCache() {
val cachedData =
redisCacheService.getFromCache(CACHE_KEY) as? FindAllStudentListResDto

if (cachedData != null) {
redisCacheService.deleteFromCache(CACHE_KEY)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.dotori.v2.domain.member.service.impl

import com.dotori.v2.domain.member.domain.repository.MemberRepository
import com.dotori.v2.domain.member.service.WithdrawalService
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentListResDto
import com.dotori.v2.global.config.redis.service.RedisCacheService
import com.dotori.v2.global.thirdparty.aws.s3.S3Service
import com.dotori.v2.global.util.UserUtil
import org.springframework.stereotype.Service
Expand All @@ -13,10 +15,24 @@ class WithdrawalServiceImpl(
private val memberRepository: MemberRepository,
private val s3Service: S3Service,
private val userUtil: UserUtil,
private val redisCacheService: RedisCacheService
) : WithdrawalService {

private val CACHE_KEY = "memberList"

override fun execute() {
val currentUser = userUtil.fetchCurrentUser()
currentUser.profileImage?.let { s3Service.deleteFile(it) }
memberRepository.delete(currentUser)
initCache()
}

private fun initCache() {
val cachedData =
redisCacheService.getFromCache(CACHE_KEY) as? FindAllStudentListResDto

if (cachedData != null) {
redisCacheService.deleteFromCache(CACHE_KEY)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.dotori.v2.domain.selfstudy.util.FindSelfStudyCountUtil
import com.dotori.v2.domain.selfstudy.util.SaveSelfStudyUtil
import com.dotori.v2.domain.selfstudy.util.SelfStudyCheckUtil
import com.dotori.v2.domain.selfstudy.util.ValidDayOfWeekAndHourUtil
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentListResDto
import com.dotori.v2.global.config.redis.service.RedisCacheService
import com.dotori.v2.global.util.UserUtil
import com.dotori.v2.indicator.IndicatorTarget
import org.springframework.stereotype.Service
Expand All @@ -20,9 +22,12 @@ class ApplySelfStudyServiceImpl(
private val findSelfStudyCountUtil: FindSelfStudyCountUtil,
private val selfStudyCheckUtil: SelfStudyCheckUtil,
private val saveSelfStudyUtil: SaveSelfStudyUtil,
private val validDayOfWeekAndHourUtil: ValidDayOfWeekAndHourUtil
private val validDayOfWeekAndHourUtil: ValidDayOfWeekAndHourUtil,
private val redisCacheService: RedisCacheService
) : ApplySelfStudyService{

private val CACHE_KEY = "memberList"

@IndicatorTarget
override fun execute() {
validDayOfWeekAndHourUtil.validateApply()
Expand All @@ -38,5 +43,15 @@ class ApplySelfStudyServiceImpl(
member.updateSelfStudyStatus(SelfStudyStatus.APPLIED)
selfStudyCount.addCount()
saveSelfStudyUtil.save(member)
initCache()
}

private fun initCache() {
val cachedData =
redisCacheService.getFromCache(CACHE_KEY) as? FindAllStudentListResDto

if (cachedData != null) {
redisCacheService.deleteFromCache(CACHE_KEY)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.dotori.v2.domain.member.domain.repository.MemberRepository
import com.dotori.v2.domain.member.enums.SelfStudyStatus
import com.dotori.v2.domain.member.exception.MemberNotFoundException
import com.dotori.v2.domain.selfstudy.service.BanSelfStudyService
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentListResDto
import com.dotori.v2.global.config.redis.service.RedisCacheService
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -13,11 +15,16 @@ import java.time.LocalDateTime
@Service
@Transactional(rollbackFor = [Exception::class])
class BanSelfStudyServiceImpl(
private val memberRepository: MemberRepository
private val memberRepository: MemberRepository,
private val redisCacheService: RedisCacheService
) : BanSelfStudyService {

private val CACHE_KEY = "memberList"

override fun execute(id: Long) {
val member = memberRepository.findByIdOrNull(id) ?: throw MemberNotFoundException()
updateSelfStudyAndExpiredDate(member, SelfStudyStatus.IMPOSSIBLE, LocalDateTime.now().plusDays(7))
initCache()
}

private fun updateSelfStudyAndExpiredDate(
Expand All @@ -28,4 +35,13 @@ class BanSelfStudyServiceImpl(
findMember.updateSelfStudyStatus(selfStudyStatus)
findMember.updateSelfStudyExpiredDate(localDateTime)
}

private fun initCache() {
val cachedData =
redisCacheService.getFromCache(CACHE_KEY) as? FindAllStudentListResDto

if (cachedData != null) {
redisCacheService.deleteFromCache(CACHE_KEY)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.dotori.v2.domain.member.domain.repository.MemberRepository
import com.dotori.v2.domain.member.enums.SelfStudyStatus
import com.dotori.v2.domain.member.exception.MemberNotFoundException
import com.dotori.v2.domain.selfstudy.service.CancelBanSelfStudyService
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentListResDto
import com.dotori.v2.global.config.redis.service.RedisCacheService
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -13,12 +15,16 @@ import java.time.LocalDateTime
@Service
@Transactional(rollbackFor = [Exception::class])
class CancelBanSelfStudyServiceImpl(
private val memberRepository: MemberRepository
private val memberRepository: MemberRepository,
private val redisCacheService: RedisCacheService
) : CancelBanSelfStudyService{

private val CACHE_KEY = "memberList"

override fun execute(id: Long) {
val member = memberRepository.findByIdOrNull(id) ?: throw MemberNotFoundException()
updateSelfStudyAndExpiredDate(member, SelfStudyStatus.CAN, null)

initCache()
}

private fun updateSelfStudyAndExpiredDate(
Expand All @@ -29,4 +35,13 @@ class CancelBanSelfStudyServiceImpl(
findMember.updateSelfStudyStatus(selfStudyStatus)
findMember.updateSelfStudyExpiredDate(localDateTime)
}

private fun initCache() {
val cachedData =
redisCacheService.getFromCache(CACHE_KEY) as? FindAllStudentListResDto

if (cachedData != null) {
redisCacheService.deleteFromCache(CACHE_KEY)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.dotori.v2.domain.selfstudy.service.CancelSelfStudyService
import com.dotori.v2.domain.selfstudy.util.FindSelfStudyCountUtil
import com.dotori.v2.domain.selfstudy.util.SelfStudyCheckUtil
import com.dotori.v2.domain.selfstudy.util.ValidDayOfWeekAndHourUtil
import com.dotori.v2.domain.student.presentation.data.res.FindAllStudentListResDto
import com.dotori.v2.global.config.redis.service.RedisCacheService
import com.dotori.v2.global.util.UserUtil
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -17,8 +19,12 @@ class CancelSelfStudyServiceImpl(
private val validDayOfWeekAndHourUtil: ValidDayOfWeekAndHourUtil,
private val findSelfStudyCountUtil: FindSelfStudyCountUtil,
private val selfStudyRepository: SelfStudyRepository,
private val selfStudyCheckUtil: SelfStudyCheckUtil
private val selfStudyCheckUtil: SelfStudyCheckUtil,
private val redisCacheService: RedisCacheService
) : CancelSelfStudyService {

private val CACHE_KEY = "memberList"

override fun execute() {
validDayOfWeekAndHourUtil.validateCancel()

Expand All @@ -32,5 +38,15 @@ class CancelSelfStudyServiceImpl(

selfStudyRepository.deleteByMember(member)
findSelfStudyCount.removeCount()
initCache()
}

private fun initCache() {
val cachedData =
redisCacheService.getFromCache(CACHE_KEY) as? FindAllStudentListResDto

if (cachedData != null) {
redisCacheService.deleteFromCache(CACHE_KEY)
}
}
}
Loading

0 comments on commit f7243d1

Please sign in to comment.