Skip to content

Commit

Permalink
refactor: RedisConfig 설정 변경 (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwooo committed Jun 7, 2024
1 parent c8944a1 commit 2994c82
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 67 deletions.
14 changes: 5 additions & 9 deletions src/main/java/com/jisungin/application/search/SearchService.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package com.jisungin.application.search;

import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Slf4j
@Service
@RequiredArgsConstructor
public class SearchService {

private final RedisTemplate<String, String> redisTemplate;

public SearchService(@Qualifier("redisTemplateSecond") RedisTemplate<String, String> redisTemplate) {
this.redisTemplate = redisTemplate;
}

public void searchKeyword(String keyword) {
ZSetOperations<String, String> zset = redisTemplate.opsForZSet();
zset.incrementScore("ranking", keyword, 1); // 점수 증가
zset.incrementScore("ranking", keyword, 1);
}

public List<String> getRankKeywords() {
Expand Down
62 changes: 4 additions & 58 deletions src/main/java/com/jisungin/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package com.jisungin.config;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
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;

@Configuration
Expand All @@ -33,35 +24,19 @@ public class RedisConfig {
@Value("${spring.data.redis.password}")
private String password;

@Primary
@Bean(name = "redisConnectionFactoryFirst")
public LettuceConnectionFactory redisConnectionFactoryFirst() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();

redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(password);
redisStandaloneConfiguration.setDatabase(0);

return new LettuceConnectionFactory(redisStandaloneConfiguration);
}

@Bean(name = "redisConnectionFactorySecond")
public LettuceConnectionFactory redisConnectionFactorySecond() {
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();

redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(password);
redisStandaloneConfiguration.setDatabase(1);

return new LettuceConnectionFactory(redisStandaloneConfiguration);
}

@Primary
@Bean(name = "redisTemplateFirst")
public RedisTemplate<String, String> redisTemplateFirst(
@Qualifier("redisConnectionFactoryFirst") LettuceConnectionFactory connectionFactory) {
@Bean
public RedisTemplate<String, String> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();

redisTemplate.setConnectionFactory(connectionFactory);
Expand All @@ -76,34 +51,5 @@ public RedisTemplate<String, String> redisTemplateFirst(
return redisTemplate;
}

@Bean(name = "redisTemplateSecond")
public RedisTemplate<String, String> redisTemplateSecond(
@Qualifier("redisConnectionFactorySecond") LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(String.class));
return redisTemplate;
}

@Bean
public RedisMessageListenerContainer redisMessageListenerContainer(
@Qualifier("redisConnectionFactoryFirst") RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
return container;
}

@Bean
public RedisCacheManager redisCacheManager(
@Qualifier("redisConnectionFactorySecond") RedisConnectionFactory connectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));

return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(connectionFactory)
.cacheDefaults(redisCacheConfiguration)
.build();
}
}

0 comments on commit 2994c82

Please sign in to comment.