-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
๐ [Fix] ConfigureRedisAction Bean ์ถ๊ฐ (#324)
- Loading branch information
1 parent
6651896
commit ea9b25c
Showing
1 changed file
with
82 additions
and
41 deletions.
There are no files selected for viewing
123 changes: 82 additions & 41 deletions
123
src/main/java/com/gg/server/global/config/RedisConfig.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 |
---|---|---|
@@ -1,76 +1,117 @@ | ||
package com.gg.server.global.config; | ||
|
||
import java.time.Duration; | ||
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.RedisCacheConfiguration; | ||
import org.springframework.data.redis.cache.RedisCacheManager; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.ListOperations; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
import org.springframework.jdbc.datasource.DataSourceTransactionManager; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import org.springframework.session.data.redis.config.ConfigureRedisAction; | ||
import org.springframework.transaction.annotation.EnableTransactionManagement; | ||
|
||
import javax.sql.DataSource; | ||
import java.sql.SQLException; | ||
import java.time.Duration; | ||
|
||
/** | ||
* Redis ์ค์ | ||
*/ | ||
@Configuration | ||
@EnableCaching | ||
@EnableRedisRepositories | ||
@EnableTransactionManagement | ||
public class RedisConfig { | ||
|
||
@Value("${spring.redis.host}") | ||
private String host; | ||
/** | ||
* Redis Host | ||
*/ | ||
@Value("${spring.redis.host}") | ||
private String host; | ||
|
||
/** | ||
* Redis Port | ||
*/ | ||
@Value("${spring.redis.port}") | ||
private int port; | ||
|
||
/** | ||
* Redis Connection Factory | ||
* <p> | ||
* redisConnectionFactory() ๋ฉ์๋๋ฅผ ํตํด RedisConnectionFactory ๋ฅผ ์์ฑํ๊ณ , ์ด๋ฅผ ํตํด RedisTemplate ๋ฅผ ์์ฑํ๋ค. ํด๋น | ||
* ๊ธฐ๋ฅ์ Spring Boot ์์ ์๋์ผ๋ก ์ ๊ณตํด์ฃผ์ง๋ง, RedisConnectionFactory ๋ฅผ ์ปค์คํฐ๋ง์ด์ง ํ๊ธฐ ์ํด ์ง์ ์์ฑ | ||
* | ||
* @return | ||
*/ | ||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(host, port); | ||
} | ||
|
||
@Value("${spring.redis.port}") | ||
private int port; | ||
/** | ||
* ์บ์ ๊ด๋ฆฌ์ | ||
* <p> | ||
* RedisCacheManager ๋ฅผ ํตํด Redis ์ ๋ํ ์บ์๋ฅผ ๊ด๋ฆฌํ๋ค. ์บ์์ ๊ธฐ๋ณธ ์ค์ ์ ๋ณ๊ฒฝํ๊ธฐ ์ํด RedisCacheConfiguration ์ ์ฌ์ฉํ๋ค. | ||
* | ||
* @param connectionFactory | ||
* @return | ||
*/ | ||
@Bean | ||
public CacheManager gameCacheManager(RedisConnectionFactory connectionFactory) { | ||
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory( | ||
redisConnectionFactory()); | ||
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig() | ||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer( | ||
new GenericJackson2JsonRedisSerializer())) // Value Serializer ๋ณ๊ฒฝ | ||
.entryTtl(Duration.ofMinutes(30)); // ์บ์ ์๋ช | ||
builder.cacheDefaults(configuration); | ||
return builder.build(); | ||
} | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(host, port); | ||
} | ||
|
||
@Bean | ||
public CacheManager gameCacheManager(RedisConnectionFactory connectionFactory) { | ||
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory()); | ||
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig() | ||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) // Value Serializer ๋ณ๊ฒฝ | ||
.entryTtl(Duration.ofMinutes(30)); // ์บ์ ์๋ช | ||
builder.cacheDefaults(configuration); | ||
return builder.build(); | ||
} | ||
@Bean | ||
public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory redisConnectionFactory) { | ||
final RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); | ||
|
||
@Bean | ||
public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory redisConnectionFactory){ | ||
final RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); | ||
|
||
redisTemplate.setConnectionFactory(redisConnectionFactory); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); | ||
redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer()); | ||
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); | ||
redisTemplate.setEnableTransactionSupport(true); // <= | ||
|
||
redisTemplate.setHashKeySerializer(new GenericJackson2JsonRedisSerializer()); | ||
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); | ||
redisTemplate.setEnableTransactionSupport(true); // <= | ||
return redisTemplate; | ||
} | ||
|
||
return redisTemplate; | ||
} | ||
/** | ||
* StringRedisTemplate | ||
* <p> | ||
* StringRedisTemplate ์ ํตํด Redis ์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๋ค. | ||
* | ||
* @param redisTemplate | ||
* @return | ||
*/ | ||
@Bean | ||
public ListOperations<?, ?> redisListTemplate(RedisTemplate<?, ?> redisTemplate) { | ||
return redisTemplate.opsForList(); | ||
} | ||
|
||
@Bean | ||
public ListOperations<?, ?> redisListTemplate(RedisTemplate<?, ?> redisTemplate) { | ||
return redisTemplate.opsForList(); | ||
} | ||
/** | ||
* ConfigureRedisAction | ||
* <p> | ||
* Elasticache๋ฅผ ์ฌ์ฉํ ๋, CONFIG ๋ช ๋ น์ด ์ฌ์ฉ์ด ์ ํ๋จ์ผ๋ก ์ธํด ๋ฐ์ํ๋ ์๋ฌ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด ์ฌ์ฉ | ||
* | ||
* @return ConfigureRedisAction | ||
*/ | ||
@Bean | ||
public ConfigureRedisAction configureRedisAction() { | ||
return ConfigureRedisAction.NO_OP; | ||
} | ||
|
||
} |