스프링
[스프링 부트] redis 초간단 사용법
MC류짱
2023. 4. 24. 20:02
레디스 설치
https://github.com/microsoftarchive/redis/releases
- 저는 3.2.100버전 설치했습니다.
- 대충 next누르면 설치됨.
- 설치 경로로 가서 redis-cli 실행하면
- ping 입력하면 PONG으로 반응해줌 레디스가 돌아가고 있음
의존성 넣어주기
// 레디스
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
application.yml
spring:
redis:
lettuce:
pool:
max-active: 10
max-idle: 10
min-idle: 2
port: 6379
host: 127.0.0.1
password:
RedisConfig
package jpabook.jpashop.config;
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.redis.host}")
private String redisHost;
@Value("${spring.redis.port}")
private String redisPort;
@Value("${spring.redis.password}")
private String redisPassword;
@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(redisHost);
redisStandaloneConfiguration.setPort(Integer.parseInt(redisPort));
redisStandaloneConfiguration.setPassword(redisPassword);
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration);
return lettuceConnectionFactory;
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
- 레디스 연결 설정해주고
- 앞으로 사용은 RedisTemplate으로 할거임
RedisController
package jpabook.jpashop.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class RedisController {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@PostMapping("/redis")
public ResponseEntity<?> addRedisKey() {
ValueOperations<String, String> vop = redisTemplate.opsForValue();
// 키: a, 값: apple
vop.set("a", "apple");
// 키: b, 해쉬 키: banana, 값: 바나나, 해쉬 키: orange, 값: 오렌지
vop.set("b", "banana");
vop.set("b:banana", "바나나");
vop.set("b:orange", "오렌지");
return new ResponseEntity<>(HttpStatus.CREATED);
}
@GetMapping("/redis")
public ResponseEntity<?> getRedisKey() {
ValueOperations<String, String> vop = redisTemplate.opsForValue();
// 키: a, 값: apple
String value = vop.get("a");
// 키: b, 해쉬 키: banana, 값: 바나나, 해쉬 키: orange, 값: 오렌지
String value2 = vop.get("b");
String value3 = vop.get("b:banana");
String value4 = vop.get("b:orange");
List<String> list = List.of(value, value2, value3, value4);
return new ResponseEntity<>(list, HttpStatus.OK);
}
}
- 위의 예시대로 a: apple도 되고,
- 해시 자료구조를 사용해서 a: {bababa: 바나나, orange: 오렌지}도 가능함
- /redis로 POST요청 보내면 레디스에 저장되고
- GET요청은 조회