본문 바로가기
ops/other

springboot에 redis 설정하기

by seohan1010 2025. 12. 6.

-build.gradle에 추가한 의존성 

     // redis 설정 
        //compileOnly 'io.lettuce:lettuce-core:6.7.1.RELEASE'
        // https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis
        //implementation 'org.springframework.data:spring-data-redis:4.0.0'      
        //lettuce 포함 
        implementation 'org.springframework.boot:spring-boot-starter-data-redis'


-Redis Configuration 파일 

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.cache.RedisCacheManager; 


@Configuration
class RedisConfig {

  @Bean
  LettuceConnectionFactory connectionFactory() {
    return new LettuceConnectionFactory();
  }

  @Bean
  RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {

    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);
    return template;
  }


  @Bean
  public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory){
    return RedisCacheManager.create(connectionFactory);
  }


}

 

 

-Repository 설정 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class ChatMessageRepository {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void save(String key, String value) {
        redisTemplate.opsForValue().set(key, value); // Key-Value 저장
    }

    public String findByKey(String key) {
        return (String) redisTemplate.opsForValue().get(key); // Key로 값 조회
    }

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



-테스트 컨트롤러 

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import java.util.HashMap;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import cohttp://m.example.demo.repository.ChatMessageRepository;
import org.springframework.data.redis.core.RedisTemplate;


@CrossOrigin(origins = "*",
             methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS},
             allowCredentials = "false" )

@RestController
public class Main{


    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


  
    @RequestMapping(value="/",method= RequestMethod.GET)
    public String  main(HttpServletRequest req  ){
              Map<String, String> map = new HashMap<>();
             
            save("data","1");
            System.out.println (findByKey("data"));      
   return "";

    }

        public void save(String key, String value) {
        redisTemplate.opsForValue().set(key, value); // Key-Value 저장
    }

    public String findByKey(String key) {
        return (String) redisTemplate.opsForValue().get(key); // Key로 값 조회
    }

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



}



https://docs.spring.io/spring-data/redis/reference/redis/template.html

 

Working with Objects through RedisTemplate :: Spring Data Redis

Since it is quite common for the keys and values stored in Redis to be java.lang.String, the Redis modules provides two extensions to RedisConnection and RedisTemplate, respectively the StringRedisConnection (and its DefaultStringRedisConnection implementa

docs.spring.io

 

 

https://docs.spring.io/spring-data/redis/reference/redis/redis-cache.html

 

Redis Cache :: Spring Data Redis

In order to achieve true time-to-idle (TTI) expiration-like behavior in your Spring Data Redis application, then an entry must be consistently accessed with (TTL) expiration on every read or write operation. There are no exceptions to this rule. If you are

docs.spring.io

 

'ops > other' 카테고리의 다른 글

swagger & OpenAPI  (0) 2025.12.06
redis 테스트  (0) 2025.12.06
redis 키 만료 시간 설정 하기  (0) 2025.12.06
redis 설치하기  (0) 2025.12.06
redis  (0) 2025.12.06