Redis + springboot(gradle) 연동

Web Development assistant·2024년 5월 17일
0

docker

목록 보기
5/5

이전 글 : redis + docker 설치 방법 Click!

spring boot 환경 설정

build.gradle

implementation 'org.springframework.boot:spring-boot-starter-data-redis'

application.yml

spring:
  data:
   redis:
    host: localhost
    port: 6379
    password: 123

RedisConfig.java

package com.project.myblog.config.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
	@Value("${spring.data.redis.host}")
	private String host;

	@Value("${spring.data.redis.port}")
	private int port;
	
	@Value("${spring.data.redis.password}")
	private String password;

	@Bean
	public RedisConnectionFactory redisConnectionFactory() {
		RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration();
        redisConfiguration.setHostName(host);
        redisConfiguration.setPort(port);
        redisConfiguration.setPassword(password);
		return new LettuceConnectionFactory(redisConfiguration);
	}

	@Bean
	public RedisTemplate<String, Object> redisTemplate() {
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(redisConnectionFactory());

		// 일반적인 key:value의 경우 시리얼라이저
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(new StringRedisSerializer());

		// Hash를 사용할 경우 시리얼라이저
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashValueSerializer(new StringRedisSerializer());

		// 모든 경우
		redisTemplate.setDefaultSerializer(new StringRedisSerializer());

		return redisTemplate;
	}

}

RedisUtils.java(RedisTemplate)

package com.project.myblog.util;

import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisUtils {
	private final RedisTemplate<String, Object> redisTemplate;

	public RedisUtils(RedisTemplate<String, Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	public void setData(String key, String value, Long expiredTime) {
		redisTemplate.opsForValue().set(key, value, expiredTime, TimeUnit.MILLISECONDS);
	}

	public String getData(String key) {
		return (String) redisTemplate.opsForValue().get(key);
	}

	public void deleteData(String key) {
		redisTemplate.delete(key);
	}
}

사용 예제

@Controller
@RequiredArgsConstructor
public class BoardController {
	private final BoardService boardService;
	private final RedisUtils redis;
 
	@GetMapping("/")
	public String index(Model model, @PageableDefault(size = 3, direction = Direction.DESC) Pageable pageable) {
		redis.setData("name", "jiseong", 32321312312L);	//Long expiredTime

		System.out.println(redis.getData("name"));
		
		Page<Board> page = boardService.getList(pageable);
		model.addAttribute("boards", page.getContent());
		return "index";
	}
}

docker redis check

스프링 서버에서 등록한 내 이름 키 밸류 값이 docker - redis 정상적으로 확인 된다!

0개의 댓글