瀏覽代碼

update metadata and content cache

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

+ 18 - 17
src/main/java/com/lhamacorp/knotes/config/CacheConfig.java

@@ -3,11 +3,13 @@ package com.lhamacorp.knotes.config;
 import com.github.benmanes.caffeine.cache.Caffeine;
 import org.springframework.cache.CacheManager;
 import org.springframework.cache.annotation.EnableCaching;
-import org.springframework.cache.caffeine.CaffeineCacheManager;
+import org.springframework.cache.caffeine.CaffeineCache;
+import org.springframework.cache.support.SimpleCacheManager;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 import java.time.Duration;
+import java.util.Arrays;
 
 @Configuration
 @EnableCaching
@@ -15,32 +17,31 @@ public class CacheConfig {
 
     @Bean
     public CacheManager cacheManager() {
-        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
+        SimpleCacheManager cacheManager = new SimpleCacheManager();
 
-        cacheManager.setCaffeine(Caffeine.newBuilder()
-                .initialCapacity(100)
-                .maximumSize(1000)
-                .expireAfterWrite(Duration.ofSeconds(10))
-                .recordStats());
+        cacheManager.setCaches(Arrays.asList(
+            new CaffeineCache("contentCache", contentCacheCaffeine().build()),
+            new CaffeineCache("metadataCache", metadataCacheCaffeine().build())
+        ));
 
         return cacheManager;
     }
 
-    @Bean("metadataCache")
-    public Caffeine<Object, Object> metadataCache() {
+    @Bean
+    public Caffeine<Object, Object> contentCacheCaffeine() {
         return Caffeine.newBuilder()
-                .initialCapacity(50)
-                .maximumSize(500)
-                .expireAfterWrite(Duration.ofSeconds(10))
+                .initialCapacity(20)
+                .maximumSize(100)
+                .expireAfterWrite(Duration.ofSeconds(30))
                 .recordStats();
     }
 
-    @Bean("contentCache")
-    public Caffeine<Object, Object> contentCache() {
+    @Bean
+    public Caffeine<Object, Object> metadataCacheCaffeine() {
         return Caffeine.newBuilder()
-                .initialCapacity(20)
-                .maximumSize(100)
-                .expireAfterWrite(Duration.ofSeconds(30))
+                .initialCapacity(50)
+                .maximumSize(500)
+                .expireAfterWrite(Duration.ofSeconds(10))
                 .recordStats();
     }
 }

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

@@ -33,14 +33,14 @@ public class NoteService {
         return repository.existsById(id);
     }
 
-    @Cacheable(value = "note", key = "#id")
+    @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!"));
     }
 
-    @Cacheable(value = "metadata", key = "#id")
+    @Cacheable(value = "metadataCache", key = "#id")
     public NoteMetadata findMetadataById(String id) {
         log.debug("Cache miss - fetching metadata for note [{}]", id);
         Note noteProjection = repository.findMetadataProjectionById(id)
@@ -48,7 +48,7 @@ public class NoteService {
         return NoteMetadata.from(noteProjection);
     }
 
-    @CacheEvict(value = {"note", "metadata"}, key = "#result.id")
+    @CacheEvict(value = {"contentCache", "metadataCache"}, key = "#result.id")
     public Note save(String content) {
         Ulid id = UlidCreator.getUlid();
 
@@ -61,7 +61,7 @@ public class NoteService {
         return savedNote;
     }
 
-    @CacheEvict(value = {"note", "metadata"}, key = "#id")
+    @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!"));