Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
spring-boot-starter-dsp
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
shenjiaqing
spring-boot-starter-dsp
Commits
3143f352
Commit
3143f352
authored
Aug 02, 2021
by
shenjiaqing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
提交代码
parent
e7219e3d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
16 deletions
+40
-16
CacheConfig.java
...com/duiba/spring/boot/starter/dsp/warmup/CacheConfig.java
+40
-16
No files found.
spring-boot-starter-dsp-warmup/src/main/java/cn/com/duiba/spring/boot/starter/dsp/warmup/CacheConfig.java
View file @
3143f352
...
...
@@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import
com.fasterxml.jackson.databind.type.MapType
;
import
com.fasterxml.jackson.databind.type.TypeFactory
;
import
com.github.benmanes.caffeine.cache.AsyncCache
;
import
com.google.common.cache.Cache
;
import
com.netflix.appinfo.InstanceInfo
;
import
com.netflix.discovery.EurekaClient
;
import
org.apache.commons.collections.CollectionUtils
;
...
...
@@ -80,6 +81,7 @@ public class CacheConfig {
}
for
(
Field
field
:
fields
)
{
boolean
isGuava
=
Cache
.
class
.
isAssignableFrom
(
field
.
getType
());
boolean
isCaffeine
=
AsyncCache
.
class
.
isAssignableFrom
(
field
.
getType
());
if
(!
isCaffeine
)
{
...
...
@@ -89,13 +91,17 @@ public class CacheConfig {
String
keyName
=
String
.
format
(
"%s-%s"
,
beanName
,
field
.
getName
());
try
{
Object
cache
=
field
.
get
(
bean
);
if
(
cache
instanceof
Cache
)
{
ConcurrentMap
<
Object
,
Object
>
concurrentMap
=
((
Cache
)
cache
).
asMap
();
CacheManager
.
saveCache
(
keyName
,
concurrentMap
);
logger
.
info
(
"beanName = {}, cache = {}, isGuava = {}, bean = {}"
,
keyName
,
cache
,
isGuava
,
bean
);
}
if
(!(
cache
instanceof
AsyncCache
))
{
continue
;
if
(
cache
instanceof
AsyncCache
)
{
ConcurrentMap
<
Object
,
Object
>
concurrentMap
=
((
AsyncCache
)
cache
).
synchronous
().
asMap
();
CacheManager
.
saveCache
(
keyName
,
concurrentMap
);
logger
.
info
(
"beanName = {}, cache = {}, isCaffeine = {}, bean = {}"
,
keyName
,
cache
,
isCaffeine
,
bean
);
}
ConcurrentMap
<
Object
,
Object
>
concurrentMap
=
((
AsyncCache
)
cache
).
synchronous
().
asMap
();
CacheManager
.
saveCache
(
keyName
,
concurrentMap
);
logger
.
info
(
"beanName = {}, cache = {}, isCaffeine = {}, bean = {}"
,
keyName
,
cache
,
isCaffeine
,
bean
);
}
catch
(
IllegalAccessException
e
)
{
//ignore
}
finally
{
...
...
@@ -146,12 +152,10 @@ public class CacheConfig {
field
.
setAccessible
(
true
);
Object
obj
=
field
.
get
(
bean
);
if
(!(
obj
instanceof
AsyncCache
))
{
if
(!(
obj
instanceof
Cache
)
||
!(
obj
instanceof
AsyncCache
))
{
continue
;
}
AsyncCache
asyncCache
=
(
AsyncCache
)
obj
;
// 通过MapType解析map结构
ParameterizedType
genericType
=
(
ParameterizedType
)
field
.
getGenericType
();
Type
[]
actualTypeArguments
=
genericType
.
getActualTypeArguments
();
...
...
@@ -159,15 +163,33 @@ public class CacheConfig {
objectMapper
.
configure
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
);
Map
convertMap
=
objectMapper
.
convertValue
(
v
,
mapType
);
convertMap
.
forEach
((
k1
,
v1
)
->
{
// 通过JavaType解析map中的value结构
ParameterizedType
genericType1
=
(
ParameterizedType
)
actualTypeArguments
[
1
];
Type
[]
actualTypeArguments1
=
genericType1
.
getActualTypeArguments
();
JavaType
javaType
=
typeFactory
.
constructType
(
actualTypeArguments1
[
0
]);
asyncCache
.
put
(
k1
,
CompletableFuture
.
supplyAsync
(()
->
Optional
.
ofNullable
(
objectMapper
.
convertValue
(
v1
,
javaType
))));
});
if
(
obj
instanceof
Cache
)
{
Cache
cache
=
(
Cache
)
obj
;
convertMap
.
forEach
((
k1
,
v1
)
->
{
// 通过JavaType解析map中的value结构
ParameterizedType
genericType1
=
(
ParameterizedType
)
actualTypeArguments
[
1
];
Type
[]
actualTypeArguments1
=
genericType1
.
getActualTypeArguments
();
JavaType
javaType
=
typeFactory
.
constructType
(
actualTypeArguments1
[
0
]);
cache
.
put
(
k1
,
Optional
.
ofNullable
(
objectMapper
.
convertValue
(
v1
,
javaType
)));
});
logger
.
info
(
"Guava Cache key = {} ; Guava Cache Size = {}"
,
k
,
cache
.
asMap
().
size
());
}
if
(
obj
instanceof
AsyncCache
)
{
AsyncCache
asyncCache
=
(
AsyncCache
)
obj
;
convertMap
.
forEach
((
k1
,
v1
)
->
{
// 通过JavaType解析map中的value结构
ParameterizedType
genericType1
=
(
ParameterizedType
)
actualTypeArguments
[
1
];
Type
[]
actualTypeArguments1
=
genericType1
.
getActualTypeArguments
();
JavaType
javaType
=
typeFactory
.
constructType
(
actualTypeArguments1
[
0
]);
asyncCache
.
put
(
k1
,
CompletableFuture
.
supplyAsync
(()
->
Optional
.
ofNullable
(
objectMapper
.
convertValue
(
v1
,
javaType
))));
});
logger
.
info
(
"Caffeine Cache key = {} ; Caffeine Cache Size = {}"
,
k
,
asyncCache
.
synchronous
().
asMap
().
size
());
}
logger
.
info
(
"Caffeine Cache key = {} ; Caffeine Cache Size = {}"
,
k
,
asyncCache
.
synchronous
().
asMap
().
size
());
field
.
setAccessible
(
false
);
}
catch
(
Exception
e
)
{
logger
.
warn
(
"this bean : {} , no have method : {}, Lead to it cannot be deserialized , cause: {}"
,
split
[
0
],
field
.
getName
()
+
"Serialize"
,
e
);
...
...
@@ -183,4 +205,6 @@ public class CacheConfig {
}
}
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