feat: add page data cache
This commit is contained in:
		| @@ -8,7 +8,6 @@ | ||||
|         <version>1.0-SNAPSHOT</version> | ||||
|     </parent> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
|  | ||||
|     <artifactId>hcframe-base</artifactId> | ||||
|     <version>1.2.3-SNAPSHOT</version> | ||||
|  | ||||
| @@ -179,7 +178,7 @@ | ||||
|         <dependency> | ||||
|             <groupId>com.github.pagehelper</groupId> | ||||
|             <artifactId>pagehelper-spring-boot-starter</artifactId> | ||||
|             <version>1.2.13</version> | ||||
|             <version>2.1.0</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.apache.commons</groupId> | ||||
|   | ||||
| @@ -51,6 +51,11 @@ public class WebPageInfo implements Serializable { | ||||
|             example = "asc") | ||||
|     private String order = ASC; | ||||
|  | ||||
|     @ApiModelProperty( | ||||
|             value = "开启缓存" | ||||
|     ) | ||||
|     private boolean enableCache = false; | ||||
|  | ||||
|     public static boolean hasSort(WebPageInfo webPageInfo) { | ||||
|         return !StringUtils.isBlank(webPageInfo.getSortField()); | ||||
|     } | ||||
|   | ||||
| @@ -1,19 +1,101 @@ | ||||
| package com.taixingyiji.base.common.utils; | ||||
|  | ||||
| import cn.hutool.json.JSONObject; | ||||
| import cn.hutool.json.JSONUtil; | ||||
| import com.github.pagehelper.PageInfo; | ||||
| import com.taixingyiji.base.common.WebPageInfo; | ||||
| import com.github.pagehelper.PageHelper; | ||||
| import com.taixingyiji.base.module.cache.CacheService; | ||||
| import com.taixingyiji.base.module.cache.base.BaseCache; | ||||
| import com.taixingyiji.base.module.cache.emum.CacheType; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.stereotype.Component; | ||||
| import org.springframework.util.StringUtils; | ||||
|  | ||||
| import javax.annotation.PostConstruct; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.function.Supplier; | ||||
|  | ||||
| @Component | ||||
| public class MyPageHelper { | ||||
|  | ||||
|     private static MyPageHelper myPageHelper; | ||||
|     @Autowired | ||||
|     BaseCache baseCache; | ||||
|  | ||||
|     @PostConstruct | ||||
|     public void init() { | ||||
|         myPageHelper = this; | ||||
|     } | ||||
|  | ||||
|     public static void start(WebPageInfo webPageInfo) { | ||||
|         if (WebPageInfo.hasSort(webPageInfo)) { | ||||
|             PageHelper.startPage(webPageInfo.getPageNum(), webPageInfo.getPageSize(), webPageInfo.getSortSql()); | ||||
|             PageHelper.startPage(webPageInfo.getPageNum(), webPageInfo.getPageSize(), webPageInfo.getSortSql()).setAsyncCount(true); | ||||
|         } else { | ||||
|             PageHelper.startPage(webPageInfo.getPageNum(), webPageInfo.getPageSize()); | ||||
|             PageHelper.startPage(webPageInfo.getPageNum(), webPageInfo.getPageSize()).setAsyncCount(true); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public static void orderBy(String sortField,String order){ | ||||
|         PageHelper.orderBy(sortField+" "+order); | ||||
|     public static void noCount(WebPageInfo webPageInfo) { | ||||
|         if (WebPageInfo.hasSort(webPageInfo)) { | ||||
|             PageHelper.startPage(webPageInfo.getPageNum(), webPageInfo.getPageSize(), false).setOrderBy(webPageInfo.getSortSql()); | ||||
|         } else { | ||||
|             PageHelper.startPage(webPageInfo.getPageNum(), webPageInfo.getPageSize(), false); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public static PageInfo<Map<String, Object>> noCount(WebPageInfo webPageInfo, Supplier<List<Map<String, Object>>> querySupplier) { | ||||
|         if (WebPageInfo.hasSort(webPageInfo)) { | ||||
|             return PageHelper.startPage(webPageInfo.getPageNum(), webPageInfo.getPageSize(), webPageInfo.getSortSql()).count(false).doSelectPageInfo(querySupplier::get); | ||||
|         } else { | ||||
|             return PageHelper.startPage(webPageInfo.getPageNum(), webPageInfo.getPageSize()).count(false).doSelectPageInfo(querySupplier::get); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public static PageInfo<Map<String, Object>> myStart(WebPageInfo webPageInfo, Supplier<List<Map<String, Object>>> querySupplier) { | ||||
|         if (WebPageInfo.hasSort(webPageInfo)) { | ||||
|             return PageHelper.startPage(webPageInfo.getPageNum(), webPageInfo.getPageSize(), webPageInfo.getSortSql()).doSelectPageInfo(querySupplier::get); | ||||
|         } else { | ||||
|             return PageHelper.startPage(webPageInfo.getPageNum(), webPageInfo.getPageSize()).doSelectPageInfo(querySupplier::get); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public static PageInfo<Map<String, Object>> start(WebPageInfo webPageInfo, String sql, Supplier<List<Map<String, Object>>> querySupplier) { | ||||
|         Long currentTime = System.currentTimeMillis(); | ||||
|         String data = myPageHelper.baseCache.get(CacheType.pageCache.toString(), sql, String.class); | ||||
|         JSONObject jsonObject = new JSONObject(); | ||||
|         if (StringUtils.isEmpty(data)) { | ||||
|             PageInfo<Map<String, Object>> result = myStart(webPageInfo, querySupplier); | ||||
|             jsonObject.set("time", currentTime); | ||||
|             jsonObject.set("count", result.getTotal()); | ||||
|             myPageHelper.baseCache.add(CacheType.pageCache.toString(), sql, jsonObject.toString(), String.class); | ||||
|             return result; | ||||
|         } else { | ||||
|             JSONObject cacheJson = JSONUtil.parseObj(data); | ||||
|             Long saveTime = (Long) cacheJson.get("time"); | ||||
|             long timeDiffInSeconds = (currentTime - saveTime) / 1000; | ||||
|             if (timeDiffInSeconds > 60) { | ||||
|                 Thread thread = new Thread(new Runnable() { | ||||
|                     @Override | ||||
|                     public void run() { | ||||
|                         PageInfo<Map<String, Object>> result = myStart(webPageInfo, querySupplier); | ||||
|                         jsonObject.set("time", currentTime); | ||||
|                         jsonObject.set("count", result.getTotal()); | ||||
|                         myPageHelper.baseCache.add(CacheType.pageCache.toString(), sql, jsonObject.toString(), String.class); | ||||
|                     } | ||||
|                 }); | ||||
|                 thread.start(); | ||||
|             } | ||||
|             long total = cacheJson.get("count", Long.class); | ||||
|             noCount(webPageInfo); | ||||
|             PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(querySupplier.get()); | ||||
|             pageInfo.setTotal(total); | ||||
|             return pageInfo; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public static void orderBy(String sortField, String order) { | ||||
|         PageHelper.orderBy(sortField + " " + order); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -11,5 +11,6 @@ public enum CacheType { | ||||
|     // 基表信息缓存 | ||||
|     baseCache, | ||||
|     // 数据库连接缓存 | ||||
|     datasourceCache | ||||
|     datasourceCache, | ||||
|     pageCache, | ||||
| } | ||||
|   | ||||
| @@ -85,12 +85,12 @@ public class TableCache implements CacheService { | ||||
|                 case tableCache: | ||||
|                     OsSysTable osSysTable = osSysTableMapper.getTableAllInfo((String) key); | ||||
|                     JudgeException.isNull(osSysTable, "can not find key " + key + " in cache which cache name is " + name); | ||||
|                     baseCache.add(name.toString(), key, osSysTableMapper.getTableAllInfo((String)key), OsSysTable.class); | ||||
|                     baseCache.add(name.toString(), key, osSysTableMapper.getTableAllInfo((String) key), OsSysTable.class); | ||||
|                     break; | ||||
|                 case baseCache: | ||||
|                     OsSysTable osSysTable1 = getCacheValue(CacheType.tableCache, key, OsSysTable.class); | ||||
|                     Condition condition = Condition.creatCriteria().andEqual("DELETED",1).build(); | ||||
|                     List<Map<String, Object>> baseList = baseMapper.selectByCondition(osSysTable1.getTableName(),condition); | ||||
|                     Condition condition = Condition.creatCriteria().andEqual("DELETED", 1).build(); | ||||
|                     List<Map<String, Object>> baseList = baseMapper.selectByCondition(osSysTable1.getTableName(), condition); | ||||
|                     JudgeException.isNull(baseList, "can not find key " + key + " in cache which cache name is " + name); | ||||
|                     baseCache.add(name.toString(), key, baseList, List.class); | ||||
|                     break; | ||||
|   | ||||
| @@ -376,16 +376,22 @@ public class BaseMapperImpl implements BaseMapper { | ||||
|     @Override | ||||
|     public <E> PageInfo<Map<String, Object>> selectByEqual(DataMap<E> dataMap, Map<String, Object> map, WebPageInfo webPageInfo) { | ||||
|         JudgesNull(dataMap.getTableName(), "tableName can not be null!"); | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         Condition condition = equal(dataMap, map); | ||||
|         if(webPageInfo.isEnableCache()){ | ||||
|            return MyPageHelper.start(webPageInfo,condition.getSql(),() -> selectList(condition)); | ||||
|         } | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         return new PageInfo<>(selectList(condition)); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public PageInfo<Map<String, Object>> selectByEqual(String tableName, Map<String, Object> map, WebPageInfo webPageInfo) { | ||||
|         JudgesNull(tableName, "tableName can not be null!"); | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         Condition condition = equal(DataMap.builder().tableName(tableName).build(), map); | ||||
|         if(webPageInfo.isEnableCache()){ | ||||
|             return MyPageHelper.start(webPageInfo,condition.getSql(),() -> selectList(condition)); | ||||
|         } | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         return new PageInfo<>(selectList(condition)); | ||||
|     } | ||||
|  | ||||
| @@ -466,38 +472,57 @@ public class BaseMapperImpl implements BaseMapper { | ||||
|     @Override | ||||
|     public PageInfo<Map<String, Object>> selectByCondition(Condition condition, WebPageInfo webPageInfo) { | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         if(webPageInfo.isEnableCache()){ | ||||
|             return MyPageHelper.start(webPageInfo,condition.getSql(),() -> selectList(condition)); | ||||
|         } | ||||
|         return new PageInfo<>(selectList(condition)); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public <E> PageInfo<Map<String, Object>> selectByCondition(DataMap<E> dataMap, Condition condition, WebPageInfo webPageInfo) { | ||||
|         JudgesNull(dataMap.getTableName(), "tableName can not be null!"); | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         condition = condition.toCreatCriteria(dataMap).build(); | ||||
|         if(webPageInfo.isEnableCache()){ | ||||
|             Condition finalCondition = condition; | ||||
|             return MyPageHelper.start(webPageInfo,condition.getSql(),() -> selectList(finalCondition)); | ||||
|         } | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         return new PageInfo<>(selectList(condition)); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public PageInfo<Map<String, Object>> selectByCondition(String tableName, Condition condition, WebPageInfo webPageInfo) { | ||||
|         JudgesNull(tableName, "tableName can not be null!"); | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         condition = condition.toCreatCriteria(DataMap.builder().tableName(tableName).build()).build(); | ||||
|         if(webPageInfo.isEnableCache()){ | ||||
|             Condition finalCondition = condition; | ||||
|             return MyPageHelper.start(webPageInfo,condition.getSql(),() -> selectList(finalCondition)); | ||||
|         } | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         return new PageInfo<>(selectList(condition)); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public PageInfo<Map<String, Object>> selectByCondition(String tableName, List<String> fieldList, Condition condition, WebPageInfo webPageInfo) { | ||||
|         JudgesNull(tableName, "tableName can not be null!"); | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         condition = condition.toCreatCriteria(DataMap.builder().tableName(tableName).fieldList(fieldList).build()).build(); | ||||
|         if(webPageInfo.isEnableCache()){ | ||||
|             Condition finalCondition = condition; | ||||
|             return MyPageHelper.start(webPageInfo,condition.getSql(),() -> selectList(finalCondition)); | ||||
|         } | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         return new PageInfo<>(selectList(condition)); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public PageInfo<Map<String, Object>> selectByCondition(String tableName, String fieldList, Condition condition, WebPageInfo webPageInfo) { | ||||
|         JudgesNull(tableName, "tableName can not be null!"); | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         condition = condition.toCreatCriteria(DataMap.builder().tableName(tableName).fields(fieldList).build()).build(); | ||||
|         if(webPageInfo.isEnableCache()){ | ||||
|             Condition finalCondition = condition; | ||||
|             return MyPageHelper.start(webPageInfo,condition.getSql(),() -> selectList(finalCondition)); | ||||
|         } | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         return new PageInfo<>(selectList(condition)); | ||||
|     } | ||||
|  | ||||
| @@ -602,6 +627,9 @@ public class BaseMapperImpl implements BaseMapper { | ||||
|  | ||||
|     @Override | ||||
|     public PageInfo<Map<String, Object>> selectSqlByPage(String sql, WebPageInfo webPageInfo) { | ||||
|         if(webPageInfo.isEnableCache()){ | ||||
|             return MyPageHelper.start(webPageInfo,sql,() -> (tableMapper.useSql(sql))); | ||||
|         } | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         return new PageInfo<>(tableMapper.useSql(sql)); | ||||
|     } | ||||
| @@ -614,8 +642,11 @@ public class BaseMapperImpl implements BaseMapper { | ||||
|  | ||||
|     @Override | ||||
|     public PageInfo<Map<String, Object>> selectSqlByPage(String sql, Map<String, Object> params, WebPageInfo webPageInfo) { | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         params.put("sql",sql); | ||||
|         if(webPageInfo.isEnableCache()){ | ||||
|             return MyPageHelper.start(webPageInfo,sql,() -> sqlSessionTemplate.selectList(TABLE_MAPPER_PACKAGE + "useSql", params)); | ||||
|         } | ||||
|         MyPageHelper.start(webPageInfo); | ||||
|         return new PageInfo<>(sqlSessionTemplate.selectList(TABLE_MAPPER_PACKAGE + "useSql", params)); | ||||
|     } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user