-
Notifications
You must be signed in to change notification settings - Fork 0
Feat: redis caching 적용 #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/main/java/com/rabbitmqprac/config/RedisCacheConfig.java
This file contains hidden or 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,113 @@ | ||
| package com.rabbitmqprac.config; | ||
|
|
||
| import com.rabbitmqprac.global.annotation.InfraRedisConnectionFactory; | ||
| import com.rabbitmqprac.global.annotation.OidcCacheManager; | ||
| import com.rabbitmqprac.global.annotation.SecurityUserCacheManager; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.cache.CacheManager; | ||
| import org.springframework.cache.annotation.EnableCaching; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.context.annotation.Primary; | ||
| import org.springframework.data.redis.cache.CacheKeyPrefix; | ||
| 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.connection.RedisStandaloneConfiguration; | ||
| import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; | ||
| import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
| import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; | ||
| import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
| import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| @Configuration | ||
| @EnableCaching | ||
| public class RedisCacheConfig { | ||
| @Value("${spring.data.redis.host}") | ||
| private String host; | ||
| @Value("${spring.data.redis.port}") | ||
| private int port; | ||
|
|
||
| private final long defaultCacheTtlSec = 60; | ||
| private final long securityUserCacheTtlSec = 30; | ||
| private final long oidcCacheTtlDay = 3; | ||
|
|
||
| @Bean | ||
| @Primary | ||
| @InfraRedisConnectionFactory | ||
| public RedisConnectionFactory infraRedisConnectionFactory() { | ||
| RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(host, port); | ||
| LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder().build(); | ||
| return new LettuceConnectionFactory(config, clientConfig); | ||
| } | ||
|
|
||
| /** | ||
| * CacheManager를 명시하지 않을 경우 default로 사용되는 CacheManager | ||
| */ | ||
| @Bean | ||
| @Primary | ||
| public CacheManager defaultCacheManager(@InfraRedisConnectionFactory RedisConnectionFactory redisConnectionFactory) { | ||
| RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() | ||
| .disableCachingNullValues() | ||
| .computePrefixWith(CacheKeyPrefix.simple()) | ||
| .serializeKeysWith( | ||
| RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()) | ||
| ) | ||
| .serializeValuesWith( | ||
| RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()) | ||
| ) | ||
| .entryTtl(Duration.ofSeconds(defaultCacheTtlSec)); | ||
|
|
||
| return RedisCacheManager.RedisCacheManagerBuilder | ||
| .fromConnectionFactory(redisConnectionFactory) | ||
| .cacheDefaults(config) | ||
| .build(); | ||
| } | ||
|
|
||
| @Bean | ||
| @SecurityUserCacheManager | ||
| public CacheManager securityUserCacheManager(@InfraRedisConnectionFactory RedisConnectionFactory redisConnectionFactory) { | ||
| RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() | ||
| .disableCachingNullValues() | ||
| .computePrefixWith(CacheKeyPrefix.simple()) | ||
| .serializeKeysWith( | ||
| RedisSerializationContext.SerializationPair.fromSerializer( | ||
| new StringRedisSerializer() | ||
| ) | ||
| ) | ||
| .serializeValuesWith( | ||
| RedisSerializationContext.SerializationPair.fromSerializer( | ||
| new GenericJackson2JsonRedisSerializer() | ||
| ) | ||
| ) | ||
| .entryTtl(Duration.ofSeconds(securityUserCacheTtlSec)); | ||
|
|
||
| return RedisCacheManager.RedisCacheManagerBuilder | ||
| .fromConnectionFactory(redisConnectionFactory) | ||
| .cacheDefaults(config) | ||
| .build(); | ||
| } | ||
|
|
||
| @Bean | ||
| @OidcCacheManager | ||
| public CacheManager oidcCacheManager(@InfraRedisConnectionFactory RedisConnectionFactory cf) { | ||
| RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig() | ||
| .serializeKeysWith( | ||
| RedisSerializationContext.SerializationPair.fromSerializer( | ||
| new StringRedisSerializer() | ||
| )) | ||
| .serializeValuesWith( | ||
| RedisSerializationContext.SerializationPair.fromSerializer( | ||
| new GenericJackson2JsonRedisSerializer() | ||
| )) | ||
| .entryTtl(Duration.ofDays(oidcCacheTtlDay)); | ||
|
|
||
| return RedisCacheManager | ||
| .RedisCacheManagerBuilder | ||
| .fromConnectionFactory(cf) | ||
| .cacheDefaults(config) | ||
| .build(); | ||
| } | ||
| } | ||
This file contains hidden or 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
17 changes: 17 additions & 0 deletions
17
src/main/java/com/rabbitmqprac/global/annotation/InfraRedisConnectionFactory.java
This file contains hidden or 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,17 @@ | ||
| package com.rabbitmqprac.global.annotation; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD, | ||
| ElementType.TYPE, ElementType.ANNOTATION_TYPE}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Documented | ||
| @Qualifier("infraRedisConnectionFactory") | ||
| public @interface InfraRedisConnectionFactory { | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/rabbitmqprac/global/annotation/OidcCacheManager.java
This file contains hidden or 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,18 @@ | ||
| package com.rabbitmqprac.global.annotation; | ||
|
|
||
| import com.rabbitmqprac.global.consant.CacheManagerType; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD, | ||
| ElementType.TYPE, ElementType.ANNOTATION_TYPE}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Documented | ||
| @Qualifier(CacheManagerType.OIDC) | ||
| public @interface OidcCacheManager { | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/rabbitmqprac/global/annotation/SecurityUserCacheManager.java
This file contains hidden or 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,19 @@ | ||
| package com.rabbitmqprac.global.annotation; | ||
|
|
||
| import com.rabbitmqprac.global.consant.CacheManagerType; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD, | ||
| ElementType.TYPE, ElementType.ANNOTATION_TYPE}) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Documented | ||
| @Qualifier(CacheManagerType.SECURITY_USER) | ||
| public @interface SecurityUserCacheManager { | ||
| } | ||
|
|
6 changes: 6 additions & 0 deletions
6
src/main/java/com/rabbitmqprac/global/consant/CacheManagerType.java
This file contains hidden or 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,6 @@ | ||
| package com.rabbitmqprac.global.consant; | ||
lsh2613 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| public final class CacheManagerType { | ||
| public static final String SECURITY_USER = "securityUserCacheManager"; | ||
| public static final String OIDC = "oidcCacheManager"; | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.