Skip to content

Commit

Permalink
feat: redis 설정
Browse files Browse the repository at this point in the history
redis를 프로젝트에 연결한다.
  • Loading branch information
kanguk01 committed Oct 3, 2024
1 parent 1782de2 commit ff11cc6
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt:0.9.1'
implementation 'org.hibernate.validator:hibernate-validator:8.0.0.Final'
implementation 'jakarta.validation:jakarta.validation-api:3.0.2'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
Expand Down
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ services:
networks:
- splanet

redis:
container_name: redis
image: redis
ports:
- 6379:6379
networks:
- splanet


# 개발할때는 주석처리하여 로컬로 개발합니다.
# springboot:
# container_name: springboot_splanet
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/splanet/splanet/config/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.splanet.splanet.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.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.data.redis.host}")
private String host;

@Value("${spring.data.redis.port}")
private int port;

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

@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());

// 일반적인 key:value의 경우 시리얼라이저
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());

// Hash를 사용할 경우 시리얼라이저
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());

// 모든 경우
redisTemplate.setDefaultSerializer(new StringRedisSerializer());

return redisTemplate;
}
}
5 changes: 5 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ spring:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
format_sql: true
data:
redis:
host: localhost # 로컬에서 실행하기 때문
port: 6379
config:
import: env.properties
security:
Expand All @@ -34,6 +38,7 @@ spring:
scope: profile_nickname, profile_image, account_email
client-name: Kakao


logging:
level:
org.springframework.security: TRACE
Expand Down

0 comments on commit ff11cc6

Please sign in to comment.