Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zlt2000
microservices-platform
提交
a5255175
microservices-platform
项目概览
zlt2000
/
microservices-platform
大约 1 年 前同步成功
通知
16
Star
4
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
microservices-platform
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a5255175
编写于
5月 04, 2020
作者:
zlt2000
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化redis序列化代码
上级
b7d4b94b
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
53 addition
and
165 deletion
+53
-165
zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthRedisTokenStore.java
.../com/central/oauth2/common/store/AuthRedisTokenStore.java
+5
-7
zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/RedisAutoConfigure.java
...ain/java/com/central/common/redis/RedisAutoConfigure.java
+25
-15
zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/template/RedisRepository.java
...va/com/central/common/redis/template/RedisRepository.java
+23
-143
未找到文件。
zlt-commons/zlt-auth-client-spring-boot-starter/src/main/java/com/central/oauth2/common/store/AuthRedisTokenStore.java
浏览文件 @
a5255175
...
...
@@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.data.redis.connection.RedisConnectionFactory
;
import
org.springframework.data.redis.serializer.RedisSerializer
;
import
org.springframework.security.oauth2.provider.token.TokenStore
;
/**
...
...
@@ -13,17 +14,14 @@ import org.springframework.security.oauth2.provider.token.TokenStore;
*
* @author zlt
* @date 2018/7/25 9:36
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@ConditionalOnProperty
(
prefix
=
"zlt.oauth2.token.store"
,
name
=
"type"
,
havingValue
=
"redis"
,
matchIfMissing
=
true
)
public
class
AuthRedisTokenStore
{
@Autowired
private
RedisConnectionFactory
connectionFactory
;
@Autowired
private
SecurityProperties
securityProperties
;
@Bean
public
TokenStore
tokenStore
()
{
public
TokenStore
tokenStore
(
RedisConnectionFactory
connectionFactory
,
SecurityProperties
securityProperties
)
{
return
new
CustomRedisTokenStore
(
connectionFactory
,
securityProperties
);
}
}
zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/RedisAutoConfigure.java
浏览文件 @
a5255175
package
com.central.common.redis
;
import
com.central.common.redis.properties.CacheManagerProperties
;
import
com.central.common.redis.template.RedisRepository
;
import
com.central.common.redis.util.RedisObjectSerializer
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.cache.interceptor.KeyGenerator
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.data.redis.RedisProperties
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.cache.CacheManager
;
...
...
@@ -27,6 +24,9 @@ import java.util.Map;
*
* @author zlt
* @date 2018/11/6 11:02
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@EnableConfigurationProperties
({
RedisProperties
.
class
,
CacheManagerProperties
.
class
})
@EnableCaching
...
...
@@ -34,35 +34,45 @@ public class RedisAutoConfigure {
@Autowired
private
CacheManagerProperties
cacheManagerProperties
;
@Bean
public
RedisSerializer
<
String
>
redisKeySerializer
()
{
return
RedisSerializer
.
string
();
}
@Bean
public
RedisSerializer
<
Object
>
redisValueSerializer
()
{
return
RedisSerializer
.
java
();
}
/**
* RedisTemplate配置
* @param factory
*/
@Bean
public
RedisTemplate
<
String
,
Object
>
redisTemplate
(
RedisConnectionFactory
factory
)
{
public
RedisTemplate
<
String
,
Object
>
redisTemplate
(
RedisConnectionFactory
factory
,
RedisSerializer
<
String
>
redisKeySerializer
,
RedisSerializer
<
Object
>
redisValueSerializer
)
{
RedisTemplate
<
String
,
Object
>
redisTemplate
=
new
RedisTemplate
<>();
redisTemplate
.
setConnectionFactory
(
factory
);
RedisSerializer
stringSerializer
=
new
StringRedisSerializer
();
RedisSerializer
redisObjectSerializer
=
new
RedisObjectSerializer
();
redisTemplate
.
setKeySerializer
(
stringSerializer
);
redisTemplate
.
setHashKeySerializer
(
stringSerializer
);
redisTemplate
.
setValueSerializer
(
redisObjectSerializer
);
redisTemplate
.
setDefaultSerializer
(
redisValueSerializer
);
redisTemplate
.
setKeySerializer
(
redisKeySerializer
);
redisTemplate
.
setHashKeySerializer
(
redisKeySerializer
);
redisTemplate
.
afterPropertiesSet
();
return
redisTemplate
;
}
@Bean
(
name
=
"cacheManager"
)
@Primary
public
CacheManager
cacheManager
(
RedisConnectionFactory
redisConnectionFactory
)
{
RedisCacheConfiguration
difConf
=
getDefConf
().
entryTtl
(
Duration
.
ofHours
(
1
));
public
CacheManager
cacheManager
(
RedisConnectionFactory
redisConnectionFactory
,
RedisSerializer
<
String
>
redisKeySerializer
,
RedisSerializer
<
Object
>
redisValueSerializer
)
{
RedisCacheConfiguration
difConf
=
getDefConf
(
redisKeySerializer
,
redisValueSerializer
).
entryTtl
(
Duration
.
ofHours
(
1
));
//自定义的缓存过期时间配置
int
configSize
=
cacheManagerProperties
.
getConfigs
()
==
null
?
0
:
cacheManagerProperties
.
getConfigs
().
size
();
Map
<
String
,
RedisCacheConfiguration
>
redisCacheConfigurationMap
=
new
HashMap
<>(
configSize
);
if
(
configSize
>
0
)
{
cacheManagerProperties
.
getConfigs
().
forEach
(
e
->
{
RedisCacheConfiguration
conf
=
getDefConf
().
entryTtl
(
Duration
.
ofSeconds
(
e
.
getSecond
()));
RedisCacheConfiguration
conf
=
getDefConf
(
redisKeySerializer
,
redisValueSerializer
).
entryTtl
(
Duration
.
ofSeconds
(
e
.
getSecond
()));
redisCacheConfigurationMap
.
put
(
e
.
getKey
(),
conf
);
});
}
...
...
@@ -86,11 +96,11 @@ public class RedisAutoConfigure {
};
}
private
RedisCacheConfiguration
getDefConf
()
{
private
RedisCacheConfiguration
getDefConf
(
RedisSerializer
<
String
>
redisKeySerializer
,
RedisSerializer
<
Object
>
redisValueSerializer
)
{
return
RedisCacheConfiguration
.
defaultCacheConfig
()
.
disableCachingNullValues
()
.
computePrefixWith
(
cacheName
->
"cache"
.
concat
(
":"
).
concat
(
cacheName
).
concat
(
":"
))
.
serializeKeysWith
(
RedisSerializationContext
.
SerializationPair
.
fromSerializer
(
new
StringRedisSerializer
()
))
.
serializeValuesWith
(
RedisSerializationContext
.
SerializationPair
.
fromSerializer
(
new
RedisObjectSerializer
()
));
.
serializeKeysWith
(
RedisSerializationContext
.
SerializationPair
.
fromSerializer
(
redisKeySerializer
))
.
serializeValuesWith
(
RedisSerializationContext
.
SerializationPair
.
fromSerializer
(
redisValueSerializer
));
}
}
zlt-commons/zlt-redis-spring-boot-starter/src/main/java/com/central/common/redis/template/RedisRepository.java
浏览文件 @
a5255175
package
com.central.common.redis.template
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.springframework.data.redis.connection.RedisClusterNode
;
import
org.springframework.data.redis.connection.RedisConnectionFactory
;
import
org.springframework.data.redis.connection.RedisServerCommands
;
...
...
@@ -9,37 +8,21 @@ import org.springframework.data.redis.core.HashOperations;
import
org.springframework.data.redis.core.ListOperations
;
import
org.springframework.data.redis.core.RedisCallback
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.serializer.JdkSerializationRedisSerializer
;
import
org.springframework.data.redis.serializer.RedisSerializer
;
import
org.springframework.data.redis.serializer.StringRedisSerializer
;
import
java.nio.charset.Charset
;
import
java.nio.charset.StandardCharsets
;
import
java.util.*
;
import
java.util.concurrent.TimeUnit
;
/**
* Redis Repository
* redis 基本操作 可扩展,基本够用了
*
* @author zlt
* <p>
* Blog: https://zlt2000.gitee.io
* Github: https://github.com/zlt2000
*/
@Slf4j
public
class
RedisRepository
{
/**
* 默认编码
*/
private
static
final
Charset
DEFAULT_CHARSET
=
StandardCharsets
.
UTF_8
;
/**
* key序列化
*/
private
static
final
StringRedisSerializer
STRING_SERIALIZER
=
new
StringRedisSerializer
();
/**
* value 序列化
*/
private
static
final
JdkSerializationRedisSerializer
OBJECT_SERIALIZER
=
new
JdkSerializationRedisSerializer
();
/**
* Spring Redis Template
*/
...
...
@@ -47,8 +30,6 @@ public class RedisRepository {
public
RedisRepository
(
RedisTemplate
<
String
,
Object
>
redisTemplate
)
{
this
.
redisTemplate
=
redisTemplate
;
this
.
redisTemplate
.
setKeySerializer
(
STRING_SERIALIZER
);
this
.
redisTemplate
.
setValueSerializer
(
OBJECT_SERIALIZER
);
}
/**
...
...
@@ -84,7 +65,6 @@ public class RedisRepository {
public
void
setExpire
(
final
byte
[]
key
,
final
byte
[]
value
,
final
long
time
)
{
redisTemplate
.
execute
((
RedisCallback
<
Long
>)
connection
->
{
connection
.
setEx
(
key
,
time
,
value
);
log
.
debug
(
"[redisTemplate redis]放入 缓存 url:{} ========缓存时间为{}秒"
,
key
,
time
);
return
1L
;
});
}
...
...
@@ -97,13 +77,7 @@ public class RedisRepository {
* @param time 过期时间(单位秒)
*/
public
void
setExpire
(
final
String
key
,
final
Object
value
,
final
long
time
)
{
redisTemplate
.
execute
((
RedisCallback
<
Long
>)
connection
->
{
RedisSerializer
<
String
>
serializer
=
getRedisSerializer
();
byte
[]
keys
=
serializer
.
serialize
(
key
);
byte
[]
values
=
OBJECT_SERIALIZER
.
serialize
(
value
);
connection
.
setEx
(
keys
,
time
,
values
);
return
1L
;
});
redisTemplate
.
opsForValue
().
set
(
key
,
value
,
time
,
TimeUnit
.
SECONDS
);
}
/**
...
...
@@ -114,15 +88,9 @@ public class RedisRepository {
* @param time 过期时间(单位秒)
*/
public
void
setExpire
(
final
String
[]
keys
,
final
Object
[]
values
,
final
long
time
)
{
redisTemplate
.
execute
((
RedisCallback
<
Long
>)
connection
->
{
RedisSerializer
<
String
>
serializer
=
getRedisSerializer
();
for
(
int
i
=
0
;
i
<
keys
.
length
;
i
++)
{
byte
[]
bKeys
=
serializer
.
serialize
(
keys
[
i
]);
byte
[]
bValues
=
OBJECT_SERIALIZER
.
serialize
(
values
[
i
]);
connection
.
setEx
(
bKeys
,
time
,
bValues
);
}
return
1L
;
});
for
(
int
i
=
0
;
i
<
keys
.
length
;
i
++)
{
redisTemplate
.
opsForValue
().
set
(
keys
[
i
],
values
[
i
],
time
,
TimeUnit
.
SECONDS
);
}
}
...
...
@@ -133,15 +101,9 @@ public class RedisRepository {
* @param values the values
*/
public
void
set
(
final
String
[]
keys
,
final
Object
[]
values
)
{
redisTemplate
.
execute
((
RedisCallback
<
Long
>)
connection
->
{
RedisSerializer
<
String
>
serializer
=
getRedisSerializer
();
for
(
int
i
=
0
;
i
<
keys
.
length
;
i
++)
{
byte
[]
bKeys
=
serializer
.
serialize
(
keys
[
i
]);
byte
[]
bValues
=
OBJECT_SERIALIZER
.
serialize
(
values
[
i
]);
connection
.
set
(
bKeys
,
bValues
);
}
return
1L
;
});
for
(
int
i
=
0
;
i
<
keys
.
length
;
i
++)
{
redisTemplate
.
opsForValue
().
set
(
keys
[
i
],
values
[
i
]);
}
}
...
...
@@ -152,39 +114,9 @@ public class RedisRepository {
* @param value the value
*/
public
void
set
(
final
String
key
,
final
Object
value
)
{
redisTemplate
.
execute
((
RedisCallback
<
Long
>)
connection
->
{
RedisSerializer
<
String
>
serializer
=
getRedisSerializer
();
byte
[]
keys
=
serializer
.
serialize
(
key
);
byte
[]
values
=
OBJECT_SERIALIZER
.
serialize
(
value
);
connection
.
set
(
keys
,
values
);
log
.
debug
(
"[redisTemplate redis]放入 缓存 url:{}"
,
key
);
return
1L
;
});
}
/**
* 查询在这个时间段内即将过期的key
*
* @param key the key
* @param time the time
* @return the list
*/
public
List
<
String
>
willExpire
(
final
String
key
,
final
long
time
)
{
final
List
<
String
>
keysList
=
new
ArrayList
<>();
redisTemplate
.
execute
((
RedisCallback
<
List
<
String
>>)
connection
->
{
Set
<
String
>
keys
=
redisTemplate
.
keys
(
key
+
"*"
);
for
(
String
key1
:
keys
)
{
Long
ttl
=
connection
.
ttl
(
key1
.
getBytes
(
DEFAULT_CHARSET
));
if
(
0
<=
ttl
&&
ttl
<=
2
*
time
)
{
keysList
.
add
(
key1
);
}
}
return
keysList
;
});
return
keysList
;
redisTemplate
.
opsForValue
().
set
(
key
,
value
);
}
/**
* 查询在以keyPatten的所有 key
*
...
...
@@ -192,7 +124,7 @@ public class RedisRepository {
* @return the set
*/
public
Set
<
String
>
keys
(
final
String
keyPatten
)
{
return
redisTemplate
.
execute
((
RedisCallback
<
Set
<
String
>>)
connection
->
redisTemplate
.
keys
(
keyPatten
+
"*"
)
);
return
redisTemplate
.
keys
(
keyPatten
+
"*"
);
}
/**
...
...
@@ -202,9 +134,7 @@ public class RedisRepository {
* @return the byte [ ]
*/
public
byte
[]
get
(
final
byte
[]
key
)
{
byte
[]
result
=
redisTemplate
.
execute
((
RedisCallback
<
byte
[]>)
connection
->
connection
.
get
(
key
));
log
.
debug
(
"[redisTemplate redis]取出 缓存 url:{} "
,
key
);
return
result
;
return
redisTemplate
.
execute
((
RedisCallback
<
byte
[]>)
connection
->
connection
.
get
(
key
));
}
/**
...
...
@@ -214,39 +144,7 @@ public class RedisRepository {
* @return the string
*/
public
Object
get
(
final
String
key
)
{
Object
resultStr
=
redisTemplate
.
execute
((
RedisCallback
<
Object
>)
connection
->
{
RedisSerializer
<
String
>
serializer
=
getRedisSerializer
();
byte
[]
keys
=
serializer
.
serialize
(
key
);
byte
[]
values
=
connection
.
get
(
keys
);
return
OBJECT_SERIALIZER
.
deserialize
(
values
);
});
log
.
debug
(
"[redisTemplate redis]取出 缓存 url:{} "
,
key
);
return
resultStr
;
}
/**
* 根据key获取对象
*
* @param keyPatten the key patten
* @return the keys values
*/
public
Map
<
String
,
Object
>
getKeysValues
(
final
String
keyPatten
)
{
log
.
debug
(
"[redisTemplate redis] getValues() patten={} "
,
keyPatten
);
return
redisTemplate
.
execute
((
RedisCallback
<
Map
<
String
,
Object
>>)
connection
->
{
RedisSerializer
<
String
>
serializer
=
getRedisSerializer
();
Map
<
String
,
Object
>
maps
=
new
HashMap
<>(
16
);
Set
<
String
>
keys
=
redisTemplate
.
keys
(
keyPatten
+
"*"
);
if
(
CollectionUtils
.
isNotEmpty
(
keys
))
{
for
(
String
key
:
keys
)
{
byte
[]
bKeys
=
serializer
.
serialize
(
key
);
byte
[]
bValues
=
connection
.
get
(
bKeys
);
Object
value
=
OBJECT_SERIALIZER
.
deserialize
(
bValues
);
maps
.
put
(
key
,
value
);
}
}
return
maps
;
});
return
redisTemplate
.
opsForValue
().
get
(
key
);
}
/**
...
...
@@ -266,7 +164,6 @@ public class RedisRepository {
* @param hashValue the hash value
*/
public
void
putHashValue
(
String
key
,
String
hashKey
,
Object
hashValue
)
{
log
.
debug
(
"[redisTemplate redis] putHashValue() key={},hashKey={},hashValue={} "
,
key
,
hashKey
,
hashValue
);
opsForHash
().
put
(
key
,
hashKey
,
hashValue
);
}
...
...
@@ -278,7 +175,6 @@ public class RedisRepository {
* @return the hash values
*/
public
Object
getHashValues
(
String
key
,
String
hashKey
)
{
log
.
debug
(
"[redisTemplate redis] getHashValues() key={},hashKey={}"
,
key
,
hashKey
);
return
opsForHash
().
get
(
key
,
hashKey
);
}
...
...
@@ -289,7 +185,6 @@ public class RedisRepository {
* @param hashKeys the hash keys
*/
public
void
delHashValues
(
String
key
,
Object
...
hashKeys
)
{
log
.
debug
(
"[redisTemplate redis] delHashValues() key={}"
,
key
);
opsForHash
().
delete
(
key
,
hashKeys
);
}
...
...
@@ -300,7 +195,6 @@ public class RedisRepository {
* @return the hash value
*/
public
Map
<
String
,
Object
>
getHashValue
(
String
key
)
{
log
.
debug
(
"[redisTemplate redis] getHashValue() key={}"
,
key
);
return
opsForHash
().
entries
(
key
);
}
...
...
@@ -342,7 +236,7 @@ public class RedisRepository {
* @return the boolean
*/
public
boolean
exists
(
final
String
key
)
{
return
redisTemplate
.
execute
((
RedisCallback
<
Boolean
>)
connection
->
connection
.
exists
(
key
.
getBytes
(
DEFAULT_CHARSET
))
);
return
redisTemplate
.
hasKey
(
key
);
}
...
...
@@ -352,23 +246,12 @@ public class RedisRepository {
* @param keys the keys
* @return the long
*/
public
long
del
(
final
String
...
keys
)
{
return
redisTemplate
.
execute
((
RedisCallback
<
Long
>)
connection
->
{
long
result
=
0
;
for
(
String
key
:
keys
)
{
result
=
connection
.
del
(
key
.
getBytes
(
DEFAULT_CHARSET
));
}
return
result
;
});
}
/**
* 获取 RedisSerializer
*
* @return the redis serializer
*/
protected
RedisSerializer
<
String
>
getRedisSerializer
()
{
return
redisTemplate
.
getStringSerializer
();
public
boolean
del
(
final
String
...
keys
)
{
boolean
result
=
false
;
for
(
String
key
:
keys
)
{
result
=
redisTemplate
.
delete
(
key
);
}
return
result
;
}
/**
...
...
@@ -378,10 +261,7 @@ public class RedisRepository {
* @return the long
*/
public
long
incr
(
final
String
key
)
{
return
redisTemplate
.
execute
((
RedisCallback
<
Long
>)
connection
->
{
RedisSerializer
<
String
>
redisSerializer
=
getRedisSerializer
();
return
connection
.
incr
(
redisSerializer
.
serialize
(
key
));
});
return
redisTemplate
.
opsForValue
().
increment
(
key
);
}
/**
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录