2017년 8월 12일 토요일

Spring Redis Setting

cache memory중 하나인 redis를 사용하기 위해서, 여러가지 client를 사용할 수 있지만

to use the redis as cache memory, there are a lot of clients,
however, I'm going to use lettuce.

First of all, you can add lettuce as dependency to your project as below.

compile group: 'biz.paluch.redis', name: 'lettuce', version: '4.4.0.Final'

And then, you can set the redisClient as bean.
Done~!!

@Configuration
public class LettuceConfig {

    @Bean(destroyMethod = "shutdown")
    ClientResources clientResources() {
        return DefaultClientResources.create();
    }

    @Bean(destroyMethod = "shutdown")
    RedisClient redisClient(ClientResources clientResources) {
        return RedisClient.create(clientResources, RedisURI.create("192.168.1.63", 6379));
    }

    @Bean(destroyMethod = "close")
    StatefulRedisConnection connection(RedisClient redisClient) {
        return redisClient.connect();
    }

}

By using redisClient, after setting string to redis, you can get the result as below.

2017-08-12 20:59:37.054  INFO 7564 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-08-12 20:59:37.091  INFO 7564 --- [           main] org.blog.test.LettuceApplication         : result : Hello, Redis!
2017-08-12 20:59:39.310  INFO 7564 --- [           main] org.blog.test.LettuceApplication         : Started LettuceApplication in 6.503 seconds (JVM running for 7.218)

All the example can be downloaded as below link.

https://gitlab.com/shashaka/lettuce-redis-project

댓글 없음 :

댓글 쓰기