1. redis를 설치해준다
https://github.com/microsoftarchive/redis/releases
2.build.gradle
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
3.application.properties
spring.redis.uri = redis://localhost:6379
4.RedisConfig.java
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.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisConnectionFactory redisConnectionFactory(){
return new LettuceConnectionFactory();
}
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
return redisTemplate;
}
}
5.사용
private final RedisTemplate<String , String > redisTemplate;
redisTemplate.opsForValue().set(key, value, timeout, timeunit); // 데이터 입력
redisTemplate.hasKey(key); // 데이터 유무
redisTemplate.delete(key); // 데이터 삭제
}
'개발일지' 카테고리의 다른 글
20231129 - UserDetails, UserDetailsService (0) | 2023.11.29 |
---|---|
20231128 - 세 번째 팀 프로젝트를 마치며 (0) | 2023.11.28 |
개발일지 25 - Jwt 검증 중 발생한 오류 (0) | 2023.11.22 |
개발일지 24 - Validation (0) | 2023.11.20 |
개발일지 23 - @PathVariable, @RequestBody (0) | 2023.11.16 |