Skip to content

Commit

Permalink
#569 [feat] 글모임 인기 정보 관련 삭제 모듈 및 캐시 삭제 API 구현
Browse files Browse the repository at this point in the history
#569 [feat] 글모임 인기 정보 관련 삭제 모듈 및 캐시 삭제 API 구현
  • Loading branch information
sohyundoh authored Nov 5, 2024
2 parents e6b328e + 74270a9 commit 921717e
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mile.controller.external;

import com.mile.common.CacheService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class InternalController {

private final CacheService cacheService;

@PostMapping("/api/v1/moim/info/cache")
public void deleteMoimInfoCache() {
cacheService.deleteMoimCache();
}
}
19 changes: 19 additions & 0 deletions module-domain/src/main/java/com/mile/common/CacheService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mile.common;

import lombok.RequiredArgsConstructor;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CacheService {

private final String MOIM_CACHE_NAME = "moimPopularInfo";
private final CacheManager cacheManager;

public void deleteMoimCache() {
if (cacheManager.getCache(MOIM_CACHE_NAME) != null) {
cacheManager.getCache(MOIM_CACHE_NAME).clear();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mile.moim.domain.popular;

import com.mile.common.config.BaseTimeEntity;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
Expand All @@ -16,7 +17,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MoimPopularInfo {
public class MoimPopularInfo extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public interface MoimPopularInfoRepository extends JpaRepository<MoimPopularInfo
@Query("select m from MoimPopularInfo m join fetch m.posts join fetch m.writers where m.moimId = :moimId")
Optional<MoimPopularInfo> findByMoimId(final long moimId);


@Scheduled(cron = "59 59 23 * * SUN")
void deleteAll();
@Query("select count(m) from MoimPopularInfo m")
Long countAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
import com.mile.moim.domain.Moim;
import com.mile.moim.domain.popular.MoimPopularInfo;
import com.mile.moim.repository.MoimPopularInfoRepository;
import com.mile.moim.service.lock.AtomicValidateMoimPopulerInfo;
import com.mile.slack.module.SendMessageModule;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class MoimPopularInfoService {
private final MoimPopularInfoRepository moimPopularInfoRepository;
private final MoimPopularInfoRegister moimPopularInfoRegister;
private final SendMessageModule sendMessageModule;


@Cacheable(value = "moimPopularInfo", key = "#moim.id")
Expand All @@ -23,4 +25,10 @@ public MoimPopularInfo getMoimPopularInfo(final Moim moim) {
);
}

@Scheduled(cron = "59 59 23 * * SUN")
public void deleteAllForScheduled() {
sendMessageModule.sendMessage("글모임 별 인기 글/ 인기 작가 삭제 완료 : 총 " + moimPopularInfoRepository.countAll() + "개의 모임");
moimPopularInfoRepository.deleteAllInBatch();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public record PostPutRequest(
String topicId,

@NotBlank(message = "제목을 입력해주세요.")
@Size(max = 29, message = "제목 최대 글자를 초과했습니다.")
@Size(max = 34, message = "제목 최대 글자를 초과했습니다.")
@Schema(description = "글 제목", example = "편안한 글쓰기")
String title,

Expand Down
1 change: 1 addition & 0 deletions module-external/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ dependencies {
implementation project(":module-common")
implementation("software.amazon.awssdk:bom:2.21.0")
implementation("software.amazon.awssdk:s3:2.21.0")
implementation("org.springframework.boot:spring-boot-starter-webflux")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.mile.slack.module;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import java.time.LocalDateTime;
import java.util.ArrayList;


@Component
@Slf4j
public class SendMessageModule implements SendWebhookMessage {
private final StringBuilder sb = new StringBuilder();

@Value("${webhook.url-for-event}")
private String webHookUri;

@Override
public void sendMessage(@NonNull final String message) {
WebClient webClient = WebClient.builder()
.baseUrl(webHookUri).build();


webClient.post()
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(generateMessage(message))
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(String.class)
.doOnError(e -> log.error("WEB HOOK 전송 중 에러 발생 -> {}", e.getMessage()))
.subscribe();
}

private SlackMessage generateMessage(final String message) {
sb.append("*[인기글 삭제 작업 완료]*").append("\n").append(message).append("\n");
sb.append("*[진행일자]*").append("\n").append(LocalDateTime.now()).append("\n");

return new SlackMessage(sb.toString());
}

@Getter
@AllArgsConstructor
private class SlackMessage {
private String text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mile.slack.module;

import org.springframework.lang.NonNull;

public interface SendWebhookMessage {
void sendMessage(@NonNull final String message);

}

0 comments on commit 921717e

Please sign in to comment.