-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf($AuthCenter): customize RedisTemplate
[skip ci]
- Loading branch information
Johnny Miller (锺俊)
committed
Dec 30, 2020
1 parent
40bac8b
commit 22c9cfb
Showing
2 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...c/main/java/com/jmsoftware/maf/authcenter/universal/configuration/RedisConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.jmsoftware.maf.authcenter.universal.configuration; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.core.ReactiveRedisTemplate; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
/** | ||
* Description: RedisConfiguration, change description here. | ||
* | ||
* @author 钟俊(zhongjun), email: [email protected], date: 12/30/2020 1:08 PM | ||
**/ | ||
@Slf4j | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class RedisConfiguration { | ||
private final ObjectMapper objectMapper; | ||
|
||
/** | ||
* RedisTemplate uses JDK byte code serialization (byte[]), which is not that readable and friendly to | ||
* human reading. | ||
* <p> | ||
* In order to replace that, we have to <b>customize</b> RedisTemplate. | ||
* | ||
* @param redisConnectionFactory the redis connection factory | ||
* @return RedisTemplate redis template | ||
* @author Johnny Miller (锺俊), email: [email protected], date: 12/30/2020 1:18 PM | ||
*/ | ||
@Bean | ||
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { | ||
log.warn("Initial bean: {}", RedisTemplate.class.getSimpleName()); | ||
|
||
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
|
||
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = | ||
new Jackson2JsonRedisSerializer<>(Object.class); | ||
jackson2JsonRedisSerializer.setObjectMapper(objectMapper); | ||
|
||
// Set key serializers as StringRedisSerializer | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
// Use Jackson2JsonRedisSerialize to replace default JDK serialization | ||
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); | ||
redisTemplate.afterPropertiesSet(); | ||
return redisTemplate; | ||
} | ||
|
||
/** | ||
* Reactive redis template factory. | ||
* | ||
* @param connectionFactory the reactive redis connection factory | ||
* @return the reactive redis template | ||
* @author Johnny Miller (锺俊), email: [email protected], date: 12/30/2020 1:43 PM | ||
*/ | ||
@Bean | ||
ReactiveRedisTemplate<Object, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory connectionFactory) { | ||
log.warn("Initial bean: {}", ReactiveRedisTemplate.class.getSimpleName()); | ||
StringRedisSerializer keySerializer = new StringRedisSerializer(); | ||
Jackson2JsonRedisSerializer<Object> valueSerializer = new Jackson2JsonRedisSerializer<>(Object.class); | ||
RedisSerializationContext<Object, Object> serializationContext = RedisSerializationContext | ||
.newSerializationContext(keySerializer) | ||
.value(valueSerializer) | ||
.hashValue(valueSerializer) | ||
.build(); | ||
return new ReactiveRedisTemplate<>(connectionFactory, serializationContext); | ||
} | ||
} |