2017년 3월 31일 금요일

Make redis client by using lettuce

You can use java redis client by using lettuce.

- Execution file

package org.blog.test;

import com.lambdaworks.redis.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.blog.test.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@Slf4j
public class LettuceApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(LettuceApplication.class, args);
    }

    @Bean
    public RedisClient redisClient() {
        return RedisClient.create("redis://localhost");
    }

    @Autowired
    private RedisService redisService;

    @Override
    public void run(String... args) throws Exception {
        log.info("redisService.ping() : {}", redisService.ping());
        log.info("redisService.set(\"key\", \"hello redis~!!\") : {}", redisService.set("key", "hello redis~!!"));
        log.info("redisService.get(\"key\") : {}", redisService.get("key"));
    }
}


- redisService

package org.blog.test.service;

import com.lambdaworks.redis.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisClient redisClient;

    @Override
    public String ping() {
        return redisClient.connect().sync().ping();
    }

    @Override
    public String get(String key) {
        return redisClient.connect().sync().get(key);
    }

    @Override
    public String set(String key, String value) {
        return redisClient.connect().sync().set(key, value);
    }
}

- output

2017-03-31 21:28:30.035  INFO 4376 --- [           main] org.blog.test.LettuceApplication         : redisService.ping() : PONG
2017-03-31 21:28:30.059  INFO 4376 --- [           main] org.blog.test.LettuceApplication         : redisService.set("key", "hello redis~!!") : OK
2017-03-31 21:28:30.080  INFO 4376 --- [           main] org.blog.test.LettuceApplication         : redisService.get("key") : hello redis~!!

댓글 없음 :

댓글 쓰기