레디스 공식 홈페이지
// redis 이미지 불러오기
docker pull redis
// 불러온 이미지를 통해 redis docker 서버 실행
docker run --name myredis -d -p 6379:6379 redis
// redis 연결 확인
docker run -it --link myredis:redis --rm redis redis-cli -h redis -p 6379
// cmd에서 redis와 연결
docker run --network host -it --rm redis redis-cli -p 6379
//연결 후
set keyTest valueTest
get keyTest
Spring Boot Redis 설정
<!-- pom.xml dependency 추가 -->
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
application.yml 설정 추가
spring:
redis:
lettuce:
pool:
max-active: 10
max-idle: 10
min-idle: 2
port: 6379
host: 127.0.0.1
password: 'abcde'
package com.web.curation.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.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisConnectionFactory redisConnectionFactory() {
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();
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;
}
@Bean
public StringRedisTemplate stringRedisTemplate() {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
stringRedisTemplate.setValueSerializer(new StringRedisSerializer());
stringRedisTemplate.setConnectionFactory(redisConnectionFactory());
return stringRedisTemplate;
}
}
package com.web.curation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
@Service
public class RedisSampleService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void getRedisStringValue(String key) {
ValueOperations<String, String> stringValueOperations = stringRedisTemplate.opsForValue();
System.out.println("Redis key : " + key);
System.out.println("Redis value : " + stringValueOperations.get(key));
}
}
package com.web.curation.controller.Redis;
import com.web.curation.Service.RedisSampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RedisSampleController {
@Autowired
private RedisSampleService redisSampleService;
@PostMapping(value = "/getRedisStringValue")
public void getRedisStringValue(String key) {
redisSampleService.getRedisStringValue(key);
}
}
b. set 테스트
콘솔창에 정상 출력 확인 시
Redis를 이용한 캐시메모리 사용 가능