|
@@ -10,20 +10,16 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.cache.annotation.CacheEvict;
|
|
import org.springframework.cache.annotation.CacheEvict;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
-import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import java.time.Instant;
|
|
import java.time.Instant;
|
|
|
-import java.util.List;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
@Service
|
|
|
public class NoteService {
|
|
public class NoteService {
|
|
|
|
|
|
|
|
private final NoteRepository repository;
|
|
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) {
|
|
public NoteService(NoteRepository repository) {
|
|
|
this.repository = repository;
|
|
this.repository = repository;
|
|
@@ -35,16 +31,14 @@ public class NoteService {
|
|
|
|
|
|
|
|
@Cacheable(value = "contentCache", key = "#id")
|
|
@Cacheable(value = "contentCache", key = "#id")
|
|
|
public Note findById(String id) {
|
|
public Note findById(String id) {
|
|
|
- log.debug("Cache miss - fetching and decompressing note [{}]", id);
|
|
|
|
|
return repository.findById(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")
|
|
@Cacheable(value = "metadataCache", key = "#id")
|
|
|
public NoteMetadata findMetadataById(String id) {
|
|
public NoteMetadata findMetadataById(String id) {
|
|
|
- log.debug("Cache miss - fetching metadata for note [{}]", id);
|
|
|
|
|
Note noteProjection = repository.findMetadataProjectionById(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);
|
|
return NoteMetadata.from(noteProjection);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -64,7 +58,7 @@ public class NoteService {
|
|
|
@CacheEvict(value = {"contentCache", "metadataCache"}, key = "#id")
|
|
@CacheEvict(value = {"contentCache", "metadataCache"}, key = "#id")
|
|
|
public Note update(String id, String content) {
|
|
public Note update(String id, String content) {
|
|
|
Note note = repository.findById(id)
|
|
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);
|
|
log.info("Updating note [{}]", id);
|
|
|
|
|
|
|
@@ -72,17 +66,4 @@ public class NoteService {
|
|
|
return repository.save(new Note(id, content, note.createdAt(), now));
|
|
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);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
}
|
|
}
|