의존성 추가

환경 설정

package com.ssafy.backend.radis;

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.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@EnableRedisRepositories
public class RedisConfig {
    @Value("${spring.redis.host}")
    private String host; // application.yml 에서 redis의 host 값

    @Value("${spring.redis.port}")
    private int port; // application.yml에서 radis의 port 값

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory(host, port);
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate() {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}
spring:
	redis:
		host: ??
		port: 6379
메소드 설명
opsForValue Strings를 쉽게 serialize/deserialize 해주는 interface
opsForList List를 쉽게 Serialize/deserialize 해주는 interface
opsForSet Set을 쉽게 serialize/deserialize 해주는 interface
opsForZSet ZSet을 쉽게 serialize/deserialize 해주는 interface
opsForHash Hash를 쉽게 Serialize/Deserialize 해주는 interface
delete(key) key를 삭제