-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#538 [feat] 모임당 인기글/ 인기 작가 로직 수정
- Loading branch information
Showing
9 changed files
with
132 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
./module-external/src/main/java/com/mile/aws/utils/S3Service merge=ours |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
module-domain/src/main/java/com/mile/moim/repository/MoimPopularInfoRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
module-domain/src/main/java/com/mile/moim/service/lock/AtomicValidateMoimPopulerInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.mile.moim.service.lock; | ||
|
||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface AtomicValidateMoimPopulerInfo { | ||
} |
49 changes: 49 additions & 0 deletions
49
module-domain/src/main/java/com/mile/moim/service/lock/MoimPopularInfoAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.mile.moim.service.lock; | ||
|
||
import com.mile.exception.message.ErrorMessage; | ||
import com.mile.exception.model.MileException; | ||
import com.mile.moim.domain.Moim; | ||
import lombok.RequiredArgsConstructor; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
import org.redisson.api.RLock; | ||
import org.redisson.api.RedissonClient; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@Aspect | ||
@RequiredArgsConstructor | ||
@Component | ||
public class MoimPopularInfoAspect { | ||
|
||
private final RedissonClient redissonClient; | ||
private final static String MOIM_POPULAR_LOCK = "MOIM_POPULAR_LOCK : "; | ||
private final AopForTransaction aopForTransaction; | ||
|
||
@Pointcut("@annotation(com.mile.moim.service.lock.AtomicValidateMoimPopulerInfo)") | ||
public void setMoimPoplarInfoCut() { | ||
} | ||
|
||
@Around("setMoimPoplarInfoCut()") | ||
public Object getLockForPopularInfoTransaction(final ProceedingJoinPoint joinPoint) throws Throwable { | ||
final Moim moim = (Moim) joinPoint.getArgs()[0]; | ||
|
||
final RLock lock = redissonClient.getLock(MOIM_POPULAR_LOCK + moim.getId().toString()); | ||
|
||
try { | ||
checkAvailability(lock.tryLock(3, 4, TimeUnit.SECONDS)); | ||
return aopForTransaction.proceed(joinPoint); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
public void checkAvailability(final Boolean available) { | ||
if (!available) throw new MileException(ErrorMessage.TIME_OUT_EXCEPTION); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
module-domain/src/main/java/com/mile/moim/service/popular/MoimPopularInfoRegister.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.mile.moim.service.popular; | ||
|
||
import com.mile.curious.repository.dto.PostAndCuriousCountInLastWeek; | ||
import com.mile.curious.service.CuriousRetriever; | ||
import com.mile.moim.domain.Moim; | ||
import com.mile.moim.domain.popular.MoimCuriousPost; | ||
import com.mile.moim.domain.popular.MoimCuriousWriter; | ||
import com.mile.moim.domain.popular.MoimPopularInfo; | ||
import com.mile.moim.repository.MoimPopularInfoRepository; | ||
import com.mile.moim.service.lock.AtomicValidateMoimPopulerInfo; | ||
import com.mile.writername.domain.WriterName; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.cache.annotation.CachePut; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class MoimPopularInfoRegister { | ||
private final MoimPopularInfoRepository moimPopularInfoRepository; | ||
|
||
private final CuriousRetriever curiousRetriever; | ||
|
||
|
||
private List<MoimCuriousPost> getMoimCuriousPost(final List<PostAndCuriousCountInLastWeek> mostCuriousPostsInLastWeek) { | ||
return mostCuriousPostsInLastWeek.stream().map(p -> | ||
MoimCuriousPost.of(p.getPost())).limit(2).toList(); | ||
} | ||
|
||
private List<MoimCuriousWriter> getMoimCuriousWriter(final List<PostAndCuriousCountInLastWeek> mostCuriousPostsInLastWeek) { | ||
Map<WriterName, Long> writerNameCount = mostCuriousPostsInLastWeek.stream() | ||
.collect(Collectors.groupingBy(p -> p.getPost().getWriterName(), Collectors.summingLong(PostAndCuriousCountInLastWeek::getCount))); | ||
|
||
List<WriterName> topTwoWriters = writerNameCount.entrySet().stream() | ||
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) | ||
.limit(2) | ||
.map(Map.Entry::getKey).toList(); | ||
|
||
return topTwoWriters.stream().map(MoimCuriousWriter::of).toList(); | ||
} | ||
|
||
@CachePut(value = "moimPopularInfo", key = "#moim.id") | ||
@AtomicValidateMoimPopulerInfo | ||
public MoimPopularInfo setMostPopularInfoOfMoim(final Moim moim) { | ||
List<PostAndCuriousCountInLastWeek> mostCuriousPostsInLastWeek = curiousRetriever.findMostCuriousPostsInLastWeek(moim); | ||
|
||
List<MoimCuriousPost> moimCuriousPosts = getMoimCuriousPost(mostCuriousPostsInLastWeek); | ||
|
||
List<MoimCuriousWriter> moimCuriousWriters = getMoimCuriousWriter(mostCuriousPostsInLastWeek); | ||
|
||
MoimPopularInfo moimPopularInfo = MoimPopularInfo.of(moim.getId(), moimCuriousPosts, moimCuriousWriters); | ||
|
||
return moimPopularInfoRepository.saveAndFlush(moimPopularInfo); | ||
} | ||
|
||
} |
43 changes: 2 additions & 41 deletions
43
module-domain/src/main/java/com/mile/moim/service/popular/MoimPopularInfoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters