Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
iBiz4j Spring R7
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
iBiz-R7后台标准模板
iBiz4j Spring R7
提交
2c54e373
提交
2c54e373
编写于
6月 01, 2020
作者:
zhouweidong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
缓存整改
上级
79c11190
变更
15
显示空白字符变更
内嵌
并排
正在显示
15 个修改的文件
包含
283 行增加
和
446 行删除
+283
-446
CaffeineCacheConfig.java.ftl
...ava/%SYS_PKGPATH%/util/cache/CaffeineCacheConfig.java.ftl
+11
-12
RedisCacheConfig.java.ftl
...n/java/%SYS_PKGPATH%/util/cache/RedisCacheConfig.java.ftl
+65
-57
CusRedisCache.java.ftl
...ava/%SYS_PKGPATH%/util/cache/cache/CusRedisCache.java.ftl
+0
-1
LayeringCache.java.ftl
...ava/%SYS_PKGPATH%/util/cache/cache/LayeringCache.java.ftl
+45
-46
CaffeineCacheManager.java.ftl
...TH%/util/cache/cacheManager/CaffeineCacheManager.java.ftl
+25
-44
LayeringCacheManager.java.ftl
...TH%/util/cache/cacheManager/LayeringCacheManager.java.ftl
+42
-42
RedisMessageListener.java.ftl
...KGPATH%/util/cache/listener/RedisMessageListener.java.ftl
+18
-22
CustomJacksonSerializer.java.ftl
...KGPATH%/util/cache/redis/CustomJacksonSerializer.java.ftl
+28
-0
CustomizedRedisCache.java.ftl
...S_PKGPATH%/util/cache/redis/CustomizedRedisCache.java.ftl
+4
-7
FastJsonRedisSerializer.java.ftl
...KGPATH%/util/cache/redis/FastJsonRedisSerializer.java.ftl
+35
-41
KryoRedisSerializer.java.ftl
...YS_PKGPATH%/util/cache/redis/KryoRedisSerializer.java.ftl
+1
-67
RedisConfig.java.ftl
.../java/%SYS_PKGPATH%/util/cache/redis/RedisConfig.java.ftl
+1
-61
StringRedisSerializer.java.ftl
..._PKGPATH%/util/cache/redis/StringRedisSerializer.java.ftl
+1
-39
RedisChannelTopic.java.ftl
.../java/%SYS_PKGPATH%/util/enums/RedisChannelTopic.java.ftl
+2
-2
AuthenticationUser.java.ftl
...a/%SYS_PKGPATH%/util/security/AuthenticationUser.java.ftl
+5
-5
未找到文件。
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/CaffeineCacheConfig.java.ftl
浏览文件 @
2c54e373
...
...
@@ -4,8 +4,6 @@ TARGET=PSSYSTEM
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
;
import
com
.
github
.
benmanes
.
caffeine
.
cache
.
CaffeineSpec
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
cacheManager
.
CaffeineCacheManager
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Autowired
;
import
org
.
springframework
.
boot
.
autoconfigure
.
cache
.
CacheProperties
;
import
org
.
springframework
.
boot
.
autoconfigure
.
condition
.
ConditionalOnExpression
;
import
org
.
springframework
.
boot
.
context
.
properties
.
EnableConfigurationProperties
;
...
...
@@ -15,6 +13,7 @@ import org.springframework.context.annotation.Bean;
import
org
.
springframework
.
context
.
annotation
.
Configuration
;
import
org
.
springframework
.
context
.
annotation
.
Primary
;
import
org
.
springframework
.
util
.
StringUtils
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
cacheManager
.
CaffeineCacheManager
;
/**
*
Caffeine
缓存配置类
...
...
@@ -25,19 +24,19 @@ import org.springframework.util.StringUtils;
@
ConditionalOnExpression
(
"'${r'${ibiz.cacheLevel:None}'}'.equals('L1')"
)
public
class
CaffeineCacheConfig
{
@
Autowired
private
CacheProperties
cacheProperties
;
@
Autowired
private
CaffeineCacheManager
caffeineCacheManager
;
/**
*
Caffeine
配置:设置过期时间
*
@
return
*/
@
Bean
@
Primary
public
CacheManager
cacheManager
()
{
String
specification
=
cacheProperties
.
getCaffeine
().
getSpec
();
if
(
StringUtils
.
hasText
(
specification
))
{
caffeineCacheManager
.
setCaffeineSpec
(
CaffeineSpec
.
parse
(
specification
));
public
CacheManager
cacheManager
(
CacheProperties
cacheProperties
)
{
CaffeineCacheManager
caffeineCacheManager
=
new
CaffeineCacheManager
();
String
strCacheConfig
=
cacheProperties
.
getCaffeine
().
getSpec
();
if
(
StringUtils
.
hasText
(
strCacheConfig
))
{
caffeineCacheManager
.
setCaffeineCacheConfig
(
CaffeineSpec
.
parse
(
strCacheConfig
));
}
return
caffeineCacheManager
;
}
}
\ No newline at end of file
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/RedisCacheConfig.java.ftl
浏览文件 @
2c54e373
...
...
@@ -3,16 +3,11 @@ TARGET=PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
;
import
com
.
alibaba
.
fastjson
.
parser
.
ParserConfig
;
import
com
.
fasterxml
.
jackson
.
annotation
.
JsonAutoDetect
;
import
com
.
fasterxml
.
jackson
.
annotation
.
PropertyAccessor
;
import
com
.
fasterxml
.
jackson
.
databind
.
ObjectMapper
;
import
com
.
github
.
benmanes
.
caffeine
.
cache
.
CaffeineSpec
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
cacheManager
.
LayeringCacheManager
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
redis
.
KryoRedisSerializer
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
redis
.
StringRedisSerializer
;
import
${
pub
.
getPKGCodeName
()}.
util
.
enums
.
RedisChannelTopic
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Autowired
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Value
;
import
org
.
springframework
.
boot
.
autoconfigure
.
cache
.
CacheProperties
;
import
org
.
springframework
.
boot
.
autoconfigure
.
condition
.
ConditionalOnExpression
;
import
org
.
springframework
.
boot
.
context
.
properties
.
EnableConfigurationProperties
;
...
...
@@ -22,14 +17,19 @@ import org.springframework.context.annotation.Bean;
import
org
.
springframework
.
context
.
annotation
.
Configuration
;
import
org
.
springframework
.
context
.
annotation
.
Primary
;
import
org
.
springframework
.
data
.
redis
.
cache
.
RedisCacheConfiguration
;
import
org
.
springframework
.
data
.
redis
.
cache
.
RedisCacheManager
;
import
org
.
springframework
.
data
.
redis
.
cache
.
RedisCacheWriter
;
import
org
.
springframework
.
data
.
redis
.
connection
.
RedisConnectionFactory
;
import
org
.
springframework
.
data
.
redis
.
core
.
RedisTemplate
;
import
org
.
springframework
.
data
.
redis
.
listener
.
RedisMessageListenerContainer
;
import
org
.
springframework
.
data
.
redis
.
listener
.
adapter
.
MessageListenerAdapter
;
import
org
.
springframework
.
data
.
redis
.
serializer
.
Jackson2JsonRedisSerializer
;
import
org
.
springframework
.
data
.
redis
.
serializer
.
RedisSerializationContext
;
import
org
.
springframework
.
data
.
redis
.
serializer
.
StringRedisSerializer
;
import
org
.
springframework
.
util
.
StringUtils
;
import
java
.
time
.
Duration
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
cacheManager
.
LayeringCacheManager
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
listener
.
RedisMessageListener
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
redis
.
CustomJacksonSerializer
;
import
${
pub
.
getPKGCodeName
()}.
util
.
enums
.
RedisChannelTopic
;
/**
*
缓存配置类
...
...
@@ -42,83 +42,91 @@ import org.springframework.util.StringUtils;
@
ConditionalOnExpression
(
"'${r'${ibiz.cacheLevel:None}'}'.equals('L2')"
)
public
class
RedisCacheConfig
{
@
Autowired
private
RedisCacheWriter
redisCacheWriter
;
@
Autowired
private
RedisCacheConfiguration
configuration
;
@
Autowired
LayeringCacheManager
layeringCacheManager
;
@
Autowired
private
CacheProperties
cacheProperties
;
@
Bean
public
RedisCacheManager
redisCacheManager
(
RedisConnectionFactory
connectionFactory
)
{
return
RedisCacheManager
.
create
(
connectionFactory
);
}
@
Value
(
"${r'${spring.cache.redis.time-to-live:3600}'}"
)
private
long
timeToLive
;
/**
*
分层缓存管理器
*
@
param
redisConnectionFactory
*
@
param
cacheProperties
*
@
return
*/
@
Bean
public
RedisCacheWriter
redisCacheWriter
(
RedisConnectionFactory
connectionFactory
){
RedisCacheWriter
redisCacheWriter
=
RedisCacheWriter
.
nonLockingRedisCacheWriter
(
connectionFactory
);
return
redisCacheWriter
;
@
Primary
public
CacheManager
cacheManager
(
RedisConnectionFactory
redisConnectionFactory
,
CacheProperties
cacheProperties
){
LayeringCacheManager
layeringCacheManager
=
new
LayeringCacheManager
(
redisCacheWriter
(
redisConnectionFactory
),
redisCacheConfiguration
(),
redisTemplate
(
redisConnectionFactory
));
String
strCacheConfig
=
cacheProperties
.
getCaffeine
().
getSpec
();
if
(
StringUtils
.
hasText
(
strCacheConfig
))
{
layeringCacheManager
.
setCaffeineCacheConfig
(
CaffeineSpec
.
parse
(
strCacheConfig
));
}
return
layeringCacheManager
;
}
/**
*
重写
Redis
序列化方式,使用
Json
方式
:
*
当我们的数据存储到
Redis
的时候,我们的键(
key
)和值(
value
)都是通过
Spring
提供的
Serializer
序列化到数据库的。
RedisTemplate
默认使用的是
JdkSerializationRedisSerializer
,
StringRedisTemplate
默认使用的是
StringRedisSerializer
。
*
Spring
Data
JPA
为我们提供了下面的
Serializer
:
*
GenericToStringSerializer
、
Jackson2JsonRedisSerializer
、
JacksonJsonRedisSerializer
、
JdkSerializationRedisSerializer
、
OxmSerializer
、
StringRedisSerializer
。
*
在此我们将自己配置
RedisTemplate
并定义
Serializer
。
*
*
@
param
redisConnectionFactory
*
redis
配置:设置过期时间及序列化方式
*
@
return
*/
@
Bean
public
RedisTemplate
<
String
,
Object
>
redisTemplate
(
RedisConnectionFactory
redisConnectionFactory
)
{
RedisTemplate
<
String
,
Object
>
redisTemplate
=
new
RedisTemplate
<>();
redisTemplate
.
setConnectionFactory
(
redisConnectionFactory
);
Jackson2JsonRedisSerializer
<
Object
>
jackson2JsonRedisSerializer
=
new
Jackson2JsonRedisSerializer
<
Object
>(
Object
.
class
);
public
RedisCacheConfiguration
redisCacheConfiguration
(){
CustomJacksonSerializer
jackson2JsonRedisSerializer
=
new
CustomJacksonSerializer
(
Object
.
class
);
ObjectMapper
om
=
new
ObjectMapper
();
om
.
setVisibility
(
PropertyAccessor
.
ALL
,
JsonAutoDetect
.
Visibility
.
ANY
);
om
.
enableDefaultTyping
(
ObjectMapper
.
DefaultTyping
.
NON_FINAL
);
jackson2JsonRedisSerializer
.
setObjectMapper
(
om
);
ParserConfig
.
getGlobalInstance
().
setAutoTypeSupport
(
true
);
KryoRedisSerializer
<
Object
>
kryoRedisSerializer
=
new
KryoRedisSerializer
<>(
Object
.
class
);
redisTemplate
.
setValueSerializer
(
kryoRedisSerializer
);//
设置值(
value
)的序列化采用
KryoRedisSerializer
。
redisTemplate
.
setHashValueSerializer
(
kryoRedisSerializer
);
redisTemplate
.
setKeySerializer
(
new
StringRedisSerializer
());//
设置键(
key
)的序列化采用
StringRedisSerializer
。
redisTemplate
.
setHashKeySerializer
(
new
StringRedisSerializer
());
redisTemplate
.
afterPropertiesSet
();
return
redisTemplate
;
RedisCacheConfiguration
configuration
=
RedisCacheConfiguration
.
defaultCacheConfig
()
.
entryTtl
(
Duration
.
ofSeconds
(
timeToLive
))
.
serializeKeysWith
(
RedisSerializationContext
.
SerializationPair
.
fromSerializer
(
new
StringRedisSerializer
()))
.
serializeValuesWith
(
RedisSerializationContext
.
SerializationPair
.
fromSerializer
(
jackson2JsonRedisSerializer
))
.
disableCachingNullValues
();
return
configuration
;
}
/**
*
创建
redis
缓存
*
@
param
connectionFactory
*
@
return
*/
@
Bean
@
Primary
public
CacheManager
cacheManager
()
{
setCaffeineCacheConfig
(
layeringCacheManager
);//
Caffeine
缓存设置
layeringCacheManager
.
setRedisCacheWriter
(
redisCacheWriter
);
layeringCacheManager
.
setRedisConfiguration
(
configuration
);
return
layeringCacheManager
;
public
RedisCacheWriter
redisCacheWriter
(
RedisConnectionFactory
connectionFactory
){
RedisCacheWriter
redisCacheWriter
=
RedisCacheWriter
.
nonLockingRedisCacheWriter
(
connectionFactory
);
return
redisCacheWriter
;
}
private
void
setCaffeineCacheConfig
(
LayeringCacheManager
layeringCacheManager
)
{
String
specification
=
cacheProperties
.
getCaffeine
().
getSpec
();
if
(
StringUtils
.
hasText
(
specification
))
{
layeringCacheManager
.
setCaffeineSpec
(
CaffeineSpec
.
parse
(
specification
));
}
/**
*
发送
redis
广播
*
@
param
factory
*
@
return
*/
@
Bean
public
RedisTemplate
<
String
,
Object
>
redisTemplate
(
RedisConnectionFactory
factory
)
{
RedisTemplate
<
String
,
Object
>
template
=
new
RedisTemplate
<>();
template
.
setConnectionFactory
(
factory
);
CustomJacksonSerializer
jacksonSerial
=
new
CustomJacksonSerializer
(
Object
.
class
);
ObjectMapper
om
=
new
ObjectMapper
();
om
.
setVisibility
(
PropertyAccessor
.
ALL
,
JsonAutoDetect
.
Visibility
.
ANY
);
jacksonSerial
.
setObjectMapper
(
om
);
template
.
setValueSerializer
(
jacksonSerial
);
template
.
setKeySerializer
(
new
StringRedisSerializer
());
template
.
setHashKeySerializer
(
new
StringRedisSerializer
());
template
.
setHashValueSerializer
(
jacksonSerial
);
template
.
afterPropertiesSet
();
return
template
;
}
/**
*
监听
redis
指定频道
*
@
param
redisConnectionFactory
*
@
param
messageListener
*
@
param
cacheManager
*
@
param
redisTemplate
*
@
return
*/
@
Bean
RedisMessageListenerContainer
redisContainer
(
RedisConnectionFactory
redisConnectionFactory
,
MessageListenerAdapter
messageListener
)
{
final
RedisMessageListenerContainer
container
=
new
RedisMessageListenerContainer
();
RedisMessageListenerContainer
redisContainer
(
RedisConnectionFactory
redisConnectionFactory
,
CacheManager
cacheManager
,
RedisTemplate
redisTemplate
)
{
RedisMessageListenerContainer
container
=
new
RedisMessageListenerContainer
();
MessageListenerAdapter
messageListener
=
new
RedisMessageListener
(
cacheManager
,
redisTemplate
);
container
.
setConnectionFactory
(
redisConnectionFactory
);
container
.
addMessageListener
(
messageListener
,
RedisChannelTopic
.
REDIS_CACHE_DELETE_TOPIC
.
getChannelTopic
());
container
.
addMessageListener
(
messageListener
,
RedisChannelTopic
.
REDIS_CACHE_CLEAR_TOPIC
.
getChannelTopic
());
return
container
;
}
}
\ No newline at end of file
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/cache/CusRedisCache.java.ftl
浏览文件 @
2c54e373
...
...
@@ -15,5 +15,4 @@ public class CusRedisCache extends RedisCache {
public
CusRedisCache
(
String
name
,
RedisCacheWriter
redisCacheWriter
,
RedisCacheConfiguration
configuration
)
{
super
(
name
,
redisCacheWriter
,
configuration
);
}
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/cache/LayeringCache.java.ftl
浏览文件 @
2c54e373
...
...
@@ -3,58 +3,55 @@ TARGET=PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
cache
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
listener
.
RedisPublisher
;
import
${
pub
.
getPKGCodeName
()}.
util
.
enums
.
RedisChannelTopic
;
import
org
.
slf4j
.
Logger
;
import
org
.
slf4j
.
LoggerFactory
;
import
lombok
.
Data
;
import
lombok
.
extern
.
slf4j
.
Slf4j
;
import
org
.
springframework
.
cache
.
caffeine
.
CaffeineCache
;
import
org
.
springframework
.
cache
.
support
.
AbstractValueAdaptingCache
;
import
org
.
springframework
.
cache
.
support
.
NullValue
;
import
org
.
springframework
.
data
.
redis
.
cache
.
RedisCache
;
import
org
.
springframework
.
data
.
redis
.
cache
.
RedisCacheConfiguration
;
import
org
.
springframework
.
data
.
redis
.
cache
.
RedisCacheWriter
;
import
org
.
springframework
.
data
.
redis
.
core
.
RedisOperations
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Map
;
import
java
.
util
.
concurrent
.
Callable
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
listener
.
RedisPublisher
;
import
${
pub
.
getPKGCodeName
()}.
util
.
enums
.
RedisChannelTopic
;
/**
*
缓存分层类
*
1
级缓存为
caffeine
*
2
级缓存为
redis
*/
@
Slf4j
public
class
LayeringCache
extends
AbstractValueAdaptingCache
{
Logger
logger
=
LoggerFactory
.
getLogger
(
LayeringCache
.
class
);
/**
*
缓存的名称
*/
private
final
String
n
ame
;
private
String
cacheN
ame
;
/**
*
redis
缓存
*
Caffeine
缓存
*/
private
RedisCache
redis
Cache
;
private
CaffeineCache
caffeine
Cache
;
/**
*
Caffeine
缓存
*
redis
缓存
*/
private
final
CaffeineCache
caffeine
Cache
;
private
RedisCache
redis
Cache
;
/**
*
redis
消息发布
*/
RedisOperations
<?
extends
Object
,
?
extends
Object
>
redisOperations
;
public
LayeringCache
(
String
name
,
RedisOperations
redisOperations
,
com
.
github
.
benmanes
.
caffeine
.
cache
.
Cache
<
Object
,
Object
>
caffeineCache
,
RedisCacheWriter
redisCacheWriter
,
RedisCacheConfiguration
configuration
)
{
public
LayeringCache
(
String
cacheName
,
CaffeineCache
caffeineCache
,
RedisCache
redisCache
,
RedisOperations
redisOperations
)
{
super
(
true
);
this
.
name
=
n
ame
;
this
.
redisCache
=
new
CusRedisCache
(
name
,
redisCacheWriter
,
configuration
)
;
this
.
caffeineCache
=
new
CaffeineCache
(
name
,
caffeineCache
,
true
)
;
this
.
cacheName
=
cacheN
ame
;
this
.
caffeineCache
=
caffeineCache
;
this
.
redisCache
=
redisCache
;
this
.
redisOperations
=
redisOperations
;
}
@
Override
public
String
getName
()
{
return
this
.
n
ame
;
return
this
.
cacheN
ame
;
}
@
Override
...
...
@@ -64,28 +61,24 @@ public class LayeringCache extends AbstractValueAdaptingCache {
@
Override
public
ValueWrapper
get
(
Object
key
)
{
//
查询一级缓存
ValueWrapper
wrapper
=
caffeineCache
.
get
(
key
);
log
ger
.
debug
(
"查询一级缓存 key:{},value:{}"
,
key
,
wrapper
);
log
.
debug
(
"查询一级缓存 key:{},value:{}"
,
key
,
wrapper
);
if
(
wrapper
==
null
)
{
//
查询二级缓存
wrapper
=
redisCache
.
get
(
key
);
caffeineCache
.
put
(
key
,
wrapper
==
null
?
null
:
wrapper
.
get
());
log
ger
.
debug
(
"查询二级缓存,并将数据放到一级缓存。 key:{}"
,
key
);
log
.
debug
(
"查询二级缓存,并将数据放到一级缓存。 key:{}"
,
key
);
}
return
wrapper
;
}
@
Override
public
<
T
>
T
get
(
Object
key
,
Class
<
T
>
type
)
{
//
查询一级缓存
T
value
=
caffeineCache
.
get
(
key
,
type
);
log
ger
.
debug
(
"查询一级缓存 key:{}"
,
key
);
log
.
debug
(
"查询一级缓存 key:{}"
,
key
);
if
(
value
==
null
)
{
//
查询二级缓存
value
=
redisCache
.
get
(
key
,
type
);
caffeineCache
.
put
(
key
,
value
);
log
ger
.
debug
(
"查询二级缓存,并将数据放到一级缓存。 key:{}"
,
key
);
log
.
debug
(
"查询二级缓存,并将数据放到一级缓存。 key:{}"
,
key
);
}
return
value
;
}
...
...
@@ -93,11 +86,9 @@ public class LayeringCache extends AbstractValueAdaptingCache {
@
SuppressWarnings
(
"unchecked"
)
@
Override
public
<
T
>
T
get
(
Object
key
,
Callable
<
T
>
valueLoader
)
{
//
查询一级缓存
,
如果一级缓存没有值则调用
getForSecondaryCache
(
k
,
valueLoader
)
查询二级缓存
T
value
=
(
T
)
caffeineCache
.
getNativeCache
().
get
(
key
,
k
->
getSecondCache
(
k
,
valueLoader
));
T
value
=
(
T
)
caffeineCache
.
getNativeCache
().
get
(
key
,
k
->
getSecondCacheValue
(
k
,
valueLoader
));
if
(
value
==
null
)
{
//
直接查询二级缓存
value
=
(
T
)
getSecondCache
(
key
,
valueLoader
);
value
=
(
T
)
getSecondCacheValue
(
key
,
valueLoader
);
}
if
(
value
instanceof
NullValue
)
{
return
null
;
...
...
@@ -122,11 +113,11 @@ public class LayeringCache extends AbstractValueAdaptingCache {
redisCache
.
evict
(
key
);
//
清除
redis
中的二级缓存
caffeineCache
.
evict
(
key
);//
清除本机一级缓存
Map
<
String
,
Object
>
message
=
new
HashMap
<>();
message
.
put
(
"cacheName"
,
n
ame
);
message
.
put
(
"cacheName"
,
cacheN
ame
);
message
.
put
(
"key"
,
key
);
RedisPublisher
redisPublisher
=
new
RedisPublisher
(
redisOperations
,
RedisChannelTopic
.
REDIS_CACHE_DELETE_TOPIC
.
getChannelTopic
());//
创建
redis
发布者
redisPublisher
.
publisher
(
message
);//
发布消息,清除其它集群机器中的一级缓存
log
ger
.
debug
(
String
.
format
(
"清除二级缓存数据[%s]"
,
key
));
log
.
debug
(
String
.
format
(
"清除二级缓存数据[%s]"
,
key
));
}
@
Override
...
...
@@ -134,7 +125,7 @@ public class LayeringCache extends AbstractValueAdaptingCache {
redisCache
.
clear
();
//
清除
redis
中的二级缓存
caffeineCache
.
clear
();//
清除本机一级缓存
Map
<
String
,
Object
>
message
=
new
HashMap
<>();
message
.
put
(
"cacheName"
,
n
ame
);
message
.
put
(
"cacheName"
,
cacheN
ame
);
RedisPublisher
redisPublisher
=
new
RedisPublisher
(
redisOperations
,
RedisChannelTopic
.
REDIS_CACHE_CLEAR_TOPIC
.
getChannelTopic
());//
创建
redis
发布者
redisPublisher
.
publisher
(
message
);//
发布消息,清除其它集群机器中的一级缓存
}
...
...
@@ -142,31 +133,39 @@ public class LayeringCache extends AbstractValueAdaptingCache {
@
Override
protected
Object
lookup
(
Object
key
)
{
Object
value
=
caffeineCache
.
get
(
key
);
log
ger
.
debug
(
"查询一级缓存 key:{}"
,
key
);
log
.
debug
(
"查询一级缓存 key:{}"
,
key
);
if
(
value
==
null
)
{
value
=
redisCache
.
get
(
key
);
log
ger
.
debug
(
"查询二级缓存 key:{}"
,
key
);
log
.
debug
(
"查询二级缓存 key:{}"
,
key
);
}
return
value
;
}
/**
*
查询二级缓存
*
@
param
key
*
@
param
valueLoader
*
获取
caffeine
缓存
*
@
return
*/
private
<
T
>
Object
getSecondCache
(
Object
key
,
Callable
<
T
>
valueLoader
)
{
T
value
=
redisCache
.
get
(
key
,
valueLoader
);
logger
.
debug
(
"查询二级缓存 key:{}"
,
key
);
return
toStoreValue
(
value
);
public
CaffeineCache
getFirstCache
()
{
return
this
.
caffeineCache
;
}
/**
*
获取
caffeine
缓存
*
获取
redis
缓存
*
@
return
*/
public
CaffeineCache
getFirstCache
()
{
return
this
.
caffeineCache
;
public
RedisCache
getSecondCache
()
{
return
this
.
redisCache
;
}
/**
*
查询二级缓存
*
@
param
key
*
@
param
valueLoader
*
@
return
*/
private
<
T
>
Object
getSecondCacheValue
(
Object
key
,
Callable
<
T
>
valueLoader
)
{
T
value
=
redisCache
.
get
(
key
,
valueLoader
);
log
.
debug
(
"查询二级缓存 key:{}"
,
key
);
return
toStoreValue
(
value
);
}
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/cacheManager/CaffeineCacheManager.java.ftl
浏览文件 @
2c54e373
...
...
@@ -3,19 +3,15 @@ TARGET=PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
cacheManager
;
import
lombok
.
Data
;
import
com
.
github
.
benmanes
.
caffeine
.
cache
.
Caffeine
;
import
com
.
github
.
benmanes
.
caffeine
.
cache
.
CaffeineSpec
;
import
lombok
.
Data
;
import
org
.
springframework
.
boot
.
autoconfigure
.
condition
.
ConditionalOnExpression
;
import
org
.
springframework
.
cache
.
Cache
;
import
org
.
springframework
.
cache
.
CacheManager
;
import
org
.
springframework
.
cache
.
caffeine
.
CaffeineCache
;
import
org
.
springframework
.
stereotype
.
Component
;
import
org
.
springframework
.
util
.
ObjectUtils
;
import
java
.
util
.
Collection
;
import
java
.
util
.
Collections
;
import
java
.
util
.
Map
;
import
java
.
util
.
concurrent
.
ConcurrentHashMap
;
import
java
.
util
.
concurrent
.
ConcurrentMap
;
import
java
.
util
.
concurrent
.
TimeUnit
;
...
...
@@ -24,37 +20,31 @@ import java.util.concurrent.TimeUnit;
*
Caffeine
本地缓存
*/
@
Data
@
Component
@
ConditionalOnExpression
(
"'${r'${ibiz.cacheLevel:None}'}'.equals('L1')"
)
public
class
CaffeineCacheManager
implements
CacheManager
{
private
static
final
int
DEFAULT_EXPIRE_AFTER_WRITE
=
1
;
private
static
final
int
DEFAULT_INITIAL_CAPACITY
=
5
;
private
static
final
int
DEFAULT_MAXIMUM_SIZE
=
1
_000
;
private
final
ConcurrentMap
<
String
,
Cache
>
cacheMap
=
new
ConcurrentHashMap
<
String
,
Cache
>(
16
);
/**
*
缓存默认设置
*/
private
static
final
int
default_expire_after_write
=
1
;
private
static
final
int
default_initial_capacity
=
5
;
private
static
final
int
default_maximum_size
=
1
_000
;
private
Caffeine
<
Object
,
Object
>
cacheBuilder
=
Caffeine
.
newBuilder
()
.
expireAfterAccess
(
DEFAULT_EXPIRE_AFTER_WRITE
,
TimeUnit
.
HOURS
)
.
initialCapacity
(
DEFAULT_INITIAL_CAPACITY
)
.
maximumSize
(
DEFAULT_MAXIMUM_SIZE
);
.
expireAfterAccess
(
default_expire_after_write
,
TimeUnit
.
HOURS
)
.
initialCapacity
(
default_initial_capacity
)
.
maximumSize
(
default_maximum_size
);
/**
*
获取缓存对象
*
@
param
n
ame
*
@
param
cacheN
ame
*
@
return
*/
@
Override
public
Cache
getCache
(
String
n
ame
)
{
Cache
cache
=
this
.
cacheMap
.
get
(
n
ame
);
public
Cache
getCache
(
String
cacheN
ame
)
{
Cache
cache
=
this
.
cacheMap
.
get
(
cacheN
ame
);
if
(
cache
==
null
)
{
synchronized
(
this
.
cacheMap
)
{
cache
=
this
.
cacheMap
.
get
(
n
ame
);
cache
=
this
.
cacheMap
.
get
(
cacheN
ame
);
if
(
cache
==
null
)
{
cache
=
createCache
(
n
ame
);
this
.
cacheMap
.
put
(
n
ame
,
cache
);
cache
=
createCache
(
cacheN
ame
);
this
.
cacheMap
.
put
(
cacheN
ame
,
cache
);
}
}
}
...
...
@@ -62,41 +52,32 @@ public class CaffeineCacheManager implements CacheManager {
}
/**
*
获取缓存名
*
创建缓存
*
@
param
cacheName
*
@
return
*/
@
Override
public
Collection
<
String
>
getCacheNames
()
{
return
Collections
.
unmodifiableSet
(
this
.
cacheMap
.
keySet
());
protected
Cache
createCache
(
String
cacheName
)
{
return
new
CaffeineCache
(
cacheName
,
this
.
cacheBuilder
.
build
(),
true
);
}
/**
*
创建缓存
*
@
param
name
*
获取缓存名
*
@
return
*/
protected
Cache
createCache
(
String
name
)
{
return
new
CaffeineCache
(
name
,
this
.
cacheBuilder
.
build
(),
true
);
@
Override
public
Collection
<
String
>
getCacheNames
()
{
return
Collections
.
unmodifiableSet
(
this
.
cacheMap
.
keySet
());
}
/**
*
缓存配置
[
缓存容量大小、时长等
]
*
@
param
caffeine
Spec
*
@
param
caffeine
CacheConfig
*/
public
void
setCaffeine
Spec
(
CaffeineSpec
caffeineSpec
)
{
Caffeine
<
Object
,
Object
>
cacheBuilder
=
Caffeine
.
from
(
caffeine
Spec
);
public
void
setCaffeine
CacheConfig
(
CaffeineSpec
caffeineCacheConfig
)
{
Caffeine
<
Object
,
Object
>
cacheBuilder
=
Caffeine
.
from
(
caffeine
CacheConfig
);
if
(
!ObjectUtils.nullSafeEquals(this.cacheBuilder, cacheBuilder)) {
this
.
cacheBuilder
=
cacheBuilder
;
refreshKnownCaches
();
}
}
/**
*
使用该
CacheManager
的当前状态重新创建已知的缓存。
*/
private
void
refreshKnownCaches
()
{
for
(
Map
.
Entry
<
String
,
Cache
>
entry
:
this
.
cacheMap
.
entrySet
())
{
entry
.
setValue
(
createCache
(
entry
.
getKey
()));
}
}
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/cacheManager/LayeringCacheManager.java.ftl
浏览文件 @
2c54e373
...
...
@@ -3,97 +3,97 @@ TARGET=PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
cacheManager
;
import
lombok
.
Data
;
import
com
.
github
.
benmanes
.
caffeine
.
cache
.
Caffeine
;
import
com
.
github
.
benmanes
.
caffeine
.
cache
.
CaffeineSpec
;
import
lombok
.
Data
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
cache
.
LayeringCache
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Autowired
;
import
org
.
springframework
.
boot
.
autoconfigure
.
condition
.
ConditionalOnExpression
;
import
org
.
springframework
.
cache
.
Cache
;
import
org
.
springframework
.
cache
.
CacheManager
;
import
org
.
springframework
.
cache
.
caffeine
.
CaffeineCache
;
import
org
.
springframework
.
data
.
redis
.
cache
.
RedisCacheConfiguration
;
import
org
.
springframework
.
data
.
redis
.
cache
.
RedisCacheWriter
;
import
org
.
springframework
.
data
.
redis
.
core
.
RedisTemplate
;
import
org
.
springframework
.
stereotype
.
Component
;
import
org
.
springframework
.
util
.
ObjectUtils
;
import
java
.
util
.
Collection
;
import
java
.
util
.
Collections
;
import
java
.
util
.
Map
;
import
java
.
util
.
concurrent
.
ConcurrentHashMap
;
import
java
.
util
.
concurrent
.
ConcurrentMap
;
import
java
.
util
.
concurrent
.
TimeUnit
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
cache
.
CusRedisCache
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
cache
.
LayeringCache
;
/**
*
缓存分层
类
*
缓存分层
*
1
级缓存为
caffeine
*
2
级缓存为
redis
*/
@
Data
@
Component
@
ConditionalOnExpression
(
"'${r'${ibiz.cacheLevel:None}'}'.equals('L2')"
)
public
class
LayeringCacheManager
implements
CacheManager
{
private
static
final
int
DEFAULT_EXPIRE_AFTER_WRITE
=
1
;
private
static
final
int
DEFAULT_INITIAL_CAPACITY
=
5
;
private
static
final
int
DEFAULT_MAXIMUM_SIZE
=
1
_000
;
private
final
ConcurrentMap
<
String
,
Cache
>
cacheMap
=
new
ConcurrentHashMap
<
String
,
Cache
>(
16
);
public
RedisCacheWriter
redisCacheWriter
;
public
RedisCacheConfiguration
redisConfiguration
;
@
Autowired
public
RedisTemplate
<
String
,
Object
>
redisTemplate
;
/**
*
缓存默认设置
*/
private
static
final
int
default_expire_after_write
=
1
;
private
static
final
int
default_initial_capacity
=
5
;
private
static
final
int
default_maximum_size
=
1
_000
;
private
Caffeine
<
Object
,
Object
>
cacheBuilder
=
Caffeine
.
newBuilder
()
.
expireAfterAccess
(
DEFAULT_EXPIRE_AFTER_WRITE
,
TimeUnit
.
HOURS
)
.
initialCapacity
(
DEFAULT_INITIAL_CAPACITY
)
.
maximumSize
(
DEFAULT_MAXIMUM_SIZE
);
.
expireAfterAccess
(
default_expire_after_write
,
TimeUnit
.
HOURS
)
.
initialCapacity
(
default_initial_capacity
)
.
maximumSize
(
default_maximum_size
);
private
RedisCacheWriter
redisCacheWriter
;
private
RedisCacheConfiguration
redisConfiguration
;
private
RedisTemplate
<
String
,
Object
>
redisTemplate
;
public
LayeringCacheManager
(
RedisCacheWriter
redisCacheWriter
,
RedisCacheConfiguration
redisConfiguration
,
RedisTemplate
<
String
,
Object
>
redisTemplate
){
this
.
redisCacheWriter
=
redisCacheWriter
;
this
.
redisConfiguration
=
redisConfiguration
;
this
.
redisTemplate
=
redisTemplate
;
}
/**
*
获取缓存对象
*
@
param
n
ame
*
@
param
cacheN
ame
*
@
return
*/
@
Override
public
Cache
getCache
(
String
n
ame
)
{
Cache
cache
=
this
.
cacheMap
.
get
(
n
ame
);
public
Cache
getCache
(
String
cacheN
ame
)
{
Cache
cache
=
this
.
cacheMap
.
get
(
cacheN
ame
);
if
(
cache
==
null
)
{
synchronized
(
this
.
cacheMap
)
{
cache
=
this
.
cacheMap
.
get
(
n
ame
);
cache
=
this
.
cacheMap
.
get
(
cacheN
ame
);
if
(
cache
==
null
)
{
cache
=
createCache
(
n
ame
);
this
.
cacheMap
.
put
(
n
ame
,
cache
);
cache
=
createCache
(
cacheN
ame
);
this
.
cacheMap
.
put
(
cacheN
ame
,
cache
);
}
}
}
return
cache
;
}
/**
*
获取集合中的缓存
*
@
return
*/
@
Override
public
Collection
<
String
>
getCacheNames
()
{
return
Collections
.
unmodifiableSet
(
this
.
cacheMap
.
keySet
());
}
protected
Cache
createCache
(
String
name
)
{
return
new
LayeringCache
(
name
,
redisTemplate
,
this
.
cacheBuilder
.
build
(),
redisCacheWriter
,
redisConfiguration
);
}
/**
*
使用该
CacheManager
的当前状态重新创建已知的缓存
*
创建缓存
*
@
param
cacheName
*
@
return
*/
private
void
refreshKnownCaches
()
{
for
(
Map
.
Entry
<
String
,
Cache
>
entry
:
this
.
cacheMap
.
entrySet
())
{
entry
.
setValue
(
createCache
(
entry
.
getKey
()));
}
protected
Cache
createCache
(
String
cacheName
)
{
return
new
LayeringCache
(
cacheName
,
new
CaffeineCache
(
cacheName
,
this
.
cacheBuilder
.
build
(),
true
),
new
CusRedisCache
(
cacheName
,
redisCacheWriter
,
redisConfiguration
),
redisTemplate
);
}
public
void
setCaffeineSpec
(
CaffeineSpec
caffeineSpec
)
{
Caffeine
<
Object
,
Object
>
cacheBuilder
=
Caffeine
.
from
(
caffeineSpec
);
/**
*
缓存配置
[
缓存容量大小、时长等
]
*
@
param
caffeineCacheConfig
*/
public
void
setCaffeineCacheConfig
(
CaffeineSpec
caffeineCacheConfig
)
{
Caffeine
<
Object
,
Object
>
cacheBuilder
=
Caffeine
.
from
(
caffeineCacheConfig
);
if
(
!ObjectUtils.nullSafeEquals(this.cacheBuilder, cacheBuilder)) {
this
.
cacheBuilder
=
cacheBuilder
;
refreshKnownCaches
();
}
}
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/listener/RedisMessageListener.java.ftl
浏览文件 @
2c54e373
...
...
@@ -3,39 +3,33 @@ TARGET=PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
listener
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
cache
.
LayeringCache
;
import
${
pub
.
getPKGCodeName
()}.
util
.
enums
.
RedisChannelTopic
;
import
org
.
slf4j
.
Logger
;
import
org
.
slf4j
.
LoggerFactory
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Autowired
;
import
org
.
springframework
.
boot
.
autoconfigure
.
condition
.
ConditionalOnExpression
;
import
lombok
.
extern
.
slf4j
.
Slf4j
;
import
org
.
springframework
.
cache
.
Cache
;
import
org
.
springframework
.
cache
.
CacheManager
;
import
org
.
springframework
.
data
.
redis
.
connection
.
Message
;
import
org
.
springframework
.
data
.
redis
.
core
.
RedisTemplate
;
import
org
.
springframework
.
data
.
redis
.
listener
.
adapter
.
MessageListenerAdapter
;
import
org
.
springframework
.
data
.
redis
.
serializer
.
RedisSerializer
;
import
org
.
springframework
.
stereotype
.
Component
;
import
org
.
springframework
.
util
.
StringUtils
;
import
java
.
util
.
Map
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
cache
.
LayeringCache
;
import
${
pub
.
getPKGCodeName
()}.
util
.
enums
.
RedisChannelTopic
;
/**
*
redis
消息的订阅者
*/
@
Component
@
ConditionalOnExpression
(
"'${r'${ibiz.cacheLevel:None}'}'.equals('L2')"
)
@
Slf4j
public
class
RedisMessageListener
extends
MessageListenerAdapter
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
RedisPublisher
.
class
);
@
Autowired
CacheManager
cacheManager
;
@
Autowired
RedisTemplate
redisTemplate
;
private
CacheManager
cacheManager
;
private
RedisTemplate
redisTemplate
;
public
RedisMessageListener
(
CacheManager
cacheManager
,
RedisTemplate
redisTemplate
){
this
.
cacheManager
=
cacheManager
;
this
.
redisTemplate
=
redisTemplate
;
}
@
Override
public
void
onMessage
(
Message
message
,
byte
[]
pattern
)
{
super
.
onMessage
(
message
,
pattern
);
RedisChannelTopic
channelTopic
=
RedisChannelTopic
.
getChannelTopicEnum
(
new
String
(
message
.
getChannel
()));
Map
<
String
,
Object
>
map
=
null
;
RedisSerializer
serializer
=
redisTemplate
.
getValueSerializer
();
...
...
@@ -44,10 +38,10 @@ public class RedisMessageListener extends MessageListenerAdapter {
map
=
(
Map
<
String
,
Object
>)
result
;
}
if
(
StringUtils
.
isEmpty
(
map
)||
(
!map.containsKey("cacheName"))|| (!map.containsKey("key"))){
log
ger
.
debug
(
"解析缓存数据失败,无法获取指定值!"
);
log
.
debug
(
"解析缓存数据失败,无法获取指定值!"
);
return
;
}
log
ger
.
debug
(
"redis消息订阅者接收到频道【{}】发布的消息。消息内容:{}"
,
channelTopic
.
getChannelTopicStr
(),
result
.
toString
());
log
.
debug
(
"redis消息订阅者接收到频道【{}】发布的消息。消息内容:{}"
,
channelTopic
.
getChannelTopicStr
(),
result
.
toString
());
String
cacheName
=
(
String
)
map
.
get
(
"cacheName"
);
Object
key
=
map
.
get
(
"key"
);
Cache
cache
=
cacheManager
.
getCache
(
cacheName
);//
根据缓存名称获取多级缓存
...
...
@@ -55,14 +49,16 @@ public class RedisMessageListener extends MessageListenerAdapter {
switch
(
channelTopic
)
{
case
REDIS_CACHE_DELETE_TOPIC
:
//
获取一级缓存,并删除一级缓存数据
((
LayeringCache
)
cache
).
getFirstCache
().
evict
(
key
);
logger
.
debug
(
"删除一级缓存{}数据,key:{},"
,
cacheName
,
key
.
toString
());
((
LayeringCache
)
cache
).
getSecondCache
().
evict
(
key
);
log
.
debug
(
"同步删除缓存{}数据,key:{},"
,
cacheName
,
key
.
toString
());
break
;
case
REDIS_CACHE_CLEAR_TOPIC
://
获取一级缓存,并删除一级缓存数据
((
LayeringCache
)
cache
).
getFirstCache
().
clear
();
logger
.
debug
(
"清除一级缓存{}数据"
,
cacheName
);
((
LayeringCache
)
cache
).
getSecondCache
().
clear
();
log
.
debug
(
"同步清除缓存{}数据"
,
cacheName
);
break
;
default
:
log
ger
.
debug
(
"接收到没有定义的订阅消息频道数据"
);
log
.
debug
(
"接收到没有定义的订阅消息频道数据"
);
break
;
}
}
...
...
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/redis/CustomJacksonSerializer.java.ftl
0 → 100644
浏览文件 @
2c54e373
<#
ibiztemplate
>
TARGET
=
PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
redis
;
import
${
pub
.
getPKGCodeName
()}.
util
.
security
.
AuthenticationUser
;
import
org
.
springframework
.
data
.
redis
.
serializer
.
Jackson2JsonRedisSerializer
;
import
org
.
springframework
.
data
.
redis
.
serializer
.
SerializationException
;
import
java
.
util
.
regex
.
Matcher
;
import
java
.
util
.
regex
.
Pattern
;
public
class
CustomJacksonSerializer
<
T
>
extends
Jackson2JsonRedisSerializer
<
T
>
{
public
static
final
String
DEFAULT_PACKAGE
=
"[
\\
w+
\\
.]+
\\
.AuthenticationUser"
;
public
static
final
String
USER_PACKAGE
=
AuthenticationUser
.
class
.
getName
();
public
CustomJacksonSerializer
(
Class
type
)
{
super
(
type
);
}
@
Override
public
T
deserialize
(
byte
[]
bytes
)
throws
SerializationException
{
String
serializerContent
=
new
String
(
bytes
,
DEFAULT_CHARSET
);
Matcher
matcher
=
Pattern
.
compile
(
DEFAULT_PACKAGE
).
matcher
(
serializerContent
);
return
matcher
.
find
()?
super
.
deserialize
(
serializerContent
.
replaceAll
(
DEFAULT_PACKAGE
,
USER_PACKAGE
).
getBytes
()):
super
.
deserialize
(
bytes
);
}
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/redis/CustomizedRedisCache.java.ftl
浏览文件 @
2c54e373
...
...
@@ -7,13 +7,10 @@ import org.springframework.data.redis.cache.RedisCache;
import
org
.
springframework
.
data
.
redis
.
cache
.
RedisCacheConfiguration
;
import
org
.
springframework
.
data
.
redis
.
cache
.
RedisCacheWriter
;
/**
*
自定义的
redis
缓存
*/
public
class
CustomizedRedisCache
extends
RedisCache
{
public
class
CustomizedRedisCache
{
public
CustomizedRedisCache
(
String
name
,
RedisCacheWriter
redisCacheWriter
,
RedisCacheConfiguration
configuration
)
{
super
(
name
,
redisCacheWriter
,
configuration
);
}
<#--
public
CustomizedRedisCache
(
String
name
,
RedisCacheWriter
redisCacheWriter
,
RedisCacheConfiguration
configuration
)
{-->
<#--
super
(
name
,
redisCacheWriter
,
configuration
);-->
<#--}-->
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/redis/FastJsonRedisSerializer.java.ftl
浏览文件 @
2c54e373
...
...
@@ -3,48 +3,42 @@ TARGET=PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
redis
;
import
com
.
alibaba
.
fastjson
.
JSON
;
import
com
.
alibaba
.
fastjson
.
serializer
.
SerializerFeature
;
import
org
.
springframework
.
data
.
redis
.
serializer
.
RedisSerializer
;
import
org
.
springframework
.
data
.
redis
.
serializer
.
SerializationException
;
import
java
.
nio
.
charset
.
Charset
;
/**
*
Value
序列化
*
*
@
author
/
*
@
param
<
T
>
*/
public
class
FastJsonRedisSerializer
<
T
>
implements
RedisSerializer
<
T
>
<#--
import
com
.
alibaba
.
fastjson
.
JSON
;-->
<#--
import
com
.
alibaba
.
fastjson
.
serializer
.
SerializerFeature
;-->
<#--
import
org
.
springframework
.
data
.
redis
.
serializer
.
RedisSerializer
;-->
<#--
import
org
.
springframework
.
data
.
redis
.
serializer
.
SerializationException
;-->
<#--
import
java
.
nio
.
charset
.
Charset
;-->
public
class
FastJsonRedisSerializer
{
public
static
final
Charset
DEFAULT_CHARSET
=
Charset
.
forName
(
"UTF-8"
);
private
Class
<
T
>
clazz
;
public
FastJsonRedisSerializer
(
Class
<
T
>
clazz
)
{
super
();
this
.
clazz
=
clazz
;
}
@
Override
public
byte
[]
serialize
(
T
t
)
throws
SerializationException
{
if
(
t
==
null
)
{
return
new
byte
[
0
];
}
return
JSON
.
toJSONString
(
t
,
SerializerFeature
.
WriteClassName
).
getBytes
(
DEFAULT_CHARSET
);
}
@
Override
public
T
deserialize
(
byte
[]
bytes
)
throws
SerializationException
{
if
(
bytes
==
null
||
bytes
.
length
<=
0
)
{
return
null
;
}
String
str
=
new
String
(
bytes
,
DEFAULT_CHARSET
);
return
(
T
)
JSON
.
parseObject
(
str
,
clazz
);
}
<#--
public
static
final
Charset
DEFAULT_CHARSET
=
Charset
.
forName
(
"UTF-8"
);-->
<#--
private
Class
<
T
>
clazz
;-->
<#--
public
FastJsonRedisSerializer
(
Class
<
T
>
clazz
)
{-->
<#--
super
();-->
<#--
this
.
clazz
=
clazz
;-->
<#--}-->
<#--@
Override
-->
<#--
public
byte
[]
serialize
(
T
t
)
throws
SerializationException
-->
<#--{-->
<#--
if
(
t
==
null
)
{-->
<#--
return
new
byte
[
0
];-->
<#--}-->
<#--
return
JSON
.
toJSONString
(
t
,
SerializerFeature
.
WriteClassName
).
getBytes
(
DEFAULT_CHARSET
);-->
<#--}-->
<#--@
Override
-->
<#--
public
T
deserialize
(
byte
[]
bytes
)
throws
SerializationException
-->
<#--{-->
<#--
if
(
bytes
==
null
||
bytes
.
length
<=
0
)
{-->
<#--
return
null
;-->
<#--}-->
<#--
String
str
=
new
String
(
bytes
,
DEFAULT_CHARSET
);-->
<#--
return
(
T
)
JSON
.
parseObject
(
str
,
clazz
);-->
<#--}-->
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/redis/KryoRedisSerializer.java.ftl
浏览文件 @
2c54e373
...
...
@@ -3,72 +3,6 @@ TARGET=PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
redis
;
import
com
.
esotericsoftware
.
kryo
.
Kryo
;
import
com
.
esotericsoftware
.
kryo
.
io
.
Input
;
import
com
.
esotericsoftware
.
kryo
.
io
.
Output
;
import
org
.
slf4j
.
Logger
;
import
org
.
slf4j
.
LoggerFactory
;
import
org
.
springframework
.
data
.
redis
.
serializer
.
RedisSerializer
;
import
org
.
springframework
.
data
.
redis
.
serializer
.
SerializationException
;
public
class
KryoRedisSerializer
{
import
java
.
io
.
ByteArrayOutputStream
;
/**
*
@
param
<
T
>
*
序列化
/
反序列化工具类
*/
public
class
KryoRedisSerializer
<
T
>
implements
RedisSerializer
<
T
>
{
Logger
logger
=
LoggerFactory
.
getLogger
(
KryoRedisSerializer
.
class
);
public
static
final
byte
[]
EMPTY_BYTE_ARRAY
=
new
byte
[
0
];
private
static
final
ThreadLocal
<
Kryo
>
kryos
=
ThreadLocal
.
withInitial
(
Kryo
::
new
);
private
Class
<
T
>
clazz
;
public
KryoRedisSerializer
(
Class
<
T
>
clazz
)
{
super
();
this
.
clazz
=
clazz
;
}
@
Override
public
byte
[]
serialize
(
T
t
)
throws
SerializationException
{
if
(
t
==
null
)
{
return
EMPTY_BYTE_ARRAY
;
}
Kryo
kryo
=
kryos
.
get
();
kryo
.
setReferences
(
false
);
kryo
.
register
(
clazz
);
try
(
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
Output
output
=
new
Output
(
baos
))
{
kryo
.
writeClassAndObject
(
output
,
t
);
output
.
flush
();
return
baos
.
toByteArray
();
}
catch
(
Exception
e
)
{
logger
.
error
(
e
.
getMessage
(),
e
);
}
return
EMPTY_BYTE_ARRAY
;
}
@
Override
public
T
deserialize
(
byte
[]
bytes
)
throws
SerializationException
{
if
(
bytes
==
null
||
bytes
.
length
<=
0
)
{
return
null
;
}
Kryo
kryo
=
kryos
.
get
();
kryo
.
setReferences
(
false
);
kryo
.
register
(
clazz
);
try
(
Input
input
=
new
Input
(
bytes
))
{
return
(
T
)
kryo
.
readClassAndObject
(
input
);
}
catch
(
Exception
e
)
{
logger
.
error
(
e
.
getMessage
(),
e
);
}
return
null
;
}
}
\ No newline at end of file
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/redis/RedisConfig.java.ftl
浏览文件 @
2c54e373
...
...
@@ -3,66 +3,6 @@ TARGET=PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
redis
;
import
com
.
alibaba
.
fastjson
.
JSON
;
import
com
.
alibaba
.
fastjson
.
parser
.
ParserConfig
;
import
lombok
.
extern
.
slf4j
.
Slf4j
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Value
;
import
org
.
springframework
.
boot
.
autoconfigure
.
condition
.
ConditionalOnClass
;
import
org
.
springframework
.
boot
.
autoconfigure
.
data
.
redis
.
RedisProperties
;
import
org
.
springframework
.
boot
.
context
.
properties
.
EnableConfigurationProperties
;
import
org
.
springframework
.
cache
.
annotation
.
CachingConfigurerSupport
;
import
org
.
springframework
.
cache
.
annotation
.
EnableCaching
;
import
org
.
springframework
.
cache
.
interceptor
.
KeyGenerator
;
import
org
.
springframework
.
context
.
annotation
.
Bean
;
import
org
.
springframework
.
context
.
annotation
.
Configuration
;
import
org
.
springframework
.
data
.
redis
.
cache
.
RedisCacheConfiguration
;
import
org
.
springframework
.
data
.
redis
.
core
.
RedisOperations
;
import
org
.
springframework
.
data
.
redis
.
serializer
.
RedisSerializationContext
;
import
java
.
time
.
Duration
;
public
class
RedisConfig
{
@
Slf4j
@
Configuration
@
ConditionalOnClass
(
RedisOperations
.
class
)
@
EnableConfigurationProperties
(
RedisProperties
.
class
)
public
class
RedisConfig
extends
CachingConfigurerSupport
{
@
Value
(
"${r'${spring.cache.redis.time-to-live:3600}'}"
)
private
long
timetolive
;
/**
*
设置
redis
数据默认过期时间,默认
1
天
*
设置
@
cacheable
序列化方式
*
@
return
*/
@
Bean
public
RedisCacheConfiguration
redisCacheConfiguration
(){
FastJsonRedisSerializer
<
Object
>
fastJsonRedisSerializer
=
new
FastJsonRedisSerializer
<>(
Object
.
class
);
RedisCacheConfiguration
configuration
=
RedisCacheConfiguration
.
defaultCacheConfig
();
ParserConfig
.
getGlobalInstance
().
setAutoTypeSupport
(
true
);
configuration
=
configuration
.
serializeValuesWith
(
RedisSerializationContext
.
SerializationPair
.
fromSerializer
(
fastJsonRedisSerializer
)).
entryTtl
(
Duration
.
ofSeconds
(
timetolive
));
return
configuration
;
}
/**
*
自定义缓存
key
生成策略
*
使用方法
@
Cacheable
(
keyGenerator
=
"keyGenerator"
)
*
@
return
*/
@
Bean
@
Override
public
KeyGenerator
keyGenerator
()
{
return
(
target
,
method
,
params
)
->
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
target
.
getClass
().
getName
());
sb
.
append
(
method
.
getName
());
for
(
Object
obj
:
params
)
{
//
由于参数可能不同
,
hashCode
肯定不一样
,
缓存的
key
也需要不一样
sb
.
append
(
JSON
.
toJSONString
(
obj
).
hashCode
());
}
return
sb
.
toString
();
};
}
}
\ No newline at end of file
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/redis/StringRedisSerializer.java.ftl
浏览文件 @
2c54e373
...
...
@@ -3,44 +3,6 @@ TARGET=PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
redis
;
import
com
.
alibaba
.
fastjson
.
JSON
;
import
org
.
springframework
.
data
.
redis
.
serializer
.
RedisSerializer
;
import
org
.
springframework
.
util
.
Assert
;
public
class
StringRedisSerializer
{
import
java
.
nio
.
charset
.
Charset
;
/**
*
必须重写序列化器,否则
@
Cacheable
注解的
key
会报类型转换错误
*/
public
class
StringRedisSerializer
implements
RedisSerializer
<
Object
>
{
private
final
Charset
charset
;
private
final
String
target
=
"
\"
"
;
private
final
String
replacement
=
""
;
public
StringRedisSerializer
()
{
this
(
Charset
.
forName
(
"UTF8"
));
}
public
StringRedisSerializer
(
Charset
charset
)
{
Assert
.
notNull
(
charset
,
"Charset must not be null!"
);
this
.
charset
=
charset
;
}
@
Override
public
String
deserialize
(
byte
[]
bytes
)
{
return
(
bytes
==
null
?
null
:
new
String
(
bytes
,
charset
));
}
@
Override
public
byte
[]
serialize
(
Object
object
)
{
String
string
=
JSON
.
toJSONString
(
object
);
if
(
string
==
null
)
{
return
null
;
}
string
=
string
.
replace
(
target
,
replacement
);
return
string
.
getBytes
(
charset
);
}
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/enums/RedisChannelTopic.java.ftl
浏览文件 @
2c54e373
...
...
@@ -8,8 +8,8 @@ import org.springframework.data.redis.listener.ChannelTopic;
*
redis
频道
*/
public
enum
RedisChannelTopic
{
REDIS_CACHE_DELETE_TOPIC
(
"redis:cache:delete:topic"
,
"删除redis缓存消息频道"
),
REDIS_CACHE_CLEAR_TOPIC
(
"redis:cache:clear:topic"
,
"清空redis缓存消息频道"
);
REDIS_CACHE_DELETE_TOPIC
(
"redis:cache:delete:topic
1
"
,
"删除redis缓存消息频道"
),
REDIS_CACHE_CLEAR_TOPIC
(
"redis:cache:clear:topic
2
"
,
"清空redis缓存消息频道"
);
String
channelTopic
;
String
label
;
...
...
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/security/AuthenticationUser.java.ftl
浏览文件 @
2c54e373
...
...
@@ -146,7 +146,8 @@ public class AuthenticationUser implements UserDetails
return
new
HashMap
<>();
}
public
Collection
<
GrantedAuthority
>
getAuthorities
()
{
public
void
setPermissionList
(
JSONObject
permissionList
)
{
this
.
permissionList
=
permissionList
;
if
(
authorities
==
null
&&
permissionList
!=null){
if
(
permissionList
.
getJSONArray
(
"authorities"
)
!=null){
authorities
=
new
ArrayList
<>();
...
...
@@ -154,6 +155,5 @@ public class AuthenticationUser implements UserDetails
forEach
(
item
->
authorities
.
add
(
new
SimpleGrantedAuthority
(
String
.
valueOf
(
item
))));
}
}
return
authorities
;
}
}
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录