diff --git a/.beagle/dockerfile b/.beagle/dockerfile index 951573bdb5f2d01e1f53171a2824a786013fee29..08c2aaafed489901f2fbf49788b90774b1d381bc 100644 --- a/.beagle/dockerfile +++ b/.beagle/dockerfile @@ -5,5 +5,6 @@ ARG VERSION LABEL maintainer=${AUTHOR} version=${VERSION} RUN echo "Asia/Shanghai" > /etc/timezone -COPY ./target/sdt-kangdun-api-1.0.0.jar /app/sdt-kangdun-api-1.0.0.jar +COPY ./target/sdk-golden-api-1.0.0.jar /app/sdk-golden-api-1.0.0.jar +COPY ./conf /app/conf/ ENTRYPOINT ["java","-jar","/app/sdt-kangdun-api-1.0.0.jar"] diff --git a/pom.xml b/pom.xml index a5438921c21f9334cbc08fc227762c242bde53a1..ec57bde757549ca72d879ba1c2fd35e374b6c5f5 100644 --- a/pom.xml +++ b/pom.xml @@ -8,33 +8,27 @@ 2.7.0 - com.beagle.sdt.kangdun - sdt-kangdun-api + com.beagle.sdk.golden + sdk-golden-api 1.0.0 jar - sdt-kangdun-api + sdk-golden-api - 8 + 8 + 8 + UTF-8 2.0.40 3.0.3 - net.java.dev.jna - jna - 4.5.2 + rtdb + rtdb-java-sdk + 3.0.48 ${pom.basedir}/lib/rtdb-java-sdk-3.0.48.jar system - - org.springframework.boot spring-boot-starter-log4j2 @@ -115,6 +109,15 @@ org.springframework spring-test + + + com.github.ben-manes.caffeine + caffeine + + + org.springframework.boot + spring-boot-starter-cache + diff --git a/src/main/java/com/beagle/base/BaseApplication.java b/src/main/java/com/beagle/base/BaseApplication.java index 13f3c604e1bcd820907a0635f4048630e202f56b..5fc112559d250bb486b7b4f6b12d7c6d83409344 100644 --- a/src/main/java/com/beagle/base/BaseApplication.java +++ b/src/main/java/com/beagle/base/BaseApplication.java @@ -4,13 +4,14 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.scheduling.annotation.EnableScheduling; /** * @author wuwanli */ -@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) -@EnableScheduling +@SpringBootApplication +@EnableCaching public class BaseApplication { public static void main(String[] args) { diff --git a/src/main/java/com/beagle/base/bean/vo/BaseCacheByIdParam.java b/src/main/java/com/beagle/base/bean/vo/BaseCacheByIdParam.java new file mode 100644 index 0000000000000000000000000000000000000000..8ae91948ee74bd03485a96be51ee1fc7406b637a --- /dev/null +++ b/src/main/java/com/beagle/base/bean/vo/BaseCacheByIdParam.java @@ -0,0 +1,11 @@ +package com.beagle.base.bean.vo; + +import lombok.Data; + +import javax.validation.constraints.NotNull; + +@Data +public class BaseCacheByIdParam { + @NotNull(message = "id不能为空") + private Integer id; +} diff --git a/src/main/java/com/beagle/base/bean/vo/BaseCacheByTagParam.java b/src/main/java/com/beagle/base/bean/vo/BaseCacheByTagParam.java new file mode 100644 index 0000000000000000000000000000000000000000..0274e72b47276ac4468ac1080da5a062f52a02b5 --- /dev/null +++ b/src/main/java/com/beagle/base/bean/vo/BaseCacheByTagParam.java @@ -0,0 +1,11 @@ +package com.beagle.base.bean.vo; + +import lombok.Data; + +import javax.validation.constraints.NotNull; + +@Data +public class BaseCacheByTagParam { + @NotNull(message = "tag不能为空") + private String tag; +} diff --git a/src/main/java/com/beagle/base/bean/vo/BasePointVO.java b/src/main/java/com/beagle/base/bean/vo/BasePointVO.java new file mode 100644 index 0000000000000000000000000000000000000000..fea8b97ada0748f41f4dbbb74b8f7c8d0a54f6ec --- /dev/null +++ b/src/main/java/com/beagle/base/bean/vo/BasePointVO.java @@ -0,0 +1,11 @@ +package com.beagle.base.bean.vo; + +import com.rtdb.enums.ValueType; +import lombok.Data; + +@Data +public class BasePointVO { + private String tag = ""; + private int id; + ValueType type; +} diff --git a/src/main/java/com/beagle/base/BasePointsPropertiesNewParam.java b/src/main/java/com/beagle/base/bean/vo/BasePointsPropertiesNewParam.java similarity index 76% rename from src/main/java/com/beagle/base/BasePointsPropertiesNewParam.java rename to src/main/java/com/beagle/base/bean/vo/BasePointsPropertiesNewParam.java index 49dbe5690c311969d04e96905444a5b6e0849819..b3c22d07696953fccceee1dbb1e3dd46bb26c218 100644 --- a/src/main/java/com/beagle/base/BasePointsPropertiesNewParam.java +++ b/src/main/java/com/beagle/base/bean/vo/BasePointsPropertiesNewParam.java @@ -1,4 +1,4 @@ -package com.beagle.base; +package com.beagle.base.bean.vo; import lombok.Data; diff --git a/src/main/java/com/beagle/base/UserVo.java b/src/main/java/com/beagle/base/bean/vo/UserVo.java similarity index 85% rename from src/main/java/com/beagle/base/UserVo.java rename to src/main/java/com/beagle/base/bean/vo/UserVo.java index e67f1edbbc73d520a413560f958b995fe1aa1b83..16e29ca3bd2adaa2682ab8e524d3a7cb74e331c8 100644 --- a/src/main/java/com/beagle/base/UserVo.java +++ b/src/main/java/com/beagle/base/bean/vo/UserVo.java @@ -1,4 +1,4 @@ -package com.beagle.base; +package com.beagle.base.bean.vo; import lombok.Data; diff --git a/src/main/java/com/beagle/base/common/cache/caffeine/CacheConfig.java b/src/main/java/com/beagle/base/common/cache/caffeine/CacheConfig.java new file mode 100644 index 0000000000000000000000000000000000000000..57ddca4940f386a3ab0d98d5af2bfe99648011dd --- /dev/null +++ b/src/main/java/com/beagle/base/common/cache/caffeine/CacheConfig.java @@ -0,0 +1,167 @@ +package com.beagle.base.common.cache.caffeine; + + +import cn.hutool.core.collection.CollUtil; +import com.beagle.base.bean.vo.BasePointVO; +import com.beagle.base.bean.vo.BasePointsPropertiesNewParam; +import com.beagle.base.bean.vo.BasePointsPropertiesParam; +import com.beagle.base.common.contant.CacheConstants; +import com.beagle.base.common.contant.CodeEnum; +import com.beagle.base.common.exception.BeagleException; +import com.beagle.base.service.BaseService; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.CacheLoader; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; +import com.rtdb.model.FullPoint; +import com.rtdb.model.MinPoint; +import lombok.AllArgsConstructor; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.springframework.cache.CacheManager; +import org.springframework.cache.caffeine.CaffeineCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; + +import javax.annotation.Resource; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; + +@Configuration +@AllArgsConstructor +public class CacheConfig { + + @Resource + private BaseService baseService; + @Bean + public CacheLoader cacheLoader() { + return new CacheLoader() { + @Override + public Object load(Object key) throws Exception { + System.out.println(key + "缓存为空时,初始化加载数据"); + // 通过key的前缀,比如user_来区分不同的缓存名称类型,来做对应的初始化操作 + if (key.toString().startsWith(CacheConstants.TAG_PREFIX)) { + // 用户信息 + // 获取userId + String tag = key.toString().substring(key.toString().indexOf("_") + 1); + // 从数据库中获取值 + BasePointsPropertiesNewParam basePointsPropertiesNewParam = new BasePointsPropertiesNewParam(); + basePointsPropertiesNewParam.setTagName(new String[]{tag}); + List pointsPropertiesTags = baseService.getPointsPropertiesTags(basePointsPropertiesNewParam); + if (CollUtil.isNotEmpty(pointsPropertiesTags)) { + MinPoint minPoint = pointsPropertiesTags.get(0); + return minPoint.getId(); + } + return null; + }else if (key.toString().startsWith(CacheConstants.POINT_PREFIX)){ + // 用户信息 + // 获取userId + String id = key.toString().substring(key.toString().indexOf("_") + 1); + // 从数据库中获取值 + BasePointsPropertiesParam basePointsPropertiesParam = new BasePointsPropertiesParam(); + basePointsPropertiesParam.setIds(new int[]{Integer.parseInt(id)}); + List fullPointList = baseService.getPointsProperties(basePointsPropertiesParam); + if (CollUtil.isNotEmpty(fullPointList)) { + FullPoint fullPoint = fullPointList.get(0); + BasePointVO basePointVO = new BasePointVO(); + basePointVO.setId(fullPoint.getBasePoint().getId()); + basePointVO.setTag(fullPoint.getBasePoint().getTag()); + basePointVO.setType(fullPoint.getBasePoint().getType()); + return basePointVO; + } + return null; + } + else { + return null; + } + } + + @Override + public @Nullable Object reload(@NonNull Object key, @NonNull Object oldValue) throws Exception { + // 过期重新加载数据 + System.out.println(key + "过期重新加载数据"); + // 通过key的前缀,比如user_来区分不同的缓存名称类型,来做对应的初始化操作 + if (key.toString().startsWith(CacheConstants.TAG_PREFIX)) { + // 用户信息 + // 获取userId + String tag = key.toString().substring(key.toString().indexOf("_") + 1); + // 从数据库中获取值 + BasePointsPropertiesNewParam basePointsPropertiesNewParam = new BasePointsPropertiesNewParam(); + basePointsPropertiesNewParam.setTagName(new String[]{tag}); + List pointsPropertiesTags = baseService.getPointsPropertiesTags(basePointsPropertiesNewParam); + if (CollUtil.isNotEmpty(pointsPropertiesTags)) { + MinPoint minPoint = pointsPropertiesTags.get(0); + return minPoint.getId(); + } + return null; + }else if (key.toString().startsWith(CacheConstants.POINT_PREFIX)){ + // 用户信息 + // 获取userId + String id = key.toString().substring(key.toString().indexOf("_") + 1); + // 从数据库中获取值 + BasePointsPropertiesParam basePointsPropertiesParam = new BasePointsPropertiesParam(); + basePointsPropertiesParam.setIds(new int[]{Integer.parseInt(id)}); + List fullPointList = baseService.getPointsProperties(basePointsPropertiesParam); + if (CollUtil.isNotEmpty(fullPointList)) { + FullPoint fullPoint = fullPointList.get(0); + BasePointVO basePointVO = new BasePointVO(); + basePointVO.setId(fullPoint.getBasePoint().getId()); + basePointVO.setTag(fullPoint.getBasePoint().getTag()); + basePointVO.setType(fullPoint.getBasePoint().getType()); + return basePointVO; + } + return null; + } + else { + return null; + } + } + }; + } + + @Bean(name = "loadingCache") + @Primary // 定义bean的加载顺序为第一个 + public LoadingCache loadingCache() { + return caffeine().build(cacheLoader()); + } + + @Bean(name = "caffeineCache") + public Cache cache(){ + return noRefreshcaffeine().build(); + } + @Bean + public CacheManager cacheManager() { + CaffeineCacheManager cacheManager = new CaffeineCacheManager(); + cacheManager.setCaffeine(caffeine()); + cacheManager.setCacheLoader(cacheLoader()); + cacheManager.setCacheNames(Arrays.asList("myCache", "myCache2", "user","userRule")); + cacheManager.setAllowNullValues(true); + return cacheManager; + } + + @Bean(name = "caffeine") + public Caffeine caffeine() { + return Caffeine.newBuilder() + // 设置过期重新刷新时间,超过该时间后,接口调用会自动去调用reload方法重新获取数据 + .refreshAfterWrite(10L, TimeUnit.MINUTES) + .maximumSize(10000) + .initialCapacity(100) + .softValues() + // 设置过期时间,超过后,缓存的值会被删除,下次调用后,因为缓存中没有值了,这时候会调用load方法,重新获取数据 + .expireAfterWrite(Long.MAX_VALUE, TimeUnit.NANOSECONDS) // 永不过期 + ; + } + + @Bean(name = "noRefreshCaffeine") + public Caffeine noRefreshcaffeine() { + return Caffeine.newBuilder() + // 设置过期重新刷新时间,超过该时间后,接口调用会自动去调用reload方法重新获取数据 + .maximumSize(1000) + .initialCapacity(100) + .softValues() + // 设置过期时间,超过后,缓存的值会被删除,下次调用后,因为缓存中没有值了,这时候会调用load方法,重新获取数据 + .expireAfterWrite(10L, TimeUnit.SECONDS); + } +} diff --git a/src/main/java/com/beagle/base/common/contant/CacheConstants.java b/src/main/java/com/beagle/base/common/contant/CacheConstants.java new file mode 100644 index 0000000000000000000000000000000000000000..d1d1eb6f3217bbd60d321fbf453889e217507c09 --- /dev/null +++ b/src/main/java/com/beagle/base/common/contant/CacheConstants.java @@ -0,0 +1,6 @@ +package com.beagle.base.common.contant; + +public class CacheConstants { + public static final String TAG_PREFIX = "tag_"; + public static final String POINT_PREFIX = "point_"; +} diff --git a/src/main/java/com/beagle/base/common/contant/CodeEnum.java b/src/main/java/com/beagle/base/common/contant/CodeEnum.java index 1ac5d9ec7de32044ab8829e4713f6ef2aa48832c..0ead81ee33b039296200bba46fe31938c892513a 100644 --- a/src/main/java/com/beagle/base/common/contant/CodeEnum.java +++ b/src/main/java/com/beagle/base/common/contant/CodeEnum.java @@ -73,7 +73,7 @@ public enum CodeEnum { UPLOAD_FILE_NAME_ERROR("5000015", "上传文件格式错误"), EXCEPTION_OPEN_SEARCH("5000016", "OPENSEARCH服务异常"), UNAUTHORIZED_OPEN_SEARCH("5000017", "OPENSEARCH未授权"), - CRYPT_CHECK_FAILED("5000018", "内容防篡改验证失败"), + CACHE_VALID_REBUILD_ERROR("5000019", "缓存失效后未找到对应的值"), ; /** diff --git a/src/main/java/com/beagle/base/common/filter/AuthenticationFilter.java b/src/main/java/com/beagle/base/common/filter/AuthenticationFilter.java index 69eb621ec01c34d96ffb7ee4f29b64a841b82895..8372a3707569dbe340939b98273b47e25bcf7f76 100644 --- a/src/main/java/com/beagle/base/common/filter/AuthenticationFilter.java +++ b/src/main/java/com/beagle/base/common/filter/AuthenticationFilter.java @@ -9,7 +9,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson2.JSONObject; -import com.beagle.base.UserVo; +import com.beagle.base.bean.vo.UserVo; import com.beagle.base.common.component.ApplicationContextUtil; import com.beagle.base.common.config.CommonConfig; import com.beagle.base.common.contant.CodeEnum; diff --git a/src/main/java/com/beagle/base/common/listener/ApplicationListener.java b/src/main/java/com/beagle/base/common/listener/ApplicationListener.java new file mode 100644 index 0000000000000000000000000000000000000000..92f3e0cd370e003bc1a8af99c89f0711777cbde7 --- /dev/null +++ b/src/main/java/com/beagle/base/common/listener/ApplicationListener.java @@ -0,0 +1,121 @@ +package com.beagle.base.common.listener; + +import cn.hutool.core.collection.CollUtil; +import com.beagle.base.bean.vo.BasePointVO; +import com.beagle.base.bean.vo.BasePointsPropertiesParam; +import com.beagle.base.bean.vo.BaseSearchParam; +import com.beagle.base.bean.vo.BaseSearchPointCountParam; +import com.beagle.base.common.contant.CacheConstants; +import com.beagle.base.service.BaseService; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.LoadingCache; +import com.rtdb.enums.DataSort; +import com.rtdb.model.FullPoint; +import com.rtdb.model.SearchCondition; +import com.rtdb.model.SearchConditionTotal; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.ApplicationEvent; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.sql.Array; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ForkJoinPool; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +@Slf4j +@Component +public class ApplicationListener implements org.springframework.context.ApplicationListener { + + @Resource + private BaseService baseService; + + + @Resource + @Qualifier(value= "loadingCache") + private LoadingCache cache; + @Override + public void onApplicationEvent(ApplicationReadyEvent event) { + // 初始化golden缓存 + // 调用 base/searchPointCount 接口 + BaseSearchPointCountParam baseSearchPointCountParam = new BaseSearchPointCountParam(); + baseSearchPointCountParam.setSort(DataSort.SORT_BY_ID); + baseSearchPointCountParam.setCondition(new SearchConditionTotal()); + Integer count = baseService.searchPointCount(baseSearchPointCountParam); + if (count == null) { + log.error("调用 base/searchPointCount 接口返回的总数为空,跳过"); + return; + } + // 调用 base/search 接口 + BaseSearchParam baseSearchParam = new BaseSearchParam(); + baseSearchParam.setSort(DataSort.SORT_BY_ID); + baseSearchParam.setCount(count); + baseSearchParam.setCondition(new SearchCondition()); + int [] totalArray = baseService.search(baseSearchParam); + if (totalArray == null) { + log.error(" 调用 base/search 接口 返回的数据为空, 跳过"); + return; + } + // 调用 base/getPointsProperties 接口 + List fullPointList = batchJob(totalArray); + + if (CollUtil.isEmpty(fullPointList)) { + log.error("调用 base/getPointsProperties 接口 返回的数据为空,跳过"); + return; + } + fullPointList.forEach(each-> { + BasePointVO basePointVO = new BasePointVO(); + basePointVO.setId(each.getBasePoint().getId()); + basePointVO.setTag(each.getBasePoint().getTag()); + basePointVO.setType(each.getBasePoint().getType()); + cache.put(CacheConstants.POINT_PREFIX + basePointVO.getId(), basePointVO); + cache.put(CacheConstants.TAG_PREFIX + basePointVO.getTag(), basePointVO.getId()); + }); + } + + private List batchJob(int [] array) { + List>> futures = new ArrayList<>(); + List> batches = new ArrayList<>(); + int batchSize = 1; + for (int start = 0; start < array.length; start += batchSize) { + batches.add(Arrays.stream(array, start, Math.min(start + batchSize, array.length)) + .boxed() + .collect(Collectors.toList())); + } + + + // 拆分任务 + for (int i = 0; i < batches.size(); i ++) { + final int start = i; + // 创建批处理异步任务 + CompletableFuture> future = CompletableFuture.supplyAsync(() -> { + // 处理当前批次的任务 + BasePointsPropertiesParam basePointsPropertiesParam = new BasePointsPropertiesParam(); + basePointsPropertiesParam.setIds(batches.get(start).stream().mapToInt(Integer::intValue).toArray()); + return baseService.getPointsProperties(basePointsPropertiesParam); + }); + + futures.add(future); + } + + // 等待所有任务完成,并整合结果 + CompletableFuture> allTasks = + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) + .thenApply(v -> futures.stream() + .map(CompletableFuture::join) + .flatMap(List::stream) // 等待所有任务并获取结果 + .collect(Collectors.toList())); + + // 获取并处理最终的结果 + List results = allTasks.join(); // 这里会阻塞,直到所有任务完成 + // 处理结果 (例如,打印数量) + System.out.println("Total results processed: " + results.size()); + return results; + } +} diff --git a/src/main/java/com/beagle/base/common/util/BaseUtil.java b/src/main/java/com/beagle/base/common/util/BaseUtil.java index f53774da118ac8f9a5aa1d099952d4bd674963a2..6339d6197d8249a287539cef63ec395e0a5f7cb9 100644 --- a/src/main/java/com/beagle/base/common/util/BaseUtil.java +++ b/src/main/java/com/beagle/base/common/util/BaseUtil.java @@ -1,7 +1,8 @@ package com.beagle.base.common.util; import cn.hutool.core.collection.CollUtil; -import com.beagle.base.BasePointsPropertiesNewParam; +import com.alibaba.fastjson2.JSON; +import com.beagle.base.bean.vo.BasePointsPropertiesNewParam; import com.beagle.base.bean.vo.BasePointsPropertiesParam; import com.beagle.base.bean.vo.BaseSearchParam; import com.beagle.base.bean.vo.BaseSearchPointCountParam; @@ -9,10 +10,11 @@ import com.beagle.base.bean.vo.BaseTableTagNamesParam; import com.rtdb.model.Entity; import com.rtdb.model.FullPoint; import com.rtdb.model.MinPoint; +import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; import java.util.List; - +@Slf4j public class BaseUtil { public static int[] search(BaseSearchParam param) { @@ -24,7 +26,7 @@ public class BaseUtil { } return new int[]{}; }catch (Exception e) { - e.printStackTrace(); + log.error("base 获取search接口异常,请求参数为:{}", JSON.toJSONString(param), e); return new int[]{}; } } @@ -40,7 +42,7 @@ public class BaseUtil { } return res; }catch (Exception e){ - e.printStackTrace(); + log.error("base 获取getPointsProperties接口异常,请求参数为:{}", JSON.toJSONString(param), e); return res; } } @@ -50,7 +52,7 @@ public class BaseUtil { try { res = CommonUtil.getBaseImpl().searchPointCount(param.getCondition(), param.getSort()); }catch (Exception e){ - e.printStackTrace(); + log.error("base 获取searchPointCount接口异常,请求参数为:{}", JSON.toJSONString(param), e); return res; } return res; @@ -61,7 +63,7 @@ public class BaseUtil { try { res = CommonUtil.getBaseImpl().getTableTagNames(param.getIds()); }catch (Exception e){ - e.printStackTrace(); + log.error("base 获取getTableTagNames接口异常,请求参数为:{}", JSON.toJSONString(param), e); res = new String[0]; return res; } @@ -76,7 +78,7 @@ public class BaseUtil { res = pointsProperties.getList(); } }catch (Exception e){ - e.printStackTrace(); + log.error("base 获取getPointsPropertiesTags接口异常,请求参数为:{}", JSON.toJSONString(param), e); return res; } return res; diff --git a/src/main/java/com/beagle/base/common/util/HistorianUtil.java b/src/main/java/com/beagle/base/common/util/HistorianUtil.java index 019d940e63e2fb73da819ae03d2e33e822497b85..6e5a1191d3d29670e8abff7f9f7c0e48be01ae86 100644 --- a/src/main/java/com/beagle/base/common/util/HistorianUtil.java +++ b/src/main/java/com/beagle/base/common/util/HistorianUtil.java @@ -1,15 +1,17 @@ package com.beagle.base.common.util; import cn.hutool.core.collection.CollUtil; +import com.alibaba.fastjson2.JSON; import com.beagle.base.bean.vo.HistorianDoubleArchivedValuesParam; import com.beagle.base.bean.vo.HistorianFloatArchivedValuesFiltParam; import com.beagle.base.bean.vo.HistorianFloatInterpoValuesParam; import com.beagle.base.bean.vo.HistorianFloatIntervalValuesParam; import com.rtdb.model.DoubleData; +import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; import java.util.List; - +@Slf4j public class HistorianUtil { public static List getFloatIntervalValues(HistorianFloatIntervalValuesParam param ) { @@ -21,7 +23,7 @@ public class HistorianUtil { } return new ArrayList<>(); }catch (Exception e) { - e.printStackTrace(); + log.error("base 获取getFloatIntervalValues接口异常,请求参数为:{}", JSON.toJSONString(param), e); return new ArrayList<>(); } } @@ -36,7 +38,7 @@ public class HistorianUtil { } return new ArrayList<>(); }catch (Exception e) { - e.printStackTrace(); + log.error("base 获取getFloatInterpoValues接口异常,请求参数为:{}", JSON.toJSONString(param), e); return new ArrayList<>(); } } @@ -50,7 +52,7 @@ public class HistorianUtil { } return new ArrayList<>(); }catch (Exception e) { - e.printStackTrace(); + log.error("base 获取getFloatArchivedValuesFilt接口异常,请求参数为:{}", JSON.toJSONString(param), e); return new ArrayList<>(); } } @@ -64,7 +66,7 @@ public class HistorianUtil { } return new ArrayList<>(); }catch (Exception e) { - e.printStackTrace(); + log.error("base 获取getDoubleArchivedValues接口异常,请求参数为:{}", JSON.toJSONString(param), e); return new ArrayList<>(); } } diff --git a/src/main/java/com/beagle/base/common/util/SnapshotUtil.java b/src/main/java/com/beagle/base/common/util/SnapshotUtil.java index aca65567fe01cb54d08e0ff10cdde6c12a982711..6b684f307206be6b27249abc64af71d28f38b4cc 100644 --- a/src/main/java/com/beagle/base/common/util/SnapshotUtil.java +++ b/src/main/java/com/beagle/base/common/util/SnapshotUtil.java @@ -1,15 +1,17 @@ package com.beagle.base.common.util; import cn.hutool.core.collection.CollUtil; +import com.alibaba.fastjson2.JSON; import com.beagle.base.bean.vo.SnapshotBlobParam; import com.beagle.base.bean.vo.SnapshotCoorParam; import com.beagle.base.bean.vo.SnapshotDoubleParam; import com.beagle.base.bean.vo.SnapshotIntParam; import com.rtdb.model.*; +import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; import java.util.List; - +@Slf4j public class SnapshotUtil { public static List getDoubleSnapshots(SnapshotDoubleParam param) { @@ -21,7 +23,7 @@ public class SnapshotUtil { } return res; }catch (Exception e) { - e.printStackTrace(); + log.error("base 获取getDoubleSnapshots接口异常,请求参数为:{}", JSON.toJSONString(param), e); return res; } } @@ -35,7 +37,7 @@ public class SnapshotUtil { } return res; }catch (Exception e) { - e.printStackTrace(); + log.error("base 获取getIntSnapshots接口异常,请求参数为:{}", JSON.toJSONString(param), e); return res; } } @@ -49,7 +51,7 @@ public class SnapshotUtil { } return res; }catch (Exception e) { - e.printStackTrace(); + log.error("base 获取getBlobSnapshots接口异常,请求参数为:{}", JSON.toJSONString(param), e); return res; } } @@ -63,7 +65,7 @@ public class SnapshotUtil { } return res; }catch (Exception e) { - e.printStackTrace(); + log.error("base 获取getCoorSnapshots接口异常,请求参数为:{}", JSON.toJSONString(param), e); return res; } } diff --git a/src/main/java/com/beagle/base/controller/BaseController.java b/src/main/java/com/beagle/base/controller/BaseController.java index 36661e04e71bad470a1b62a2461c4cea4ff8667f..30f76b4feb3db7074810450130fe2be13e35ab13 100644 --- a/src/main/java/com/beagle/base/controller/BaseController.java +++ b/src/main/java/com/beagle/base/controller/BaseController.java @@ -1,6 +1,6 @@ package com.beagle.base.controller; -import com.beagle.base.BasePointsPropertiesNewParam; +import com.beagle.base.bean.vo.BasePointsPropertiesNewParam; import com.beagle.base.bean.vo.BasePointsPropertiesParam; import com.beagle.base.bean.vo.BaseSearchParam; import com.beagle.base.bean.vo.BaseSearchPointCountParam; diff --git a/src/main/java/com/beagle/base/controller/CacheController.java b/src/main/java/com/beagle/base/controller/CacheController.java new file mode 100644 index 0000000000000000000000000000000000000000..420dd8528002efadc71335070bed96bb022de7d0 --- /dev/null +++ b/src/main/java/com/beagle/base/controller/CacheController.java @@ -0,0 +1,43 @@ +package com.beagle.base.controller; + +import com.beagle.base.bean.vo.*; +import com.beagle.base.bean.vo.resp.Response; +import com.beagle.base.service.CacheService; +import com.beagle.base.service.HistorianService; +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; +import com.rtdb.model.DoubleData; +import com.rtdb.model.FullPoint; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Map; + +@Api(tags = "缓存接口") +@RestController +@RequestMapping("/v1/cache") +public class CacheController { + @Resource + private CacheService cacheService; + + @ApiOperationSupport(order = 1) + @ApiOperation(value = "从缓存中通过tag获取id") + @PostMapping("/base/byTag") + public Response getBaseIdByTag(@RequestBody @ApiParam(value = "入参") @Validated BaseCacheByTagParam param) { + return new Response<>(cacheService.getBaseIdByTag(param)); + } + + @ApiOperationSupport(order = 2) + @ApiOperation(value = "从缓存中通过id获取point") + @PostMapping("/base/byId") + public Response getBasePointById(@RequestBody @ApiParam(value = "入参") @Validated BaseCacheByIdParam param) { + return new Response<>(cacheService.getBasePointById(param)); + } +} diff --git a/src/main/java/com/beagle/base/service/BaseService.java b/src/main/java/com/beagle/base/service/BaseService.java index 3c6716613eb645055bd4396881ef1cb3753c0c59..f9265f4ef67d0aabc8f64d6979067b5801f09e01 100644 --- a/src/main/java/com/beagle/base/service/BaseService.java +++ b/src/main/java/com/beagle/base/service/BaseService.java @@ -1,6 +1,6 @@ package com.beagle.base.service; -import com.beagle.base.BasePointsPropertiesNewParam; +import com.beagle.base.bean.vo.BasePointsPropertiesNewParam; import com.beagle.base.bean.vo.BasePointsPropertiesParam; import com.beagle.base.bean.vo.BaseSearchParam; import com.beagle.base.bean.vo.BaseSearchPointCountParam; diff --git a/src/main/java/com/beagle/base/service/CacheService.java b/src/main/java/com/beagle/base/service/CacheService.java new file mode 100644 index 0000000000000000000000000000000000000000..852efcea84785e9b780de43e8a15c6d12ef09c1e --- /dev/null +++ b/src/main/java/com/beagle/base/service/CacheService.java @@ -0,0 +1,13 @@ +package com.beagle.base.service; + +import com.beagle.base.bean.vo.BaseCacheByIdParam; +import com.beagle.base.bean.vo.BaseCacheByTagParam; +import com.beagle.base.bean.vo.BasePointVO; +import com.rtdb.model.FullPoint; + +public interface CacheService { + + Integer getBaseIdByTag(BaseCacheByTagParam param); + + BasePointVO getBasePointById(BaseCacheByIdParam param); +} diff --git a/src/main/java/com/beagle/base/service/impl/BaseServiceImpl.java b/src/main/java/com/beagle/base/service/impl/BaseServiceImpl.java index afeae0b9e9e0659c2ee0c2d7fce4ba43a62ad8d5..43d5acbaf813063e58a02c377845fabe0b10c084 100644 --- a/src/main/java/com/beagle/base/service/impl/BaseServiceImpl.java +++ b/src/main/java/com/beagle/base/service/impl/BaseServiceImpl.java @@ -1,7 +1,7 @@ package com.beagle.base.service.impl; -import com.beagle.base.BasePointsPropertiesNewParam; +import com.beagle.base.bean.vo.BasePointsPropertiesNewParam; import com.beagle.base.bean.vo.BasePointsPropertiesParam; import com.beagle.base.bean.vo.BaseSearchParam; import com.beagle.base.bean.vo.BaseSearchPointCountParam; diff --git a/src/main/java/com/beagle/base/service/impl/CacheServiceImpl.java b/src/main/java/com/beagle/base/service/impl/CacheServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..d6a58fb30fccd97af19fdfe3f3a8a09fc7b8a61f --- /dev/null +++ b/src/main/java/com/beagle/base/service/impl/CacheServiceImpl.java @@ -0,0 +1,36 @@ +package com.beagle.base.service.impl; + +import cn.hutool.core.util.NumberUtil; +import com.beagle.base.bean.vo.BaseCacheByIdParam; +import com.beagle.base.bean.vo.BaseCacheByTagParam; +import com.beagle.base.bean.vo.BasePointVO; +import com.beagle.base.common.contant.CacheConstants; +import com.beagle.base.service.CacheService; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.LoadingCache; +import com.rtdb.model.FullPoint; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +@Service +public class CacheServiceImpl implements CacheService { + + @Resource + @Qualifier(value= "loadingCache") + private LoadingCache cache; + + @Override + public Integer getBaseIdByTag(BaseCacheByTagParam param) { + // 从缓存中获取 + return NumberUtil.parseInt(cache.get(CacheConstants.TAG_PREFIX + param.getTag()) + ""); + } + + @Override + public BasePointVO getBasePointById(BaseCacheByIdParam param) { + // 从缓存中获取 + // 从缓存中获取 + return (BasePointVO) cache.get(CacheConstants.POINT_PREFIX + param.getId()); + } +} diff --git a/src/main/test/Test.java b/src/main/test/Test.java index 653ffe08e8465688e14b809a3e0f7b764b21908d..9c92377f817335ba0e41119806141a2c48bc804d 100644 --- a/src/main/test/Test.java +++ b/src/main/test/Test.java @@ -1,5 +1,5 @@ import com.alibaba.fastjson2.JSON; -import com.beagle.base.BasePointsPropertiesNewParam; +import com.beagle.base.bean.vo.BasePointsPropertiesNewParam; import com.beagle.base.bean.vo.BaseSearchPointCountParam; import com.beagle.base.bean.vo.resp.Response; import com.rtdb.api.enums.SearchMask;