From 57551e95215105566964075a21c5431b9142b1a0 Mon Sep 17 00:00:00 2001 From: taixingyiji Date: Thu, 27 Feb 2025 16:50:27 +0800 Subject: [PATCH] feat: add page data cache --- hcframe-parent/hcframe-base/pom.xml | 3 +- .../taixingyiji/base/common/WebPageInfo.java | 5 ++ .../base/common/utils/MyPageHelper.java | 90 ++++++++++++++++++- .../base/module/cache/emum/CacheType.java | 3 +- .../base/module/cache/impl/TableCache.java | 6 +- .../module/data/module/BaseMapperImpl.java | 45 ++++++++-- 6 files changed, 135 insertions(+), 17 deletions(-) diff --git a/hcframe-parent/hcframe-base/pom.xml b/hcframe-parent/hcframe-base/pom.xml index 3daea12..d4a7b19 100644 --- a/hcframe-parent/hcframe-base/pom.xml +++ b/hcframe-parent/hcframe-base/pom.xml @@ -8,7 +8,6 @@ 1.0-SNAPSHOT 4.0.0 - hcframe-base 1.2.3-SNAPSHOT @@ -179,7 +178,7 @@ com.github.pagehelper pagehelper-spring-boot-starter - 1.2.13 + 2.1.0 org.apache.commons diff --git a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/common/WebPageInfo.java b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/common/WebPageInfo.java index d03a9dc..de236ab 100644 --- a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/common/WebPageInfo.java +++ b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/common/WebPageInfo.java @@ -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()); } diff --git a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/common/utils/MyPageHelper.java b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/common/utils/MyPageHelper.java index 5bd05bf..3c1188d 100644 --- a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/common/utils/MyPageHelper.java +++ b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/common/utils/MyPageHelper.java @@ -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> noCount(WebPageInfo webPageInfo, Supplier>> 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> myStart(WebPageInfo webPageInfo, Supplier>> 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> start(WebPageInfo webPageInfo, String sql, Supplier>> 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> 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> 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> pageInfo = new PageInfo<>(querySupplier.get()); + pageInfo.setTotal(total); + return pageInfo; + } + } + + public static void orderBy(String sortField, String order) { + PageHelper.orderBy(sortField + " " + order); } } diff --git a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/cache/emum/CacheType.java b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/cache/emum/CacheType.java index 55330d3..5d8b946 100644 --- a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/cache/emum/CacheType.java +++ b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/cache/emum/CacheType.java @@ -11,5 +11,6 @@ public enum CacheType { // 基表信息缓存 baseCache, // 数据库连接缓存 - datasourceCache + datasourceCache, + pageCache, } diff --git a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/cache/impl/TableCache.java b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/cache/impl/TableCache.java index 20f948c..1b964a5 100644 --- a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/cache/impl/TableCache.java +++ b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/cache/impl/TableCache.java @@ -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> baseList = baseMapper.selectByCondition(osSysTable1.getTableName(),condition); + Condition condition = Condition.creatCriteria().andEqual("DELETED", 1).build(); + List> 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; diff --git a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/BaseMapperImpl.java b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/BaseMapperImpl.java index 9a556c7..2d5b8e2 100644 --- a/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/BaseMapperImpl.java +++ b/hcframe-parent/hcframe-base/src/main/java/com/taixingyiji/base/module/data/module/BaseMapperImpl.java @@ -376,16 +376,22 @@ public class BaseMapperImpl implements BaseMapper { @Override public PageInfo> selectByEqual(DataMap dataMap, Map 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> selectByEqual(String tableName, Map 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> 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 PageInfo> selectByCondition(DataMap 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> 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> selectByCondition(String tableName, List 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> 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> 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> selectSqlByPage(String sql, Map 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)); }