Giriş
Şu satırı dahil ederiz
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
Eğer ayarları application.properties ile yapmak istemiyorsak bu sınıfı yaratmak gerekir.
Hem LettuceConnectionFactory hem de JedisConnectionFactory için bu sınıf gerekir
JedisConnectionFactory
Örnek
Şöyle yaparız. Bu kod ile artık RedisTemplate kullanabiliriz.
@Configuration
@EnableRedisRepositories
public class RedisConfig {
@Bean
public JedisConnectionFactory connectionFactory(){
RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
configuration.setHostName("localhost");
configuration.setPort(6379);
return new JedisConnectionFactory(configuration);
}
@Bean
@Primary
public RedisTemplate<String, Object> redisTemplate(){
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory());
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new JdkSerializationRedisSerializer());
template.setHashValueSerializer(new JdkSerializationRedisSerializer());
template.setEnableTransactionSupport(true);
template.afterPropertiesSet();
return template;
}
}
Hiç yorum yok:
Yorum Gönder