浏览代码

优化缓存

zhouhao 5 年之前
父节点
当前提交
833d9437de

+ 9 - 7
hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/service/EnableCacheReactiveCrudService.java

@@ -14,13 +14,15 @@ public interface EnableCacheReactiveCrudService<E, K> extends ReactiveCrudServic
 
     ReactiveCache<E> getCache();
 
+    default Mono<E> findById(K id) {
+        return this.getCache()
+                .mono("id:" + id)
+                .onCacheMissResume(ReactiveCrudService.super.findById(Mono.just(id)));
+    }
+
     @Override
     default Mono<E> findById(Mono<K> publisher) {
-        return publisher.flatMap(id -> {
-            return this.getCache()
-                    .mono("id:" + id)
-                    .onCacheMissResume(ReactiveCrudService.super.findById(Mono.just(id)));
-        });
+        return publisher.flatMap(this::findById);
     }
 
     @Override
@@ -57,12 +59,12 @@ public interface EnableCacheReactiveCrudService<E, K> extends ReactiveCrudServic
     @Override
     default ReactiveUpdate<E> createUpdate() {
         return ReactiveCrudService.super.createUpdate()
-                .onExecute((update,s) -> s.doFinally((__) -> getCache().clear().subscribe()));
+                .onExecute((update, s) -> s.doFinally((__) -> getCache().clear().subscribe()));
     }
 
     @Override
     default ReactiveDelete createDelete() {
         return ReactiveCrudService.super.createDelete()
-                .onExecute((update,s) -> s.doFinally((__) -> getCache().clear().subscribe()));
+                .onExecute((update, s) -> s.doFinally((__) -> getCache().clear().subscribe()));
     }
 }

+ 2 - 0
hsweb-commons/hsweb-commons-crud/src/test/java/org/hswebframework/web/crud/service/GenericReactiveCacheSupportCrudServiceTest.java

@@ -7,6 +7,7 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.annotation.DirtiesContext;
 import org.springframework.test.context.junit4.SpringRunner;
 import reactor.core.publisher.Mono;
 import reactor.test.StepVerifier;
@@ -15,6 +16,7 @@ import static org.junit.Assert.*;
 
 @SpringBootTest(classes = TestApplication.class, args = "--hsweb.cache.type=guava")
 @RunWith(SpringRunner.class)
+@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
 public class GenericReactiveCacheSupportCrudServiceTest {
 
     @Autowired