Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
iBiz4j Spring R7
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
iBiz-R7后台标准模板
iBiz4j Spring R7
提交
fcab7ff0
提交
fcab7ff0
编写于
5月 07, 2020
作者:
zhouweidong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
缓存
上级
976134f7
变更
15
显示空白字符变更
内嵌
并排
正在显示
15 个修改的文件
包含
1070 行增加
和
0 行删除
+1070
-0
pom.xml.ftl
SLN/%PUBPRJ%-dependencies/pom.xml.ftl
+20
-0
pom.xml.ftl
SLN/%PUBPRJ%-util/pom.xml.ftl
+21
-0
CacheConfig.java.ftl
...c/main/java/%SYS_PKGPATH%/util/cache/CacheConfig.java.ftl
+131
-0
LayeringCache.java.ftl
.../%SYS_PKGPATH%/util/cache/layering/LayeringCache.java.ftl
+200
-0
LayeringCacheManager.java.ftl
...KGPATH%/util/cache/layering/LayeringCacheManager.java.ftl
+184
-0
RedisMessageListener.java.ftl
...KGPATH%/util/cache/listener/RedisMessageListener.java.ftl
+67
-0
RedisPublisher.java.ftl
...%SYS_PKGPATH%/util/cache/listener/RedisPublisher.java.ftl
+41
-0
CustomizedRedisCache.java.ftl
...S_PKGPATH%/util/cache/redis/CustomizedRedisCache.java.ftl
+19
-0
FastJsonRedisSerializer.java.ftl
...KGPATH%/util/cache/redis/FastJsonRedisSerializer.java.ftl
+50
-0
KryoRedisSerializer.java.ftl
...YS_PKGPATH%/util/cache/redis/KryoRedisSerializer.java.ftl
+74
-0
RedisConfig.java.ftl
.../java/%SYS_PKGPATH%/util/cache/redis/RedisConfig.java.ftl
+68
-0
StringRedisSerializer.java.ftl
..._PKGPATH%/util/cache/redis/StringRedisSerializer.java.ftl
+46
-0
FirstCacheSetting.java.ftl
...YS_PKGPATH%/util/cache/setting/FirstCacheSetting.java.ftl
+23
-0
SecondaryCacheSetting.java.ftl
...KGPATH%/util/cache/setting/SecondaryCacheSetting.java.ftl
+87
-0
RedisChannelTopic.java.ftl
.../java/%SYS_PKGPATH%/util/enums/RedisChannelTopic.java.ftl
+39
-0
未找到文件。
SLN/%PUBPRJ%-dependencies/pom.xml.ftl
浏览文件 @
fcab7ff0
...
...
@@ -69,6 +69,12 @@ TARGET=PSSYSTEM
<!--Liquibase数据库版本更新工具-->
<liquibase.version>3.6.3</liquibase.version>
<!--caffeine缓存-->
<caffeine-cache.version>2.6.0</caffeine-cache.version>
<!--反序列化工具-->
<kryo.version>4.0.2</kryo.version>
</properties>
<#-- dependencies,just for statement, manage the version of jars -->
...
...
@@ -246,6 +252,20 @@ TARGET=PSSYSTEM
<version>${r'${zalando-problem-spring-web.version}'}</version>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>${r'${caffeine-cache.version}'}</version>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo-shaded</artifactId>
<version>${r'${kryo.version}'}</version>
</dependency>
</dependencies>
</dependencyManagement>
...
...
SLN/%PUBPRJ%-util/pom.xml.ftl
浏览文件 @
fcab7ff0
...
...
@@ -57,5 +57,26 @@ TARGET=PSSYSTEM
<artifactId>problem-spring-web</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo-shaded</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
</dependencies>
</project>
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/CacheConfig.java.ftl
0 → 100644
浏览文件 @
fcab7ff0
<#
ibiztemplate
>
TARGET
=
PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
config
;
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
.
enums
.
RedisChannelTopic
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
layering
.
LayeringCacheManager
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
redis
.
KryoRedisSerializer
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
redis
.
StringRedisSerializer
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
setting
.
FirstCacheSetting
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Autowired
;
import
org
.
springframework
.
boot
.
autoconfigure
.
cache
.
CacheProperties
;
import
org
.
springframework
.
boot
.
context
.
properties
.
EnableConfigurationProperties
;
import
org
.
springframework
.
cache
.
CacheManager
;
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
.
cache
.
annotation
.
EnableCaching
;
import
org
.
springframework
.
context
.
annotation
.
Profile
;
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
.
util
.
StringUtils
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Map
;
/**
*
缓存配置类
*
1
级缓存为
caffeine
*
2
级缓存为
redis
*/
@
Profile
(
"prod"
)
@
EnableCaching
@
Configuration
@
EnableConfigurationProperties
(
CacheProperties
.
class
)
public
class
CacheConfig
{
@
Bean
public
RedisCacheManager
redisCacheManager
(
RedisConnectionFactory
connectionFactory
)
{
return
RedisCacheManager
.
create
(
connectionFactory
);
}
@
Bean
public
RedisCacheWriter
redisCacheWriter
(
RedisConnectionFactory
connectionFactory
){
RedisCacheWriter
redisCacheWriter
=
RedisCacheWriter
.
nonLockingRedisCacheWriter
(
connectionFactory
);
return
redisCacheWriter
;
}
@
Autowired
private
RedisCacheWriter
redisCacheWriter
;
@
Autowired
private
RedisCacheConfiguration
configuration
;
/**
*
重写
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
*
@
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
);
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
;
}
@
Bean
@
Primary
public
CacheManager
cacheManager
(
RedisTemplate
<
String
,
Object
>
redisTemplate
)
{
LayeringCacheManager
layeringCacheManager
=
new
LayeringCacheManager
(
redisTemplate
);
setFirstCacheConfig
(
layeringCacheManager
);//
Caffeine
缓存设置
layeringCacheManager
.
setAllowNullValues
(
true
);
//
允许存
null
,防止缓存击穿
layeringCacheManager
.
setRedisCacheWriter
(
redisCacheWriter
);
layeringCacheManager
.
setRedisConfiguration
(
configuration
);
return
layeringCacheManager
;
}
@
Autowired
private
CacheProperties
cacheProperties
;
private
void
setFirstCacheConfig
(
LayeringCacheManager
layeringCacheManager
)
{
String
specification
=
cacheProperties
.
getCaffeine
().
getSpec
();
if
(
StringUtils
.
hasText
(
specification
))
{
layeringCacheManager
.
setCaffeineSpec
(
CaffeineSpec
.
parse
(
specification
));
}
Map
<
String
,
FirstCacheSetting
>
firstCacheSettings
=
new
HashMap
<>();
layeringCacheManager
.
setFirstCacheSettings
(
firstCacheSettings
);
}
<#--/**-->
<#--*
监听
redis
指定频道
-->
<#--*
@
param
redisConnectionFactory
-->
<#--*
@
param
messageListener
-->
<#--*
@
return
-->
<#--*/-->
<#--@
Bean
-->
<#--
RedisMessageListenerContainer
redisContainer
(
RedisConnectionFactory
redisConnectionFactory
,
MessageListenerAdapter
messageListener
)
{-->
<#--
final
RedisMessageListenerContainer
container
=
new
RedisMessageListenerContainer
();-->
<#--
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/layering/LayeringCache.java.ftl
0 → 100644
浏览文件 @
fcab7ff0
<#
ibiztemplate
>
TARGET
=
PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
layering
;
import
${
pub
.
getPKGCodeName
()}.
util
.
enums
.
RedisChannelTopic
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
listener
.
RedisPublisher
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
redis
.
CustomizedRedisCache
;
import
org
.
slf4j
.
Logger
;
import
org
.
slf4j
.
LoggerFactory
;
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
;
/**
*
缓存分层类
*
1
级缓存为
caffeine
*
2
级缓存为
redis
*/
public
class
LayeringCache
extends
AbstractValueAdaptingCache
{
Logger
logger
=
LoggerFactory
.
getLogger
(
LayeringCache
.
class
);
/**
*
缓存的名称
*/
private
final
String
name
;
/**
*
是否使用一级缓存
*/
private
boolean
usedFirstCache
=
true
;
/**
*
redi
缓存
*/
private
RedisCache
redisCache
;
/**
*
Caffeine
缓存
*/
private
final
CaffeineCache
caffeineCache
;
RedisOperations
<?
extends
Object
,
?
extends
Object
>
redisOperations
;
/**
*
@
param
name
缓存名称
*
@
param
allowNullValues
是否允许存
NULL
,默认是
false
*
@
param
usedFirstCache
是否使用一级缓存,默认是
true
*
@
param
caffeineCache
Caffeine
缓存
*/
public
LayeringCache
(
String
name
,
boolean
allowNullValues
,
RedisOperations
redisOperations
,
boolean
usedFirstCache
,
com
.
github
.
benmanes
.
caffeine
.
cache
.
Cache
<
Object
,
Object
>
caffeineCache
,
RedisCacheWriter
redisCacheWriter
,
RedisCacheConfiguration
configuration
)
{
super
(
allowNullValues
);
this
.
name
=
name
;
this
.
usedFirstCache
=
usedFirstCache
;
this
.
redisCache
=
new
CustomizedRedisCache
(
name
,
redisCacheWriter
,
configuration
);
this
.
caffeineCache
=
new
CaffeineCache
(
name
,
caffeineCache
,
allowNullValues
);
this
.
redisOperations
=
redisOperations
;
}
public
CaffeineCache
getFirstCache
()
{
return
this
.
caffeineCache
;
}
@
Override
public
String
getName
()
{
return
this
.
name
;
}
@
Override
public
Object
getNativeCache
()
{
return
this
;
}
@
Override
public
ValueWrapper
get
(
Object
key
)
{
ValueWrapper
wrapper
=
null
;
if
(
usedFirstCache
)
{
//
查询一级缓存
wrapper
=
caffeineCache
.
get
(
key
);
logger
.
debug
(
"查询一级缓存 key:{},value:{}"
,
key
,
wrapper
);
}
if
(
wrapper
==
null
)
{
//
查询二级缓存
wrapper
=
redisCache
.
get
(
key
);
caffeineCache
.
put
(
key
,
wrapper
==
null
?
null
:
wrapper
.
get
());
logger
.
debug
(
"查询二级缓存,并将数据放到一级缓存。 key:{}"
,
key
);
}
return
wrapper
;
}
@
Override
public
<
T
>
T
get
(
Object
key
,
Class
<
T
>
type
)
{
T
value
=
null
;
if
(
usedFirstCache
)
{
//
查询一级缓存
value
=
caffeineCache
.
get
(
key
,
type
);
logger
.
debug
(
"查询一级缓存 key:{}"
,
key
);
}
if
(
value
==
null
)
{
//
查询二级缓存
value
=
redisCache
.
get
(
key
,
type
);
caffeineCache
.
put
(
key
,
value
);
logger
.
debug
(
"查询二级缓存,并将数据放到一级缓存。 key:{}"
,
key
);
}
return
value
;
}
@
SuppressWarnings
(
"unchecked"
)
@
Override
public
<
T
>
T
get
(
Object
key
,
Callable
<
T
>
valueLoader
)
{
T
value
=
null
;
if
(
usedFirstCache
)
{
//
查询一级缓存
,
如果一级缓存没有值则调用
getForSecondaryCache
(
k
,
valueLoader
)
查询二级缓存
value
=
(
T
)
caffeineCache
.
getNativeCache
().
get
(
key
,
k
->
getForSecondaryCache
(
k
,
valueLoader
));
}
else
{
//
直接查询二级缓存
value
=
(
T
)
getForSecondaryCache
(
key
,
valueLoader
);
}
if
(
value
instanceof
NullValue
)
{
return
null
;
}
return
value
;
}
@
Override
public
void
put
(
Object
key
,
Object
value
)
{
if
(
usedFirstCache
)
{
caffeineCache
.
put
(
key
,
value
);
}
redisCache
.
put
(
key
,
value
);
}
@
Override
public
ValueWrapper
putIfAbsent
(
Object
key
,
Object
value
)
{
if
(
usedFirstCache
)
{
caffeineCache
.
putIfAbsent
(
key
,
value
);
}
return
redisCache
.
putIfAbsent
(
key
,
value
);
}
@
Override
public
void
evict
(
Object
key
)
{
redisCache
.
evict
(
key
);
//
清除
redis
中的二级缓存
if
(
usedFirstCache
)
{
caffeineCache
.
evict
(
key
);//
清除本机一级缓存
Map
<
String
,
Object
>
message
=
new
HashMap
<>();
message
.
put
(
"cacheName"
,
name
);
message
.
put
(
"key"
,
key
);
RedisPublisher
redisPublisher
=
new
RedisPublisher
(
redisOperations
,
RedisChannelTopic
.
REDIS_CACHE_DELETE_TOPIC
.
getChannelTopic
());//
创建
redis
发布者
redisPublisher
.
publisher
(
message
);//
发布消息,清除其它集群机器中的一级缓存
}
logger
.
debug
(
String
.
format
(
"清除二级缓存数据[%s]"
,
key
));
}
@
Override
public
void
clear
()
{
redisCache
.
clear
();
//
清除
redis
中的二级缓存
if
(
usedFirstCache
)
{
caffeineCache
.
clear
();//
清除本机一级缓存
Map
<
String
,
Object
>
message
=
new
HashMap
<>();
message
.
put
(
"cacheName"
,
name
);
RedisPublisher
redisPublisher
=
new
RedisPublisher
(
redisOperations
,
RedisChannelTopic
.
REDIS_CACHE_CLEAR_TOPIC
.
getChannelTopic
());//
创建
redis
发布者
redisPublisher
.
publisher
(
message
);//
发布消息,清除其它集群机器中的一级缓存
}
}
@
Override
protected
Object
lookup
(
Object
key
)
{
Object
value
=
null
;
if
(
usedFirstCache
)
{
value
=
caffeineCache
.
get
(
key
);
logger
.
debug
(
"查询一级缓存 key:{}"
,
key
);
}
if
(
value
==
null
)
{
value
=
redisCache
.
get
(
key
);
logger
.
debug
(
"查询二级缓存 key:{}"
,
key
);
}
return
value
;
}
/**
*
查询二级缓存
*
@
param
key
*
@
param
valueLoader
*
@
return
*/
private
<
T
>
Object
getForSecondaryCache
(
Object
key
,
Callable
<
T
>
valueLoader
)
{
T
value
=
redisCache
.
get
(
key
,
valueLoader
);
logger
.
debug
(
"查询二级缓存 key:{}"
,
key
);
return
toStoreValue
(
value
);
}
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/layering/LayeringCacheManager.java.ftl
0 → 100644
浏览文件 @
fcab7ff0
<#
ibiztemplate
>
TARGET
=
PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
layering
;
import
com
.
github
.
benmanes
.
caffeine
.
cache
.
Caffeine
;
import
com
.
github
.
benmanes
.
caffeine
.
cache
.
CaffeineSpec
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
setting
.
FirstCacheSetting
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
setting
.
SecondaryCacheSetting
;
import
lombok
.
Data
;
import
org
.
springframework
.
cache
.
Cache
;
import
org
.
springframework
.
cache
.
CacheManager
;
import
org
.
springframework
.
data
.
redis
.
cache
.
RedisCacheConfiguration
;
import
org
.
springframework
.
data
.
redis
.
cache
.
RedisCacheWriter
;
import
org
.
springframework
.
data
.
redis
.
core
.
RedisOperations
;
import
org
.
springframework
.
data
.
redis
.
core
.
RedisTemplate
;
import
org
.
springframework
.
util
.
CollectionUtils
;
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
org
.
springframework
.
util
.
StringUtils
;
/**
*
缓存分层类
*
1
级缓存为
caffeine
*
2
级缓存为
redis
*/
@
Data
public
class
LayeringCacheManager
implements
CacheManager
{
static
final
int
DEFAULT_EXPIRE_AFTER_WRITE
=
2
;
static
final
int
DEFAULT_INITIAL_CAPACITY
=
5
;
static
final
int
DEFAULT_MAXIMUM_SIZE
=
1
_000
;
private
final
ConcurrentMap
<
String
,
Cache
>
cacheMap
=
new
ConcurrentHashMap
<
String
,
Cache
>(
16
);
/**
*
一级缓存配置
*/
private
Map
<
String
,
FirstCacheSetting
>
firstCacheSettings
=
null
;
/**
*
二级缓存配置
*/
private
Map
<
String
,
SecondaryCacheSetting
>
secondaryCacheSettings
=
null
;
public
RedisCacheWriter
redisCacheWriter
;
public
RedisCacheConfiguration
redisConfiguration
;
public
RedisOperations
redisOperations
;
/**
*
是否允许动态创建缓存,默认是
true
*/
private
boolean
dynamic
=
true
;
/**
*
缓存值是否允许为
NULL
*/
private
boolean
allowNullValues
=
false
;
/**
*
expireAfterWrite
:
2
小时
*
initialCapacity
:
5
*
maximumSize
:
1
_000
*/
private
Caffeine
<
Object
,
Object
>
cacheBuilder
=
Caffeine
.
newBuilder
()
.
expireAfterAccess
(
DEFAULT_EXPIRE_AFTER_WRITE
,
TimeUnit
.
HOURS
)
.
initialCapacity
(
DEFAULT_INITIAL_CAPACITY
)
.
maximumSize
(
DEFAULT_MAXIMUM_SIZE
);
public
LayeringCacheManager
(
RedisTemplate
<
String
,
Object
>
redisTemplate
)
{
this
.
redisOperations
=
redisTemplate
;
}
/**
*
获取缓存对象
*
@
param
name
*
@
return
*/
@
Override
public
Cache
getCache
(
String
name
)
{
Cache
cache
=
this
.
cacheMap
.
get
(
name
);
if
(
cache
==
null
&&
this
.
dynamic
)
{
synchronized
(
this
.
cacheMap
)
{
cache
=
this
.
cacheMap
.
get
(
name
);
if
(
cache
==
null
)
{
cache
=
createCache
(
name
);
this
.
cacheMap
.
put
(
name
,
cache
);
}
}
}
return
cache
;
}
@
Override
public
Collection
<
String
>
getCacheNames
()
{
return
Collections
.
unmodifiableSet
(
this
.
cacheMap
.
keySet
());
}
protected
Cache
createCache
(
String
name
)
{
return
new
LayeringCache
(
name
,
isAllowNullValues
(),
this
.
redisOperations
,
getUsedFirstCache
(
name
),
createNativeCaffeineCache
(
name
),
redisCacheWriter
,
redisConfiguration
);
}
/**
*
Create
a
native
Caffeine
Cache
instance
for
the
specified
cache
name
.
*
*
@
param
name
the
name
of
the
cache
*
@
return
the
native
Caffeine
Cache
instance
*/
protected
com
.
github
.
benmanes
.
caffeine
.
cache
.
Cache
<
Object
,
Object
>
createNativeCaffeineCache
(
String
name
)
{
return
getCaffeine
(
name
).
build
();
}
/**
*
使用该
CacheManager
的当前状态重新创建已知的缓存。
*/
private
void
refreshKnownCaches
()
{
for
(
Map
.
Entry
<
String
,
Cache
>
entry
:
this
.
cacheMap
.
entrySet
())
{
entry
.
setValue
(
createCache
(
entry
.
getKey
()));
}
}
/**
*
设置是否允许
Cache
的值为
null
*
*
@
param
allowNullValues
*/
public
void
setAllowNullValues
(
boolean
allowNullValues
)
{
if
(
this
.
allowNullValues
!= allowNullValues) {
this
.
allowNullValues
=
allowNullValues
;
refreshKnownCaches
();
}
}
/**
*
获取是否允许
Cache
的值为
null
*
*
@
return
*/
public
boolean
isAllowNullValues
()
{
return
this
.
allowNullValues
;
}
/**
*
根据缓存名称设置一级缓存的有效时间和刷新时间,单位秒
*
*
@
param
firstCacheSettings
*/
public
void
setFirstCacheSettings
(
Map
<
String
,
FirstCacheSetting
>
firstCacheSettings
)
{
this
.
firstCacheSettings
=
(
!CollectionUtils.isEmpty(firstCacheSettings) ? new ConcurrentHashMap<>(firstCacheSettings) : null);
}
/**
*
获取是否使用一级缓存,默认是
true
*/
public
boolean
getUsedFirstCache
(
String
name
)
{
SecondaryCacheSetting
secondaryCacheSetting
=
null
;
if
(
!CollectionUtils.isEmpty(secondaryCacheSettings)) {
secondaryCacheSetting
=
secondaryCacheSettings
.
get
(
name
);
}
return
secondaryCacheSetting
!= null ? secondaryCacheSetting.getUsedFirstCache() : true;
}
public
void
setCaffeineSpec
(
CaffeineSpec
caffeineSpec
)
{
Caffeine
<
Object
,
Object
>
cacheBuilder
=
Caffeine
.
from
(
caffeineSpec
);
if
(
!ObjectUtils.nullSafeEquals(this.cacheBuilder, cacheBuilder)) {
this
.
cacheBuilder
=
cacheBuilder
;
refreshKnownCaches
();
}
}
private
Caffeine
<
Object
,
Object
>
getCaffeine
(
String
name
)
{
if
(
!CollectionUtils.isEmpty(firstCacheSettings)) {
FirstCacheSetting
firstCacheSetting
=
firstCacheSettings
.
get
(
name
);
if
(
firstCacheSetting
!= null && StringUtils.isEmpty(firstCacheSetting.getCacheSpecification())) {
//
根据缓存名称获取一级缓存配置
return
Caffeine
.
from
(
CaffeineSpec
.
parse
(
firstCacheSetting
.
getCacheSpecification
()));
}
}
return
this
.
cacheBuilder
;
}
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/listener/RedisMessageListener.java.ftl
0 → 100644
浏览文件 @
fcab7ff0
<#
ibiztemplate
>
TARGET
=
PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
listener
;
import
${
pub
.
getPKGCodeName
()}.
util
.
enums
.
RedisChannelTopic
;
import
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
layering
.
LayeringCache
;
import
org
.
slf4j
.
Logger
;
import
org
.
slf4j
.
LoggerFactory
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Autowired
;
import
org
.
springframework
.
cache
.
Cache
;
import
org
.
springframework
.
cache
.
CacheManager
;
import
org
.
springframework
.
context
.
annotation
.
Profile
;
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
;
/**
*
redis
消息的订阅者
*/
@
Profile
(
"prod"
)
@
Component
public
class
RedisMessageListener
extends
MessageListenerAdapter
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
RedisPublisher
.
class
);
@
Autowired
CacheManager
cacheManager
;
@
Autowired
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
();
Object
result
=
serializer
.
deserialize
(
message
.
getBody
());
if
(
result
instanceof
Map
){
map
=
(
Map
<
String
,
Object
>)
result
;
}
if
(
StringUtils
.
isEmpty
(
map
)||
(
!map.containsKey("cacheName"))|| (!map.containsKey("key"))){
logger
.
info
(
"解析缓存数据失败,无法获取指定值!"
);
return
;
}
logger
.
debug
(
"redis消息订阅者接收到频道【{}】发布的消息。消息内容:{}"
,
channelTopic
.
getChannelTopicStr
(),
result
.
toString
());
String
cacheName
=
(
String
)
map
.
get
(
"cacheName"
);
Object
key
=
map
.
get
(
"key"
);
Cache
cache
=
cacheManager
.
getCache
(
cacheName
);//
根据缓存名称获取多级缓存
if
(
cache
!= null && cache instanceof LayeringCache) { // 判断缓存是否是多级缓存
switch
(
channelTopic
)
{
case
REDIS_CACHE_DELETE_TOPIC
:
//
获取一级缓存,并删除一级缓存数据
((
LayeringCache
)
cache
).
getFirstCache
().
evict
(
key
);
logger
.
debug
(
"删除一级缓存{}数据,key:{},"
,
cacheName
,
key
.
toString
());
break
;
case
REDIS_CACHE_CLEAR_TOPIC
://
获取一级缓存,并删除一级缓存数据
((
LayeringCache
)
cache
).
getFirstCache
().
clear
();
logger
.
debug
(
"清除一级缓存{}数据"
,
cacheName
);
break
;
default
:
logger
.
debug
(
"接收到没有定义的订阅消息频道数据"
);
break
;
}
}
}
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/listener/RedisPublisher.java.ftl
0 → 100644
浏览文件 @
fcab7ff0
<#
ibiztemplate
>
TARGET
=
PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
listener
;
import
org
.
slf4j
.
Logger
;
import
org
.
slf4j
.
LoggerFactory
;
import
org
.
springframework
.
data
.
redis
.
core
.
RedisOperations
;
import
org
.
springframework
.
data
.
redis
.
listener
.
ChannelTopic
;
/**
*
redis
消息的发布者
*/
public
class
RedisPublisher
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
RedisPublisher
.
class
);
RedisOperations
<?
extends
Object
,
?
extends
Object
>
redisOperations
;
/**
*
频道名称
*/
ChannelTopic
channelTopic
;
/**
*
@
param
redisOperations
Redis
客户端
*
@
param
channelTopic
频道名称
*/
public
RedisPublisher
(
RedisOperations
<?
extends
Object
,
?
extends
Object
>
redisOperations
,
ChannelTopic
channelTopic
)
{
this
.
channelTopic
=
channelTopic
;
this
.
redisOperations
=
redisOperations
;
}
/**
*
发布消息到频道(
Channel
)
*
*
@
param
message
消息内容
*/
public
void
publisher
(
Object
message
)
{
redisOperations
.
convertAndSend
(
channelTopic
.
toString
(),
message
);
logger
.
debug
(
"redis消息发布者向频道【{}】发布了【{}】消息"
,
channelTopic
.
toString
(),
message
.
toString
());
}
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/redis/CustomizedRedisCache.java.ftl
0 → 100644
浏览文件 @
fcab7ff0
<#
ibiztemplate
>
TARGET
=
PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
redis
;
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
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
0 → 100644
浏览文件 @
fcab7ff0
<#
ibiztemplate
>
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
>
{
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
0 → 100644
浏览文件 @
fcab7ff0
<#
ibiztemplate
>
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
;
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
0 → 100644
浏览文件 @
fcab7ff0
<#
ibiztemplate
>
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
;
@
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
0 → 100644
浏览文件 @
fcab7ff0
<#
ibiztemplate
>
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
;
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/cache/setting/FirstCacheSetting.java.ftl
0 → 100644
浏览文件 @
fcab7ff0
<#
ibiztemplate
>
TARGET
=
PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
setting
;
import
com
.
github
.
benmanes
.
caffeine
.
cache
.
CaffeineSpec
;
public
class
FirstCacheSetting
{
/**
*
一级缓存配置,配置项请点击这里
{@
link
CaffeineSpec
#
configure
(
String
,
String
)}
*
@
param
cacheSpecification
*/
public
FirstCacheSetting
(
String
cacheSpecification
)
{
this
.
cacheSpecification
=
cacheSpecification
;
}
private
String
cacheSpecification
;
public
String
getCacheSpecification
()
{
return
cacheSpecification
;
}
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/cache/setting/SecondaryCacheSetting.java.ftl
0 → 100644
浏览文件 @
fcab7ff0
<#
ibiztemplate
>
TARGET
=
PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
cache
.
setting
;
public
class
SecondaryCacheSetting
{
/**
*
@
param
expirationSecondTime
设置
redis
缓存的有效时间,单位秒
*
@
param
preloadSecondTime
设置
redis
缓存的自动刷新时间,单位秒
*/
public
SecondaryCacheSetting
(
long
expirationSecondTime
,
long
preloadSecondTime
)
{
this
.
expirationSecondTime
=
expirationSecondTime
;
this
.
preloadSecondTime
=
preloadSecondTime
;
}
/**
*
@
param
usedFirstCache
是否启用一级缓存,默认
true
*
@
param
expirationSecondTime
设置
redis
缓存的有效时间,单位秒
*
@
param
preloadSecondTime
设置
redis
缓存的自动刷新时间,单位秒
*/
public
SecondaryCacheSetting
(
boolean
usedFirstCache
,
long
expirationSecondTime
,
long
preloadSecondTime
)
{
this
.
expirationSecondTime
=
expirationSecondTime
;
this
.
preloadSecondTime
=
preloadSecondTime
;
this
.
usedFirstCache
=
usedFirstCache
;
}
/**
*
@
param
expirationSecondTime
设置
redis
缓存的有效时间,单位秒
*
@
param
preloadSecondTime
设置
redis
缓存的自动刷新时间,单位秒
*
@
param
forceRefresh
是否使用强制刷新(走数据库),默认
false
*/
public
SecondaryCacheSetting
(
long
expirationSecondTime
,
long
preloadSecondTime
,
boolean
forceRefresh
)
{
this
.
expirationSecondTime
=
expirationSecondTime
;
this
.
preloadSecondTime
=
preloadSecondTime
;
this
.
forceRefresh
=
forceRefresh
;
}
/**
*
@
param
expirationSecondTime
设置
redis
缓存的有效时间,单位秒
*
@
param
preloadSecondTime
设置
redis
缓存的自动刷新时间,单位秒
*
@
param
usedFirstCache
是否启用一级缓存,默认
true
*
@
param
forceRefresh
是否使用强制刷新(走数据库),默认
false
*/
public
SecondaryCacheSetting
(
long
expirationSecondTime
,
long
preloadSecondTime
,
boolean
usedFirstCache
,
boolean
forceRefresh
)
{
this
.
expirationSecondTime
=
expirationSecondTime
;
this
.
preloadSecondTime
=
preloadSecondTime
;
this
.
usedFirstCache
=
usedFirstCache
;
this
.
forceRefresh
=
forceRefresh
;
}
/**
*
缓存有效时间
*/
private
long
expirationSecondTime
;
/**
*
缓存主动在失效前强制刷新缓存的时间
*
单位:秒
*/
private
long
preloadSecondTime
=
0
;
/**
*
是否使用一级缓存,默认是
true
*/
private
boolean
usedFirstCache
=
true
;
/**
*
是否使用强刷新(走数据库),默认是
false
*/
private
boolean
forceRefresh
=
false
;
public
long
getPreloadSecondTime
()
{
return
preloadSecondTime
;
}
public
long
getExpirationSecondTime
()
{
return
expirationSecondTime
;
}
public
boolean
getUsedFirstCache
()
{
return
usedFirstCache
;
}
public
boolean
getForceRefresh
()
{
return
forceRefresh
;
}
}
SLN/%PUBPRJ%-util/src/main/java/%SYS_PKGPATH%/util/enums/RedisChannelTopic.java.ftl
0 → 100644
浏览文件 @
fcab7ff0
<#
ibiztemplate
>
TARGET
=
PSSYSTEM
</#
ibiztemplate
>
package
${
pub
.
getPKGCodeName
()}.
util
.
enums
;
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缓存消息频道"
);
String
channelTopic
;
String
label
;
RedisChannelTopic
(
String
channelTopic
,
String
label
)
{
this
.
channelTopic
=
channelTopic
;
this
.
label
=
label
;
}
public
ChannelTopic
getChannelTopic
()
{
return
new
ChannelTopic
(
channelTopic
);
}
public
static
RedisChannelTopic
getChannelTopicEnum
(
String
channelTopic
)
{
for
(
RedisChannelTopic
awardTypeEnum
:
RedisChannelTopic
.
values
())
{
if
(
awardTypeEnum
.
getChannelTopicStr
().
equals
(
channelTopic
))
{
return
awardTypeEnum
;
}
}
return
null
;
}
public
String
getChannelTopicStr
()
{
return
channelTopic
;
}
}
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录