Browse Source

fix: 修改缓存刷新后立即失效的bug

ChenSiTong 6 years ago
parent
commit
376afdc0ac

+ 47 - 47
pom.xml

@@ -33,14 +33,14 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-jpa</artifactId>
         </dependency>
-<!--        <dependency>-->
-<!--            <groupId>org.springframework.boot</groupId>-->
-<!--            <artifactId>spring-boot-starter-data-redis</artifactId>-->
-<!--        </dependency>-->
-<!--        <dependency>-->
-<!--            <groupId>org.springframework.boot</groupId>-->
-<!--            <artifactId>spring-boot-starter-mail</artifactId>-->
-<!--        </dependency>-->
+        <!--        <dependency>-->
+        <!--            <groupId>org.springframework.boot</groupId>-->
+        <!--            <artifactId>spring-boot-starter-data-redis</artifactId>-->
+        <!--        </dependency>-->
+        <!--        <dependency>-->
+        <!--            <groupId>org.springframework.boot</groupId>-->
+        <!--            <artifactId>spring-boot-starter-mail</artifactId>-->
+        <!--        </dependency>-->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
@@ -86,26 +86,26 @@
             <artifactId>pdfbox</artifactId>
             <version>2.0.18</version>
         </dependency>
-<!--        <dependency>-->
-<!--            <groupId>org.apache.poi</groupId>-->
-<!--            <artifactId>poi-ooxml</artifactId>-->
-<!--            <version>4.1.2</version>-->
-<!--        </dependency>-->
-<!--        <dependency>-->
-<!--            <groupId>org.apache.poi</groupId>-->
-<!--            <artifactId>poi-scratchpad</artifactId>-->
-<!--            <version>4.1.2</version>-->
-<!--        </dependency>-->
+        <!--        <dependency>-->
+        <!--            <groupId>org.apache.poi</groupId>-->
+        <!--            <artifactId>poi-ooxml</artifactId>-->
+        <!--            <version>4.1.2</version>-->
+        <!--        </dependency>-->
+        <!--        <dependency>-->
+        <!--            <groupId>org.apache.poi</groupId>-->
+        <!--            <artifactId>poi-scratchpad</artifactId>-->
+        <!--            <version>4.1.2</version>-->
+        <!--        </dependency>-->
         <dependency>
             <groupId>com.aliyun.oss</groupId>
             <artifactId>aliyun-sdk-oss</artifactId>
             <version>3.8.1</version>
         </dependency>
-<!--        <dependency>-->
-<!--            <groupId>com.aliyun</groupId>-->
-<!--            <artifactId>aliyun-java-sdk-core</artifactId>-->
-<!--            <version>4.0.3</version>-->
-<!--        </dependency>-->
+        <!--        <dependency>-->
+        <!--            <groupId>com.aliyun</groupId>-->
+        <!--            <artifactId>aliyun-java-sdk-core</artifactId>-->
+        <!--            <version>4.0.3</version>-->
+        <!--        </dependency>-->
         <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
@@ -122,14 +122,14 @@
             <artifactId>hibernate-types-52</artifactId>
             <version>2.9.5</version>
         </dependency>
-<!--        <dependency>-->
-<!--            <groupId>org.hibernate</groupId>-->
-<!--            <artifactId>hibernate-jcache</artifactId>-->
-<!--        </dependency>-->
-<!--        <dependency>-->
-<!--            <groupId>org.ehcache</groupId>-->
-<!--            <artifactId>ehcache</artifactId>-->
-<!--        </dependency>-->
+        <!--        <dependency>-->
+        <!--            <groupId>org.hibernate</groupId>-->
+        <!--            <artifactId>hibernate-jcache</artifactId>-->
+        <!--        </dependency>-->
+        <!--        <dependency>-->
+        <!--            <groupId>org.ehcache</groupId>-->
+        <!--            <artifactId>ehcache</artifactId>-->
+        <!--        </dependency>-->
         <!--一个工具类库,不用自己造轮子了!!!-->
         <dependency>
             <groupId>cn.hutool</groupId>
@@ -154,22 +154,22 @@
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
-<!--            <plugin>-->
-<!--                <groupId>com.github.apiggs</groupId>-->
-<!--                <artifactId>apiggs-maven-plugin</artifactId>-->
-<!--                <version>1.6</version>-->
-<!--                <executions>-->
-<!--                    <execution>-->
-<!--                        <phase>compile</phase>-->
-<!--                        <goals>-->
-<!--                            <goal>apiggs</goal>-->
-<!--                        </goals>-->
-<!--                    </execution>-->
-<!--                </executions>-->
-<!--                <configuration>-->
-<!--                    &lt;!&ndash; options in there &ndash;&gt;-->
-<!--                </configuration>-->
-<!--            </plugin>-->
+            <!--            <plugin>-->
+            <!--                <groupId>com.github.apiggs</groupId>-->
+            <!--                <artifactId>apiggs-maven-plugin</artifactId>-->
+            <!--                <version>1.6</version>-->
+            <!--                <executions>-->
+            <!--                    <execution>-->
+            <!--                        <phase>compile</phase>-->
+            <!--                        <goals>-->
+            <!--                            <goal>apiggs</goal>-->
+            <!--                        </goals>-->
+            <!--                    </execution>-->
+            <!--                </executions>-->
+            <!--                <configuration>-->
+            <!--                    &lt;!&ndash; options in there &ndash;&gt;-->
+            <!--                </configuration>-->
+            <!--            </plugin>-->
         </plugins>
     </build>
 

+ 2 - 2
src/main/java/nju/seec/helper/sys/bok/api/KnowledgeApi.java

@@ -4,7 +4,7 @@ import lombok.AllArgsConstructor;
 import lombok.Data;
 import nju.seec.helper.sys.bok.vo.KnowledgeNodeVO;
 import nju.seec.helper.util.RestRequestUtil;
-import nju.seec.helper.util.cache.CaffeineCacheUtils;
+import nju.seec.helper.util.cache.BaseCacheUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
@@ -22,7 +22,7 @@ public class KnowledgeApi {
     private String bokKnowledgeCacheName;
 
     private final RestRequestUtil restRequestUtil;
-    private final CaffeineCacheUtils cacheUtils;
+    private final BaseCacheUtils cacheUtils;
 
     private String url;
 

+ 2 - 2
src/main/java/nju/seec/helper/sys/bok/api/QuestionApi.java

@@ -12,7 +12,7 @@ import nju.seec.helper.sys.bok.vo.PageableQuestionListVO;
 import nju.seec.helper.sys.bok.vo.QuestionVO;
 import nju.seec.helper.util.RestRequestUtil;
 import nju.seec.helper.util.StringUtils;
-import nju.seec.helper.util.cache.CaffeineCacheUtils;
+import nju.seec.helper.util.cache.BaseCacheUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.data.domain.Page;
@@ -37,7 +37,7 @@ public class QuestionApi {
     private String bokQuestionCacheName;
 
     private final RestRequestUtil restRequestUtil;
-    private final CaffeineCacheUtils cacheUtils;
+    private final BaseCacheUtils cacheUtils;
     private String url;
     private String searchUrl;
 

+ 2 - 2
src/main/java/nju/seec/helper/sys/bok/api/QuestionQueryApi.java

@@ -8,7 +8,7 @@ import lombok.Data;
 import nju.seec.helper.sys.bok.vo.QuestionVO;
 import nju.seec.helper.util.JsonUtils;
 import nju.seec.helper.util.RestRequestUtil;
-import nju.seec.helper.util.cache.CaffeineCacheUtils;
+import nju.seec.helper.util.cache.BaseCacheUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
@@ -28,7 +28,7 @@ public class QuestionQueryApi {
     private String bokQuestionCacheName;
 
     private final RestRequestUtil restRequestUtil;
-    private final CaffeineCacheUtils cacheUtils;
+    private final BaseCacheUtils cacheUtils;
 
     private String byIdUrl;
 

+ 0 - 3
src/main/java/nju/seec/helper/sys/bok/api/UserInteractiveApi.java

@@ -11,7 +11,6 @@ import nju.seec.helper.sys.bok.vo.QuestionVO;
 import nju.seec.helper.util.JsonUtils;
 import nju.seec.helper.util.RestRequestUtil;
 import nju.seec.helper.util.StringUtils;
-import nju.seec.helper.util.cache.CaffeineCacheUtils;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageImpl;
@@ -29,7 +28,6 @@ import java.util.Map;
 @Data
 public class UserInteractiveApi {
     private final RestRequestUtil restRequestUtil;
-    private final CaffeineCacheUtils cacheUtils;
 
     private String rateUrl;
     private String collectionUrl;
@@ -45,7 +43,6 @@ public class UserInteractiveApi {
     }
 
     public Page<QuestionVO> getCollectAndRateQuestions(String userId, Boolean collect, Boolean rate, Pageable pageable) {
-        System.out.println(StringUtils.getSortString(pageable.getSort()));
         Map<String, ?> uriVariables = ImmutableMap.<String, Object>builder()
                 .put("userId", userId)
                 .put("collection", collect == null ? "" : collect)

+ 1 - 1
src/main/java/nju/seec/helper/sys/dataanalysis/api/ExerciseApi.java

@@ -23,7 +23,7 @@ public class ExerciseApi {
 
     @Async
     public void reportExercise(List<ExerciseRecord> exerciseRecords) {
-        System.out.println(exerciseRecords);
+//        System.out.println(exerciseRecords);
 //        restRequestUtil.sendPostRequest(exerciseReportUrl, exerciseRecords, Boolean.class);
     }
 

+ 2 - 2
src/main/java/nju/seec/helper/util/OssUtils.java

@@ -9,7 +9,7 @@ import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import nju.seec.helper.enums.ExceptionType;
 import nju.seec.helper.exception.HelperException;
-import nju.seec.helper.util.cache.CaffeineCacheUtils;
+import nju.seec.helper.util.cache.BaseCacheUtils;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Component;
@@ -34,7 +34,7 @@ public class OssUtils {
     private String bucketName;
     private long objectUrlLivingSeconds;
     private String objectUrlCacheName;
-    private final CaffeineCacheUtils cacheUtils;
+    private final BaseCacheUtils cacheUtils;
 
     public void upload(String objectName, InputStream inputStream) {
         OSS ossClient = getOss();

+ 52 - 0
src/main/java/nju/seec/helper/util/cache/BaseCacheUtils.java

@@ -0,0 +1,52 @@
+package nju.seec.helper.util.cache;
+
+import java.util.Collection;
+import java.util.Map;
+
+/**
+ * @author cst
+ */
+public interface BaseCacheUtils {
+    /**
+     * 设置缓存
+     *
+     * @param cacheName
+     * @param key
+     * @param value
+     */
+    void set(String cacheName, String key, Object value);
+
+    /**
+     * 批量设置缓存
+     *
+     * @param cacheName
+     * @param valueMap
+     */
+    void setAll(String cacheName, Map<String, Object> valueMap);
+
+    /**
+     * 获取缓存
+     *
+     * @param cacheName
+     * @param key
+     * @return
+     */
+    Object get(String cacheName, String key);
+
+    /**
+     * 批量获取缓存
+     *
+     * @param cacheName
+     * @param keys
+     * @return
+     */
+    Map<String, Object> multiGet(String cacheName, Collection<String> keys);
+
+    /**
+     * 删除缓存
+     *
+     * @param cacheName
+     * @param key
+     */
+    void delete(String cacheName, String key);
+}

+ 8 - 7
src/main/java/nju/seec/helper/util/cache/CaffeineCacheUtils.java

@@ -16,7 +16,7 @@ import java.util.stream.Collectors;
  * updated by cst
  */
 @Component
-public class CaffeineCacheUtils {
+public class CaffeineCacheUtils implements BaseCacheUtils {
     private final Cache<String, Object> cache;
 
     public CaffeineCacheUtils(@Value("${helper.cache.maximumSize}") Long maximumSize,
@@ -25,37 +25,38 @@ public class CaffeineCacheUtils {
         cache = Caffeine
                 .newBuilder()
                 .maximumSize(maximumSize)
-                .expireAfterAccess(expireAfterAccessInSeconds, TimeUnit.SECONDS)
+//                .expireAfterAccess(expireAfterAccessInSeconds, TimeUnit.SECONDS)
                 .expireAfterWrite(expireAfterWriteInSeconds, TimeUnit.SECONDS)
                 .build();
     }
 
+    @Override
     public void set(String cacheName, String key, Object value) {
         cache.put(combineKey(cacheName, key), value);
     }
 
+    @Override
     public void setAll(String cacheName, Map<String, Object> putAll) {
         final Map<String, Object> all = putAll.entrySet().stream().collect(Collectors.toMap(entry -> combineKey(cacheName, entry.getKey()), Map.Entry::getValue));
         cache.putAll(all);
     }
 
+    @Override
     public Object get(String cacheName, String key) {
         return cache.getIfPresent(combineKey(cacheName, key));
     }
 
+    @Override
     public Map<String, Object> multiGet(String cacheName, Collection<String> keys) {
         Collection<String> body = keys.stream().map(s -> combineKey(cacheName, s)).collect(Collectors.toList());
         return cache.getAllPresent(body);
     }
 
-    public void invalidate(String cacheName, String key) {
+    @Override
+    public void delete(String cacheName, String key) {
         cache.invalidate(combineKey(cacheName, key));
     }
 
-    public void invalidateAll() {
-        cache.invalidateAll();
-    }
-
     private String combineKey(String cacheName, String key) {
         return cacheName + ":" + key;
     }

+ 3 - 3
src/main/resources/application.yml

@@ -1,3 +1,3 @@
-#spring:
-#  profiles:
-#    active: dev
+spring:
+  profiles:
+    active: dev

+ 26 - 0
src/test/java/nju/seec/helper/HelperApplicationTests.java

@@ -1,14 +1,40 @@
 package nju.seec.helper;
 
+import com.google.common.collect.ImmutableList;
+import lombok.SneakyThrows;
+import nju.seec.helper.util.cache.BaseCacheUtils;
 import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
 class HelperApplicationTests {
+    @Autowired
+    private BaseCacheUtils cacheUtils;
 
+    @SneakyThrows
     @Test
     void contextLoads() {
 
     }
 
+    @Test
+    void testOne() {
+        System.out.println(cacheUtils.get("n", "123"));
+        cacheUtils.set("n", "123", "123456");
+        for (int i = 0; i < 10; i++)
+            System.out.println(cacheUtils.get("n", "123"));
+    }
+
+    @Test
+    void testAll() {
+        Map<String, Object> ids = Stream.of(1, 2, 3, 4, 5, 6).map(num -> num + "").collect(Collectors.toMap(key -> key, key -> key));
+        cacheUtils.setAll("test", ids);
+        System.out.println(cacheUtils.multiGet("test", ImmutableList.of("1", "2", "3")));
+    }
+
 }