Last active
January 10, 2018 06:54
-
-
Save jeonguk/0f58f2c03ca9b7659a0f1bff81b2666e to your computer and use it in GitHub Desktop.
Spring - Embeded Redis configuration
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
package com.jeonguk.web.config; | |
import static java.util.stream.Collectors.joining; | |
import javax.annotation.PostConstruct; | |
import javax.annotation.PreDestroy; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; | |
import org.springframework.data.redis.core.RedisTemplate; | |
import lombok.extern.slf4j.Slf4j; | |
import redis.clients.jedis.Protocol; | |
import redis.embedded.RedisServer; | |
@Slf4j | |
@Configuration | |
public class RedisConfig { | |
@Bean | |
public RedisServer redisServer() { | |
RedisServer.builder().reset(); | |
return RedisServer.builder().port(Protocol.DEFAULT_PORT).build(); | |
} | |
@Bean | |
JedisConnectionFactory jedisConnectionFactory() { | |
return new JedisConnectionFactory(); | |
} | |
@Bean | |
public RedisTemplate<String, Object> redisTemplate() { | |
RedisTemplate<String, Object> template = new RedisTemplate<>(); | |
template.setConnectionFactory(jedisConnectionFactory()); | |
return template; | |
} | |
@PostConstruct | |
public void start() { | |
log.info("starting redis..."); | |
if (!redisServer().isActive()) redisServer().start(); | |
log.info("redis listen ports: {}", redisServer().ports().stream() | |
.map(Object::toString).collect(joining(","))); | |
} | |
@PreDestroy | |
public void stop() { | |
log.info("shutting down redis..."); | |
redisServer().stop(); | |
log.info("bye!"); | |
} | |
} |
Author
jeonguk
commented
Jan 7, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment