Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yudao-cloud
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
hblj
yudao-cloud
Commits
ee735c0e
提交
ee735c0e
authored
5月 11, 2020
作者:
wuwenbin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix redis 缓存客户端,包括jedis,redisson,spring-data-redis
上级
cce62c43
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
360 行增加
和
0 行删除
+360
-0
pom.xml
common/mall-spring-boot-starter-cache/pom.xml
+37
-0
JedisClient.java
...c/main/java/cn/iocoder/mall/cache/config/JedisClient.java
+79
-0
RedissonClient.java
...ain/java/cn/iocoder/mall/cache/config/RedissonClient.java
+51
-0
SpringDataRedisConfig.java
...a/cn/iocoder/mall/cache/config/SpringDataRedisConfig.java
+165
-0
redis.properties
...ng-boot-starter-cache/src/main/resources/redis.properties
+17
-0
pom.xml
common/pom.xml
+10
-0
pom.xml
pom.xml
+1
-0
没有找到文件。
common/mall-spring-boot-starter-cache/pom.xml
0 → 100644
浏览文件 @
ee735c0e
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<parent>
<artifactId>
onemall
</artifactId>
<groupId>
cn.iocoder.mall
</groupId>
<version>
1.0-SNAPSHOT
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
mall-spring-boot-starter-cache
</artifactId>
<dependencies>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-data-redis
</artifactId>
</dependency>
<dependency>
<groupId>
org.redisson
</groupId>
<artifactId>
redisson
</artifactId>
<version>
3.10.6
</version>
</dependency>
<dependency>
<groupId>
redis.clients
</groupId>
<artifactId>
jedis
</artifactId>
<version>
3.1.0
</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
common/mall-spring-boot-starter-cache/src/main/java/cn/iocoder/mall/cache/config/JedisClient.java
0 → 100644
浏览文件 @
ee735c0e
package
cn
.
iocoder
.
mall
.
cache
.
config
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.StringUtils
;
import
redis.clients.jedis.Jedis
;
import
redis.clients.jedis.JedisSentinelPool
;
import
javax.annotation.PostConstruct
;
import
javax.annotation.Resource
;
@Component
public
class
JedisClient
{
@Resource
private
static
JedisSentinelPool
jedisSentinelPool
;
public
static
String
get
(
String
key
)
{
Jedis
jedis
=
null
;
try
{
jedis
=
jedisSentinelPool
.
getResource
();
return
jedis
.
get
(
key
);
}
catch
(
Exception
e
)
{
}
finally
{
if
(
jedis
!=
null
)
{
jedis
.
close
();
}
}
return
""
;
}
public
static
boolean
set
(
String
key
,
String
value
)
{
Jedis
jedis
=
null
;
try
{
jedis
=
jedisSentinelPool
.
getResource
();
String
ret
=
jedis
.
set
(
key
,
value
);
return
"ok"
.
equalsIgnoreCase
(
ret
);
}
catch
(
Exception
e
)
{
}
finally
{
if
(
jedis
!=
null
)
{
jedis
.
close
();
}
}
return
false
;
}
public
static
boolean
set
(
String
key
,
String
value
,
int
seconds
)
{
Jedis
jedis
=
null
;
try
{
jedis
=
jedisSentinelPool
.
getResource
();
String
ret
=
jedis
.
set
(
key
,
value
);
jedis
.
expire
(
key
,
seconds
);
return
"ok"
.
equalsIgnoreCase
(
ret
);
}
catch
(
Exception
e
)
{
}
finally
{
if
(
jedis
!=
null
)
{
jedis
.
close
();
}
}
return
false
;
}
public
static
boolean
del
(
String
key
)
{
Long
removedSize
=
0L
;
Jedis
jedis
=
null
;
try
{
jedis
=
jedisSentinelPool
.
getResource
();
removedSize
=
jedis
.
del
(
key
);
}
catch
(
Exception
e
)
{
}
finally
{
if
(
jedis
!=
null
)
{
jedis
.
close
();
}
}
return
removedSize
>
0
;
}
}
common/mall-spring-boot-starter-cache/src/main/java/cn/iocoder/mall/cache/config/RedissonClient.java
0 → 100644
浏览文件 @
ee735c0e
package
cn
.
iocoder
.
mall
.
cache
.
config
;
import
org.redisson.Redisson
;
import
org.redisson.config.Config
;
import
org.redisson.config.ReadMode
;
import
org.redisson.config.SentinelServersConfig
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
@Component
public
class
RedissonClient
{
@Value
(
"${spring.redis.database}"
)
private
int
database
;
@Value
(
"${spring.redis.sentinel.master}"
)
private
String
master
;
@Value
(
"${spring.redis.sentinel.nodes}"
)
private
String
nodes
;
/**
* 哨兵模式 redisson 客户端
* @return
*/
@Bean
public
org
.
redisson
.
api
.
RedissonClient
redissonClient
()
{
Config
config
=
new
Config
();
List
<
String
>
nodes
=
Arrays
.
asList
(
this
.
nodes
.
split
(
","
));
List
<
String
>
newNodes
=
new
ArrayList
(
nodes
.
size
());
nodes
.
forEach
((
index
)
->
newNodes
.
add
(
index
.
startsWith
(
"redis://"
)
?
index
:
"redis://"
+
index
));
SentinelServersConfig
serverConfig
=
config
.
useSentinelServers
()
.
addSentinelAddress
(
newNodes
.
toArray
(
new
String
[
3
]))
.
setMasterName
(
this
.
master
)
.
setReadMode
(
ReadMode
.
SLAVE
)
;
serverConfig
.
setDatabase
(
this
.
database
);
return
Redisson
.
create
(
config
);
}
}
common/mall-spring-boot-starter-cache/src/main/java/cn/iocoder/mall/cache/config/SpringDataRedisConfig.java
0 → 100644
浏览文件 @
ee735c0e
package
cn
.
iocoder
.
mall
.
cache
.
config
;
import
com.fasterxml.jackson.annotation.JsonAutoDetect
;
import
com.fasterxml.jackson.annotation.PropertyAccessor
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
org.redisson.Redisson
;
import
org.redisson.api.RedissonClient
;
import
org.redisson.config.Config
;
import
org.redisson.config.ReadMode
;
import
org.redisson.config.SentinelServersConfig
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.cache.CacheManager
;
import
org.springframework.cache.annotation.CachingConfigurerSupport
;
import
org.springframework.cache.annotation.EnableCaching
;
import
org.springframework.cache.interceptor.KeyGenerator
;
import
org.springframework.cache.interceptor.SimpleKeyGenerator
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.data.redis.cache.RedisCacheConfiguration
;
import
org.springframework.data.redis.cache.RedisCacheManager
;
import
org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer
;
import
org.springframework.data.redis.serializer.RedisSerializationContext
;
import
org.springframework.data.redis.serializer.RedisSerializer
;
import
org.springframework.data.redis.serializer.StringRedisSerializer
;
import
redis.clients.jedis.Jedis
;
import
java.lang.reflect.Method
;
import
java.time.Duration
;
import
java.util.*
;
import
static
com
.
fasterxml
.
jackson
.
databind
.
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
;
@Configuration
@EnableCaching
public
class
SpringDataRedisConfig
extends
CachingConfigurerSupport
{
@Value
(
"${spring.redis.database}"
)
private
int
database
;
@Value
(
"${spring.redis.sentinel.master}"
)
private
String
master
;
@Value
(
"${spring.redis.sentinel.nodes}"
)
private
String
nodes
;
private
static
RedisTemplate
redisTemplate
;
static
{
}
public
static
String
get
(
String
key
)
{
redisTemplate
.
opsForValue
().
get
(
key
);
return
""
;
}
public
static
boolean
set
(
String
key
,
String
value
)
{
redisTemplate
.
opsForValue
().
set
(
key
,
value
);
return
false
;
}
public
static
boolean
set
(
String
key
,
String
value
,
int
seconds
)
{
redisTemplate
.
opsForValue
().
set
(
key
,
value
,
seconds
);
return
false
;
}
/**
* json序列化
* @return
*/
@Bean
public
RedisSerializer
<
Object
>
jackson2JsonRedisSerializer
()
{
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
Jackson2JsonRedisSerializer
serializer
=
new
Jackson2JsonRedisSerializer
(
Object
.
class
);
ObjectMapper
mapper
=
new
ObjectMapper
();
mapper
.
setVisibility
(
PropertyAccessor
.
ALL
,
JsonAutoDetect
.
Visibility
.
ANY
);
mapper
.
enableDefaultTyping
(
ObjectMapper
.
DefaultTyping
.
NON_FINAL
);
mapper
.
configure
(
FAIL_ON_UNKNOWN_PROPERTIES
,
false
);
serializer
.
setObjectMapper
(
mapper
);
return
serializer
;
}
@Bean
public
RedisTemplate
<
String
,
Object
>
redisTemplate
(
LettuceConnectionFactory
lettuceConnectionFactory
)
{
//StringRedisTemplate的构造方法中默认设置了stringSerializer
RedisTemplate
<
String
,
Object
>
template
=
new
RedisTemplate
<>();
//set key serializer
StringRedisSerializer
stringRedisSerializer
=
new
StringRedisSerializer
();
template
.
setKeySerializer
(
stringRedisSerializer
);
template
.
setHashKeySerializer
(
stringRedisSerializer
);
//set value serializer
template
.
setDefaultSerializer
(
jackson2JsonRedisSerializer
());
template
.
setConnectionFactory
(
lettuceConnectionFactory
);
template
.
afterPropertiesSet
();
return
template
;
}
@Bean
public
StringRedisTemplate
stringRedisTemplate
(
LettuceConnectionFactory
lettuceConnectionFactory
)
{
StringRedisTemplate
template
=
new
StringRedisTemplate
();
template
.
setConnectionFactory
(
lettuceConnectionFactory
);
return
template
;
}
@Override
@Bean
public
KeyGenerator
keyGenerator
()
{
return
new
SimpleKeyGenerator
()
{
@Override
public
Object
generate
(
Object
target
,
Method
method
,
Object
...
params
)
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
target
.
getClass
().
getName
());
sb
.
append
(
"."
).
append
(
method
.
getName
());
StringBuilder
paramsSb
=
new
StringBuilder
();
for
(
Object
param
:
params
)
{
// 如果不指定,默认生成包含到键值中
if
(
param
!=
null
)
{
paramsSb
.
append
(
param
.
toString
());
}
}
if
(
paramsSb
.
length
()
>
0
)
{
sb
.
append
(
"_"
).
append
(
paramsSb
);
}
return
sb
.
toString
();
}
};
}
private
RedisCacheConfiguration
getRedisCacheConfigurationWithTtl
(
Integer
time
,
int
timeType
)
{
Duration
duration
=
Duration
.
ofMillis
(
time
);
if
(
timeType
==
1
){
//时
duration
=
Duration
.
ofHours
(
time
);
}
else
if
(
timeType
==
2
){
//分
duration
=
Duration
.
ofMinutes
(
time
);
}
else
if
(
timeType
==
3
){
//秒
duration
=
Duration
.
ofSeconds
(
time
);
}
RedisCacheConfiguration
redisCacheConfiguration
=
RedisCacheConfiguration
.
defaultCacheConfig
();
redisCacheConfiguration
=
redisCacheConfiguration
.
serializeKeysWith
(
RedisSerializationContext
.
SerializationPair
.
fromSerializer
(
new
StringRedisSerializer
()))
.
serializeValuesWith
(
RedisSerializationContext
.
SerializationPair
.
fromSerializer
(
jackson2JsonRedisSerializer
())
)
//超时时间
.
entryTtl
(
duration
);
return
redisCacheConfiguration
;
}
}
common/mall-spring-boot-starter-cache/src/main/resources/redis.properties
0 → 100644
浏览文件 @
ee735c0e
redis.pool.maxIdle
=
200
redis.pool.minIdle
=
10
#redis.pool.maxActive=600
redis.pool.maxTotal
=
1024
redis.pool.maxWaitMillis
=
3000
redis.pool.minEvictableIdleTimeMillis
=
300000
redis.pool.numTestsPerEvictionRun
=
1024
redis.pool.timeBetweenEvictionRunsMillis
=
30000
redis.pool.testOnBorrow
=
true
redis.pool.testWhileIdle
=
true
redis.pool.testOnReturn
=
true
#Redis 配置
spring.redis.sentinel.master
=
spring.redis.sentinel.nodes
=
spring.redis.database
=
common/pom.xml
浏览文件 @
ee735c0e
...
...
@@ -19,6 +19,16 @@
<module>
mall-spring-boot-starter-security
</module>
<module>
mall-spring-boot-starter-mybatis
</module>
</modules>
<dependencies>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-context
</artifactId>
</dependency>
<dependency>
<groupId>
redis.clients
</groupId>
<artifactId>
jedis
</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
...
...
pom.xml
浏览文件 @
ee735c0e
...
...
@@ -31,6 +31,7 @@
<module>
order/order-rpc
</module>
<module>
order/order-rpc-api
</module>
<module>
order-rest
</module>
<module>
mall-spring-boot-starter-cache
</module>
</modules>
<packaging>
pom
</packaging>
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论