-
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.
feat($AuthCenter): enable Redis cache
- Loading branch information
Johnny Miller (锺俊)
committed
Dec 30, 2020
1 parent
22c9cfb
commit 6c14c7b
Showing
3 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
...java/com/jmsoftware/maf/authcenter/universal/configuration/RedisCachingConfiguration.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,78 @@ | ||
package com.jmsoftware.maf.authcenter.universal.configuration; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.cache.CacheManager; | ||
import org.springframework.cache.annotation.CachingConfigurerSupport; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.cache.interceptor.*; | ||
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.RedisConnectionFactory; | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.Objects; | ||
|
||
import static org.springframework.data.redis.cache.RedisCacheConfiguration.defaultCacheConfig; | ||
|
||
/** | ||
* Description: RedisCachingConfiguration, change description here. | ||
* | ||
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/30/2020 11:48 AM | ||
* @see | ||
* <a href='https://blog.csdn.net/echizao1839/article/details/102660649'>Spring boot 之 spring-boot-starter-cache (整合 Redis)</a> | ||
**/ | ||
@Slf4j | ||
@Configuration | ||
@EnableCaching | ||
@RequiredArgsConstructor | ||
public class RedisCachingConfiguration extends CachingConfigurerSupport { | ||
private final RedisConnectionFactory redisConnectionFactory; | ||
|
||
@PostConstruct | ||
private void postConstruct() { | ||
log.warn("Initial bean: {}", RedisCachingConfiguration.class.getSimpleName()); | ||
} | ||
|
||
@Bean | ||
@Override | ||
public CacheManager cacheManager() { | ||
RedisCacheConfiguration cacheConfiguration = defaultCacheConfig() | ||
.disableCachingNullValues() | ||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer( | ||
new GenericJackson2JsonRedisSerializer())); | ||
return RedisCacheManager | ||
.builder(redisConnectionFactory) | ||
.cacheDefaults(cacheConfiguration) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@Override | ||
public CacheResolver cacheResolver() { | ||
return new SimpleCacheResolver(Objects.requireNonNull(cacheManager())); | ||
} | ||
|
||
@Bean | ||
@Override | ||
public KeyGenerator keyGenerator() { | ||
return (target, method, params) -> { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
stringBuilder.append(target.getClass().getName()).append("#").append(method.getName()); | ||
for (Object param : params) { | ||
stringBuilder.append(":").append(param.toString()); | ||
} | ||
return stringBuilder.toString(); | ||
}; | ||
} | ||
|
||
@Bean | ||
@Override | ||
public CacheErrorHandler errorHandler() { | ||
return new SimpleCacheErrorHandler(); | ||
} | ||
} |
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
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