瀏覽代碼

extract cleanup scheduler

Daniel Bohry 1 月之前
父節點
當前提交
a151bab95d

+ 37 - 0
src/main/java/com/lhamacorp/knotes/service/CleanupScheduler.java

@@ -0,0 +1,37 @@
+package com.lhamacorp.knotes.service;
+
+import com.lhamacorp.knotes.domain.Note;
+import com.lhamacorp.knotes.repository.NoteRepository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+@Component
+public class CleanupScheduler {
+
+    private final NoteRepository repository;
+
+    private static final String ONCE_PER_DAY_AT_2AM = "0 0 2 * * *";
+    private static final Logger log = LoggerFactory.getLogger(CleanupScheduler.class);
+
+    public CleanupScheduler(NoteRepository repository) {
+        this.repository = repository;
+    }
+
+    @Scheduled(cron = ONCE_PER_DAY_AT_2AM)
+    public void cleanup() {
+        List<Note> emptyNotes = repository.findEmptyNotes();
+        List<String> ids = emptyNotes.stream()
+            .map(Note::id)
+            .toList();
+
+        if (!ids.isEmpty()) {
+            log.info("Cleaning empty notes [{}]", ids);
+            repository.deleteAllById(ids);
+        }
+    }
+
+}

+ 4 - 23
src/main/java/com/lhamacorp/knotes/service/NoteService.java

@@ -10,20 +10,16 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
-import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
 import java.time.Instant;
-import java.util.List;
 
 @Service
 public class NoteService {
 
     private final NoteRepository repository;
 
-    private static final String ONCE_PER_DAY_AT_2AM = "0 0 2 * * *";
-
-    Logger log = LoggerFactory.getLogger(NoteService.class);
+    private static final Logger log = LoggerFactory.getLogger(NoteService.class);
 
     public NoteService(NoteRepository repository) {
         this.repository = repository;
@@ -35,16 +31,14 @@ public class NoteService {
 
     @Cacheable(value = "contentCache", key = "#id")
     public Note findById(String id) {
-        log.debug("Cache miss - fetching and decompressing note [{}]", id);
         return repository.findById(id)
-                .orElseThrow(() -> new NotFoundException("Note with id " + id + " not found!"));
+            .orElseThrow(() -> new NotFoundException("Note with id " + id + " not found!"));
     }
 
     @Cacheable(value = "metadataCache", key = "#id")
     public NoteMetadata findMetadataById(String id) {
-        log.debug("Cache miss - fetching metadata for note [{}]", id);
         Note noteProjection = repository.findMetadataProjectionById(id)
-                .orElseThrow(() -> new NotFoundException("Note with id " + id + " not found!"));
+            .orElseThrow(() -> new NotFoundException("Note with id " + id + " not found!"));
         return NoteMetadata.from(noteProjection);
     }
 
@@ -64,7 +58,7 @@ public class NoteService {
     @CacheEvict(value = {"contentCache", "metadataCache"}, key = "#id")
     public Note update(String id, String content) {
         Note note = repository.findById(id)
-                .orElseThrow(() -> new NotFoundException("Note with id " + id + " not found!"));
+            .orElseThrow(() -> new NotFoundException("Note with id " + id + " not found!"));
 
         log.info("Updating note [{}]", id);
 
@@ -72,17 +66,4 @@ public class NoteService {
         return repository.save(new Note(id, content, note.createdAt(), now));
     }
 
-    @Scheduled(cron = ONCE_PER_DAY_AT_2AM)
-    public void cleanup() {
-        List<Note> emptyNotes = repository.findEmptyNotes();
-        List<String> ids = emptyNotes.stream()
-                .map(Note::id)
-                .toList();
-
-        if (!ids.isEmpty()) {
-            log.info("Cleaning empty notes [{}]", ids);
-            repository.deleteAllById(ids);
-        }
-    }
-
 }