Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sdk-gloden-api
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
haidian
sdk-gloden-api
Commits
b5e90abc
Commit
b5e90abc
authored
Aug 28, 2024
by
李振振
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增缓存接口
parent
ced377e6
Changes
23
Show whitespace changes
Inline
Side-by-side
Showing
23 changed files
with
473 additions
and
43 deletions
+473
-43
.beagle/dockerfile
.beagle/dockerfile
+2
-1
pom.xml
pom.xml
+18
-15
src/main/java/com/beagle/base/BaseApplication.java
src/main/java/com/beagle/base/BaseApplication.java
+3
-2
src/main/java/com/beagle/base/bean/vo/BaseCacheByIdParam.java
...main/java/com/beagle/base/bean/vo/BaseCacheByIdParam.java
+11
-0
src/main/java/com/beagle/base/bean/vo/BaseCacheByTagParam.java
...ain/java/com/beagle/base/bean/vo/BaseCacheByTagParam.java
+11
-0
src/main/java/com/beagle/base/bean/vo/BasePointVO.java
src/main/java/com/beagle/base/bean/vo/BasePointVO.java
+11
-0
src/main/java/com/beagle/base/bean/vo/BasePointsPropertiesNewParam.java
...com/beagle/base/bean/vo/BasePointsPropertiesNewParam.java
+1
-1
src/main/java/com/beagle/base/bean/vo/UserVo.java
src/main/java/com/beagle/base/bean/vo/UserVo.java
+1
-1
src/main/java/com/beagle/base/common/cache/caffeine/CacheConfig.java
...va/com/beagle/base/common/cache/caffeine/CacheConfig.java
+167
-0
src/main/java/com/beagle/base/common/contant/CacheConstants.java
...n/java/com/beagle/base/common/contant/CacheConstants.java
+6
-0
src/main/java/com/beagle/base/common/contant/CodeEnum.java
src/main/java/com/beagle/base/common/contant/CodeEnum.java
+1
-1
src/main/java/com/beagle/base/common/filter/AuthenticationFilter.java
...a/com/beagle/base/common/filter/AuthenticationFilter.java
+1
-1
src/main/java/com/beagle/base/common/listener/ApplicationListener.java
.../com/beagle/base/common/listener/ApplicationListener.java
+121
-0
src/main/java/com/beagle/base/common/util/BaseUtil.java
src/main/java/com/beagle/base/common/util/BaseUtil.java
+9
-7
src/main/java/com/beagle/base/common/util/HistorianUtil.java
src/main/java/com/beagle/base/common/util/HistorianUtil.java
+7
-5
src/main/java/com/beagle/base/common/util/SnapshotUtil.java
src/main/java/com/beagle/base/common/util/SnapshotUtil.java
+7
-5
src/main/java/com/beagle/base/controller/BaseController.java
src/main/java/com/beagle/base/controller/BaseController.java
+1
-1
src/main/java/com/beagle/base/controller/CacheController.java
...main/java/com/beagle/base/controller/CacheController.java
+43
-0
src/main/java/com/beagle/base/service/BaseService.java
src/main/java/com/beagle/base/service/BaseService.java
+1
-1
src/main/java/com/beagle/base/service/CacheService.java
src/main/java/com/beagle/base/service/CacheService.java
+13
-0
src/main/java/com/beagle/base/service/impl/BaseServiceImpl.java
...in/java/com/beagle/base/service/impl/BaseServiceImpl.java
+1
-1
src/main/java/com/beagle/base/service/impl/CacheServiceImpl.java
...n/java/com/beagle/base/service/impl/CacheServiceImpl.java
+36
-0
src/main/test/Test.java
src/main/test/Test.java
+1
-1
No files found.
.beagle/dockerfile
View file @
b5e90abc
...
...
@@ -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"]
pom.xml
View file @
b5e90abc
...
...
@@ -8,33 +8,27 @@
<version>
2.7.0
</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>
com.beagle.sd
t.kangdu
n
</groupId>
<artifactId>
sd
t-kangdu
n-api
</artifactId>
<groupId>
com.beagle.sd
k.golde
n
</groupId>
<artifactId>
sd
k-golde
n-api
</artifactId>
<version>
1.0.0
</version>
<packaging>
jar
</packaging>
<name>
sd
t-kangdu
n-api
</name>
<name>
sd
k-golde
n-api
</name>
<properties>
<java.version>
8
</java.version>
<maven.compiler.source>
8
</maven.compiler.source>
<maven.compiler.target>
8
</maven.compiler.target>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<alibaba.fastjson.version>
2.0.40
</alibaba.fastjson.version>
<knife4j.version>
3.0.3
</knife4j.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
<groupId>
net.java.dev.jna
</groupId>
<artifactId>
jna
</artifactId>
<version>
4.5.2
</version>
<groupId>
rtdb
</groupId>
<artifactId>
rtdb-java-sdk
</artifactId>
<version>
3.0.48
</version>
<systemPath>
${pom.basedir}/lib/rtdb-java-sdk-3.0.48.jar
</systemPath>
<scope>
system
</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<!-- <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<systemPath>${pom.basedir}/lib/log4j-1.2.17.jar</systemPath>
<scope>system</scope>
</dependency>-->
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-log4j2
</artifactId>
...
...
@@ -115,6 +109,15 @@
<groupId>
org.springframework
</groupId>
<artifactId>
spring-test
</artifactId>
</dependency>
<dependency>
<groupId>
com.github.ben-manes.caffeine
</groupId>
<artifactId>
caffeine
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-cache
</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
...
...
src/main/java/com/beagle/base/BaseApplication.java
View file @
b5e90abc
...
...
@@ -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
})
@Enable
Schedul
ing
@SpringBootApplication
@Enable
Cach
ing
public
class
BaseApplication
{
public
static
void
main
(
String
[]
args
)
{
...
...
src/main/java/com/beagle/base/bean/vo/BaseCacheByIdParam.java
0 → 100644
View file @
b5e90abc
package
com.beagle.base.bean.vo
;
import
lombok.Data
;
import
javax.validation.constraints.NotNull
;
@Data
public
class
BaseCacheByIdParam
{
@NotNull
(
message
=
"id不能为空"
)
private
Integer
id
;
}
src/main/java/com/beagle/base/bean/vo/BaseCacheByTagParam.java
0 → 100644
View file @
b5e90abc
package
com.beagle.base.bean.vo
;
import
lombok.Data
;
import
javax.validation.constraints.NotNull
;
@Data
public
class
BaseCacheByTagParam
{
@NotNull
(
message
=
"tag不能为空"
)
private
String
tag
;
}
src/main/java/com/beagle/base/bean/vo/BasePointVO.java
0 → 100644
View file @
b5e90abc
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
;
}
src/main/java/com/beagle/base/BasePointsPropertiesNewParam.java
→
src/main/java/com/beagle/base/
bean/vo/
BasePointsPropertiesNewParam.java
View file @
b5e90abc
package
com.beagle.base
;
package
com.beagle.base
.bean.vo
;
import
lombok.Data
;
...
...
src/main/java/com/beagle/base/UserVo.java
→
src/main/java/com/beagle/base/
bean/vo/
UserVo.java
View file @
b5e90abc
package
com.beagle.base
;
package
com.beagle.base
.bean.vo
;
import
lombok.Data
;
...
...
src/main/java/com/beagle/base/common/cache/caffeine/CacheConfig.java
0 → 100644
View file @
b5e90abc
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
<
Object
,
Object
>
cacheLoader
()
{
return
new
CacheLoader
<
Object
,
Object
>()
{
@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
<
MinPoint
>
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
<
FullPoint
>
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
<
MinPoint
>
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
<
FullPoint
>
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
<
Object
,
Object
>
loadingCache
()
{
return
caffeine
().
build
(
cacheLoader
());
}
@Bean
(
name
=
"caffeineCache"
)
public
Cache
<
Object
,
Object
>
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
<
Object
,
Object
>
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
<
Object
,
Object
>
noRefreshcaffeine
()
{
return
Caffeine
.
newBuilder
()
// 设置过期重新刷新时间,超过该时间后,接口调用会自动去调用reload方法重新获取数据
.
maximumSize
(
1000
)
.
initialCapacity
(
100
)
.
softValues
()
// 设置过期时间,超过后,缓存的值会被删除,下次调用后,因为缓存中没有值了,这时候会调用load方法,重新获取数据
.
expireAfterWrite
(
10L
,
TimeUnit
.
SECONDS
);
}
}
src/main/java/com/beagle/base/common/contant/CacheConstants.java
0 → 100644
View file @
b5e90abc
package
com.beagle.base.common.contant
;
public
class
CacheConstants
{
public
static
final
String
TAG_PREFIX
=
"tag_"
;
public
static
final
String
POINT_PREFIX
=
"point_"
;
}
src/main/java/com/beagle/base/common/contant/CodeEnum.java
View file @
b5e90abc
...
...
@@ -73,7 +73,7 @@ public enum CodeEnum {
UPLOAD_FILE_NAME_ERROR
(
"5000015"
,
"上传文件格式错误"
),
EXCEPTION_OPEN_SEARCH
(
"5000016"
,
"OPENSEARCH服务异常"
),
UNAUTHORIZED_OPEN_SEARCH
(
"5000017"
,
"OPENSEARCH未授权"
),
C
RYPT_CHECK_FAILED
(
"5000018"
,
"内容防篡改验证失败
"
),
C
ACHE_VALID_REBUILD_ERROR
(
"5000019"
,
"缓存失效后未找到对应的值
"
),
;
/**
...
...
src/main/java/com/beagle/base/common/filter/AuthenticationFilter.java
View file @
b5e90abc
...
...
@@ -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
;
...
...
src/main/java/com/beagle/base/common/listener/ApplicationListener.java
0 → 100644
View file @
b5e90abc
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
<
ApplicationReadyEvent
>
{
@Resource
private
BaseService
baseService
;
@Resource
@Qualifier
(
value
=
"loadingCache"
)
private
LoadingCache
<
Object
,
Object
>
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
<
FullPoint
>
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
<
FullPoint
>
batchJob
(
int
[]
array
)
{
List
<
CompletableFuture
<
List
<
FullPoint
>>>
futures
=
new
ArrayList
<>();
List
<
List
<
Integer
>>
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
<
List
<
FullPoint
>>
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
<
List
<
FullPoint
>>
allTasks
=
CompletableFuture
.
allOf
(
futures
.
toArray
(
new
CompletableFuture
[
0
]))
.
thenApply
(
v
->
futures
.
stream
()
.
map
(
CompletableFuture:
:
join
)
.
flatMap
(
List:
:
stream
)
// 等待所有任务并获取结果
.
collect
(
Collectors
.
toList
()));
// 获取并处理最终的结果
List
<
FullPoint
>
results
=
allTasks
.
join
();
// 这里会阻塞,直到所有任务完成
// 处理结果 (例如,打印数量)
System
.
out
.
println
(
"Total results processed: "
+
results
.
size
());
return
results
;
}
}
src/main/java/com/beagle/base/common/util/BaseUtil.java
View file @
b5e90abc
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
;
...
...
src/main/java/com/beagle/base/common/util/HistorianUtil.java
View file @
b5e90abc
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
<
DoubleData
>
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
<>();
}
}
...
...
src/main/java/com/beagle/base/common/util/SnapshotUtil.java
View file @
b5e90abc
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
<
DoubleData
>
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
;
}
}
...
...
src/main/java/com/beagle/base/controller/BaseController.java
View file @
b5e90abc
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
;
...
...
src/main/java/com/beagle/base/controller/CacheController.java
0 → 100644
View file @
b5e90abc
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
<
Integer
>
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
<
BasePointVO
>
getBasePointById
(
@RequestBody
@ApiParam
(
value
=
"入参"
)
@Validated
BaseCacheByIdParam
param
)
{
return
new
Response
<>(
cacheService
.
getBasePointById
(
param
));
}
}
src/main/java/com/beagle/base/service/BaseService.java
View file @
b5e90abc
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
;
...
...
src/main/java/com/beagle/base/service/CacheService.java
0 → 100644
View file @
b5e90abc
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
);
}
src/main/java/com/beagle/base/service/impl/BaseServiceImpl.java
View file @
b5e90abc
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
;
...
...
src/main/java/com/beagle/base/service/impl/CacheServiceImpl.java
0 → 100644
View file @
b5e90abc
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
<
Object
,
Object
>
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
());
}
}
src/main/test/Test.java
View file @
b5e90abc
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
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment