Skip to content

Commit

Permalink
[refac] content async 도입 (#147)
Browse files Browse the repository at this point in the history
* [refac] content api 로직 async 도입 #146

* [chore] spotless 적용 #146
  • Loading branch information
wjdtkdgns authored Aug 28, 2023
1 parent c5f031b commit 469f257
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 26 deletions.
35 changes: 35 additions & 0 deletions Api/src/main/java/allchive/server/api/common/aop/TestTimerAop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package allchive.server.api.common.aop;


import allchive.server.core.helper.SpringEnvironmentHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Aspect
@Component
@Slf4j
@RequiredArgsConstructor
public class TestTimerAop {
private final SpringEnvironmentHelper springEnvironmentHelper;

@Around("@annotation(org.springframework.transaction.annotation.Transactional)")
public Object handleEvent(ProceedingJoinPoint joinPoint) throws Throwable {
if (!springEnvironmentHelper.isProdAndDevProfile()) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();

Object result = joinPoint.proceed();

stopWatch.stop();
log.info(String.valueOf(stopWatch.getLastTaskTimeMillis()));
return result;
} else {
return joinPoint.proceed();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import allchive.server.api.content.model.dto.response.ContentTagResponse;
import allchive.server.api.content.model.mapper.ContentMapper;
import allchive.server.core.annotation.UseCase;
import allchive.server.domain.domains.archiving.service.ArchivingDomainService;
import allchive.server.domain.domains.archiving.service.ArchivingAsyncDomainService;
import allchive.server.domain.domains.archiving.validator.ArchivingValidator;
import allchive.server.domain.domains.content.adaptor.TagAdaptor;
import allchive.server.domain.domains.content.domain.Content;
import allchive.server.domain.domains.content.domain.ContentTagGroup;
import allchive.server.domain.domains.content.domain.Tag;
import allchive.server.domain.domains.content.service.ContentDomainService;
import allchive.server.domain.domains.content.service.ContentTagGroupDomainService;
import allchive.server.domain.domains.content.service.TagDomainService;
import allchive.server.domain.domains.content.service.TagAsyncDomainService;
import allchive.server.domain.domains.content.validator.TagValidator;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -29,20 +29,20 @@ public class CreateContentUseCase {
private final ContentDomainService contentDomainService;
private final TagValidator tagValidator;
private final TagAdaptor tagAdaptor;
private final TagDomainService tagDomainService;
private final ContentTagGroupDomainService contentTagGroupDomainService;
private final ArchivingDomainService archivingDomainService;
private final ArchivingAsyncDomainService archivingAsyncDomainService;
private final TagAsyncDomainService tagAsyncDomainService;

@Transactional
public ContentTagResponse execute(CreateContentRequest request) {
Long userId = SecurityUtil.getCurrentUserId();
validateExecution(userId, request);
Content content = contentMapper.toEntity(request);
updateTagUsedAt(request.getTagIds());
List<ContentTagGroup> contentTagGroupList =
createContentTagGroup(content, request.getTagIds());
contentDomainService.save(content);
archivingDomainService.updateContentCnt(
updateTagUsedAt(request.getTagIds());
archivingAsyncDomainService.updateContentCnt(
request.getArchivingId(), request.getContentType(), PLUS_ONE);
return contentMapper.toContentTagResponse(content, contentTagGroupList, true, userId);
}
Expand All @@ -61,6 +61,6 @@ private List<ContentTagGroup> createContentTagGroup(Content content, List<Long>
}

private void updateTagUsedAt(List<Long> tagIds) {
tagAdaptor.queryTagByTagIdIn(tagIds).forEach(tagDomainService::updateUsedAt);
tagAdaptor.queryTagByTagIdIn(tagIds).forEach(tagAsyncDomainService::updateUsedAt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import allchive.server.api.config.security.SecurityUtil;
import allchive.server.api.recycle.model.mapper.RecycleMapper;
import allchive.server.core.annotation.UseCase;
import allchive.server.domain.domains.archiving.service.ArchivingDomainService;
import allchive.server.domain.domains.archiving.service.ArchivingAsyncDomainService;
import allchive.server.domain.domains.content.adaptor.ContentAdaptor;
import allchive.server.domain.domains.content.domain.Content;
import allchive.server.domain.domains.content.service.ContentDomainService;
Expand All @@ -23,8 +23,8 @@ public class DeleteContentUseCase {
private final ContentDomainService contentDomainService;
private final RecycleMapper recycleMapper;
private final RecycleDomainService recycleDomainService;
private final ArchivingDomainService archivingDomainService;
private final ContentAdaptor contentAdaptor;
private final ArchivingAsyncDomainService archivingAsyncDomainService;

@Transactional
public void execute(Long contentId) {
Expand All @@ -48,7 +48,7 @@ private void createRecycle(Long userId, Long contentId) {

private void decreaseArchivingCount(Long contentId) {
Content content = contentAdaptor.findById(contentId);
archivingDomainService.updateContentCnt(
archivingAsyncDomainService.updateContentCnt(
content.getArchivingId(), content.getContentType(), MINUS_ONE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
import allchive.server.core.annotation.UseCase;
import allchive.server.core.event.Event;
import allchive.server.core.event.events.s3.S3ImageDeleteEvent;
import allchive.server.domain.domains.archiving.service.ArchivingDomainService;
import allchive.server.domain.domains.archiving.service.ArchivingAsyncDomainService;
import allchive.server.domain.domains.content.adaptor.ContentAdaptor;
import allchive.server.domain.domains.content.adaptor.ContentTagGroupAdaptor;
import allchive.server.domain.domains.content.adaptor.TagAdaptor;
import allchive.server.domain.domains.content.domain.Content;
import allchive.server.domain.domains.content.domain.ContentTagGroup;
import allchive.server.domain.domains.content.domain.Tag;
import allchive.server.domain.domains.content.domain.enums.ContentType;
import allchive.server.domain.domains.content.service.ContentDomainService;
import allchive.server.domain.domains.content.service.ContentTagGroupDomainService;
import allchive.server.domain.domains.content.service.TagDomainService;
import allchive.server.domain.domains.content.service.TagAsyncDomainService;
import allchive.server.domain.domains.content.validator.ContentValidator;
import allchive.server.domain.domains.content.validator.TagValidator;
import allchive.server.infrastructure.s3.service.S3DeleteObjectService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -37,32 +37,32 @@ public class UpdateContentUseCase {
private final ContentMapper contentMapper;
private final ContentDomainService contentDomainService;
private final ContentTagGroupDomainService contentTagGroupDomainService;
private final ArchivingDomainService archivingDomainService;
private final TagDomainService tagDomainService;
private final S3DeleteObjectService s3DeleteObjectService;
private final TagAsyncDomainService tagAsyncDomainService;
private final ContentTagGroupAdaptor contentTagGroupAdaptor;
private final ArchivingAsyncDomainService archivingAsyncDomainService;

@Transactional
public void execute(Long contentId, UpdateContentRequest request) {
validateExecution(contentId, request);
updateTagUsedAt(request.getTagIds());
regenerateContentTagGroup(contentId, request.getTagIds());
updateArchiving(contentId, request.getArchivingId(), request.getContentType());
eliminateOldImage(contentId, request.getImgUrl());
contentDomainService.update(
contentId,
request.getArchivingId(),
request.getLink(),
request.getMemo(),
UrlUtil.convertUrlToKey(request.getImgUrl()),
request.getTitle());
updateArchiving(contentId, request.getArchivingId(), request.getContentType());
updateTagUsedAt(request.getTagIds());
eliminateOldImage(contentId, request.getImgUrl());
}

private void updateArchiving(Long contentId, Long newArchivingId, ContentType contentType) {
Content content = contentAdaptor.findById(contentId);
if (!content.getArchivingId().equals(newArchivingId)) {
archivingDomainService.updateContentCnt(
archivingAsyncDomainService.updateContentCnt(
content.getArchivingId(), content.getContentType(), MINUS_ONE);
archivingDomainService.updateContentCnt(newArchivingId, contentType, PLUS_ONE);
archivingAsyncDomainService.updateContentCnt(newArchivingId, contentType, PLUS_ONE);
}
}

Expand All @@ -74,15 +74,29 @@ private void validateExecution(Long contentId, UpdateContentRequest request) {

private void regenerateContentTagGroup(Long contentId, List<Long> tagIds) {
Content content = contentAdaptor.findById(contentId);
contentTagGroupDomainService.deleteAllByContent(content);
List<Tag> tags = tagAdaptor.queryTagByTagIdIn(tagIds);
List<ContentTagGroup> contentTagGroups =
contentTagGroupAdaptor.queryContentTagGroupByContentIn(List.of(content));
List<ContentTagGroup> oldContentTagGroups =
contentTagGroups.stream()
.filter(
contentTagGroup ->
!tagIds.contains(contentTagGroup.getTag().getId()))
.toList();
contentTagGroupDomainService.deleteAll(oldContentTagGroups);

List<Long> oldTagIds =
contentTagGroups.stream()
.map(contentTagGroup -> contentTagGroup.getTag().getId())
.toList();
List<Long> newTagIds = tagIds.stream().filter(tagId -> !oldTagIds.contains(tagId)).toList();
List<Tag> tags = tagAdaptor.queryTagByTagIdIn(newTagIds);
List<ContentTagGroup> contentTagGroupList =
contentMapper.toContentTagGroupEntityList(content, tags);
contentTagGroupDomainService.saveAll(contentTagGroupList);
}

private void updateTagUsedAt(List<Long> tagIds) {
tagAdaptor.queryTagByTagIdIn(tagIds).forEach(tagDomainService::updateUsedAt);
tagAdaptor.queryTagByTagIdIn(tagIds).forEach(tagAsyncDomainService::updateUsedAt);
}

private void eliminateOldImage(Long contentId, String newUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return customAsyncExceptionHandler;
}

@Bean(name = "archivingContentCntTaskExecutor")
public Executor archivingContentCntTaskExecutor() {
return createTaskExecutor("ARCHIVING_CONTENT_CNT_TASK_EXECUTOR");
}

@Bean(name = "tagTaskExecutor")
public Executor tagTaskExecutor() {
return createTaskExecutor("TAG_TASK_EXECUTOR");
}

@Bean(name = "s3ImageTaskExecutor")
public Executor s3ImageTaskExecutor() {
return createTaskExecutor("S3_IMAGE_TASK_EXECUTOR");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package allchive.server.domain.domains.archiving.service;


import allchive.server.core.annotation.DomainService;
import allchive.server.domain.domains.content.domain.enums.ContentType;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;

@DomainService
@RequiredArgsConstructor
public class ArchivingAsyncDomainService {
private final ArchivingDomainService archivingDomainService;

@Async(value = "archivingContentCntTaskExecutor")
public void updateContentCnt(Long archivingId, ContentType contentType, int i) {
archivingDomainService.updateContentCnt(archivingId, contentType, i);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ public void updateContentCnt(Long archivingId, ContentType contentType, int i) {
} else {
archiving.updateLinkCnt(i);
}
archivingAdaptor.save(archiving);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ public void deleteAllByContent(Content content) {
public List<ContentTagGroup> queryContentTagGroupByTagInWithContent(List<Tag> tags) {
return contentTagGroupRepository.queryContentTagGroupByTagInWithContent(tags);
}

public void deleteAll(List<ContentTagGroup> contentTagGroups) {
contentTagGroupRepository.deleteAll(contentTagGroups);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public void deleteAllByTagIn(List<Tag> tagList) {
public void deleteAllByContent(Content content) {
contentTagGroupAdaptor.deleteAllByContent(content);
}

public void deleteAll(List<ContentTagGroup> contentTagGroups) {
contentTagGroupAdaptor.deleteAll(contentTagGroups);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package allchive.server.domain.domains.content.service;


import allchive.server.core.annotation.DomainService;
import allchive.server.domain.domains.content.domain.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;

@DomainService
@RequiredArgsConstructor
public class TagAsyncDomainService {
private final TagDomainService tagDomainService;

@Async(value = "tagTaskExecutor")
public void updateUsedAt(Tag tag) {
tagDomainService.updateUsedAt(tag);
}
}
4 changes: 2 additions & 2 deletions Domain/src/main/resources/application-domain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ logging:
com.zaxxer.hikari: TRACE
org.springframework.orm.jpa: DEBUG
org.springframework.transaction: DEBUG
# org.hibernate.SQL: debug
# org.hibernate.type: trace
org.hibernate.SQL: debug
org.hibernate.type: trace
---
# dev
spring:
Expand Down

0 comments on commit 469f257

Please sign in to comment.