瀏覽代碼

增加Cache缓存功能

chenw 6 年之前
父節點
當前提交
b5c86685d8

+ 5 - 0
pom.xml

@@ -54,6 +54,11 @@
       <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
 
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-cache</artifactId>
+    </dependency>
+
     <dependency>
       <groupId>com.auth0</groupId>
       <artifactId>java-jwt</artifactId>

+ 6 - 0
src/main/java/com/Application.java

@@ -4,7 +4,11 @@ import com.config.BasesSource.DynamicDataSourceRegister;
 import com.util.ContextUtil;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.cache.support.SimpleCacheManager;
 import org.springframework.context.ApplicationContext;
+import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Import;
 import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@@ -12,6 +16,8 @@ import springfox.documentation.swagger2.annotations.EnableSwagger2;
 //@ComponentScan(basePackages={"com.util"})
 @EnableSwagger2
 @Import({DynamicDataSourceRegister.class})
+@EnableCaching
+@AutoConfigureCache
 public class Application {
     public static void main(String[] args) {
         ApplicationContext app = SpringApplication.run(Application.class, args);

+ 16 - 2
src/main/java/com/server/dataSource/DataConnectorService.java

@@ -31,6 +31,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
 import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
@@ -127,7 +129,13 @@ public class DataConnectorService {
         return new RepEntity(RepCode.success,dataId);
     }
 
-    //查看数据源列表
+    /**
+     * 查看数据源列表
+     * @param token
+     * @param testPage
+     * @return
+     */
+    @Cacheable(value = "dataConnector", key = "#testPage.pageNum")
     public RepEntity getListConnector(String token, TestPage testPage){
         Map<String, String> resultMap = getTokenData.getTokenData(token);
         String id = resultMap.get("id");
@@ -160,6 +168,7 @@ public class DataConnectorService {
     /*
     查看单个数据源配置
      */
+    @Cacheable(value = "singleDataConnector", key = "#body")
     public RepEntity getConnector(int id){
         DataConnector dataConnector = dataConnectorMapper.getOneData(id);
         if (dataConnector == null){
@@ -185,6 +194,7 @@ public class DataConnectorService {
     /*
     更新数据源配置
      */
+    @CacheEvict(value = "singleDataConnector", key = "#body.dataId")
     public RepEntity updateData(DataConnectorInfo dataConnectorInfo){
 
         if (dataConnectorInfo == null && "".equals(dataConnectorInfo)){
@@ -247,6 +257,7 @@ public class DataConnectorService {
     /*
     更新数据源分组
      */
+    @CacheEvict(value = "singleDataConnector", key = "#body.id")
     public RepEntity updateConfigGroup(GroupUpdateId groupUpdateId){
         int id = groupUpdateId.getId();
         int groupId = groupUpdateId.getGroupId();
@@ -257,7 +268,7 @@ public class DataConnectorService {
     /*
     删除数据源
      */
-
+    @CacheEvict(value = "singleDataConnector")
     public RepEntity delDataConnector(List<Integer> idList){
         Iterator isList = idList.iterator();
 //        while (isList.hasNext()){
@@ -286,6 +297,7 @@ public class DataConnectorService {
     /*
     更新数据源分组
      */
+    @CacheEvict(value = "singleDataConnector", key = "#body.id")
     public RepEntity updataDataConnectorGroup(GroupInfo groupInfo){
         dataConnectorMapper.updataConnectorGroup(groupInfo);
         return new RepEntity(RepCode.success);
@@ -294,6 +306,7 @@ public class DataConnectorService {
     /*
    批量更新数据源分组
     */
+    @CacheEvict(value = "singleDataConnector")
     public RepEntity updataConnectorGroups(List<GroupInfo> groupInfoList){
         Iterator isList = groupInfoList.iterator();
         GroupInfo groupInfo = new GroupInfo();
@@ -308,6 +321,7 @@ public class DataConnectorService {
     /*
     删除数据源分组
      */
+    @CacheEvict(value = "singleDataConnector")
     public RepEntity delDataConnectorGroup(List<Integer> idList){
         List<Integer> integerList = idList;
         Iterator isList = integerList.iterator();

+ 3 - 1
src/main/resources/application.properties

@@ -73,4 +73,6 @@ mybatis.configuration.callSettersOnNulls=true
 
 server.port=8011
 #server.session.timeout=10
-server.tomcat.uri-encoding=UTF-8
+server.tomcat.uri-encoding=UTF-8
+#debug=true
+#logging.level.root=debug

+ 83 - 0
src/test/java/com/test/DataConnectorServiceTest.java

@@ -0,0 +1,83 @@
+package com.test;
+
+import com.alibaba.druid.support.json.JSONUtils;
+import com.alibaba.fastjson.JSON;
+import com.model.pojo.RepEntity;
+import com.model.pojo.TestPage;
+import com.server.dataSource.DataConnectorService;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import static org.junit.Assert.*;
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@SpringBootTest
+@EnableAutoConfiguration
+public class DataConnectorServiceTest {
+
+    @Autowired
+    private DataConnectorService dataConnectorService;
+
+    private final String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwb3N0Ijoi6L2v5Lu256eRIiwibmFtZSI6IumZiOeCnCIsImlkIjoiNDEiLCJkZXBhcnRtZW50Ijoi6L2v5Lu25bel56iL5biIIiwiZXhwIjoxNTUxMzI5MDg0LCJpYXQiOjE1NTEzMjE4ODR9.HbZze4WiYX6Zn1aCjnoV60bPFbQdR79ijNLt__rOdc4";
+
+    @Test
+    public void setDataConnector() throws Exception {
+    }
+
+    @Test
+    public void getListConnector() throws Exception {
+        RepEntity repEntity = dataConnectorService.getListConnector(token, new TestPage());
+        System.out.println(JSON.toJSONString(repEntity));
+        RepEntity repEntity2 = dataConnectorService.getListConnector(token, new TestPage());
+        RepEntity repEntity3 = dataConnectorService.getListConnector(token, new TestPage());
+    }
+
+    @Test
+    public void getConnector() throws Exception {
+    }
+
+    @Test
+    public void updateData() throws Exception {
+    }
+
+    @Test
+    public void changeOrder() throws Exception {
+    }
+
+    @Test
+    public void updateConfigGroup() throws Exception {
+    }
+
+    @Test
+    public void delDataConnector() throws Exception {
+    }
+
+    @Test
+    public void setDataConnectorGroup() throws Exception {
+    }
+
+    @Test
+    public void updataDataConnectorGroup() throws Exception {
+    }
+
+    @Test
+    public void updataConnectorGroups() throws Exception {
+    }
+
+    @Test
+    public void delDataConnectorGroup() throws Exception {
+    }
+
+    @Test
+    public void getConnectorGroup() throws Exception {
+    }
+
+    @Test
+    public void getConnectorData() throws Exception {
+    }
+
+}