diff --git a/build.gradle b/build.gradle index fd01d18..a65e5a2 100644 --- a/build.gradle +++ b/build.gradle @@ -19,6 +19,7 @@ dependencies { implementation 'mysql:mysql-connector-java' implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' implementation 'org.springframework.boot:spring-boot-starter-data-redis' + implementation 'io.lettuce:lettuce-core:6.1.5.RELEASE' implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.2' implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' diff --git a/src/main/java/com/mews/mews_backend/MewsBackendApplication.java b/src/main/java/com/mews/mews_backend/MewsBackendApplication.java index 8030d78..69b59bb 100644 --- a/src/main/java/com/mews/mews_backend/MewsBackendApplication.java +++ b/src/main/java/com/mews/mews_backend/MewsBackendApplication.java @@ -2,10 +2,12 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; @EnableJpaAuditing @SpringBootApplication +@EnableCaching public class MewsBackendApplication { public static void main(String[] args) { diff --git a/src/main/java/com/mews/mews_backend/global/config/RedisSearchConfig.java b/src/main/java/com/mews/mews_backend/global/config/RedisSearchConfig.java new file mode 100644 index 0000000..8fde48b --- /dev/null +++ b/src/main/java/com/mews/mews_backend/global/config/RedisSearchConfig.java @@ -0,0 +1,58 @@ +package com.mews.mews_backend.global.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cache.annotation.CachingConfigurerSupport; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheManager; +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.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializationContext; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +@Configuration +@EnableRedisRepositories +public class RedisSearchConfig extends CachingConfigurerSupport { + + @Value("${spring.redis.search.port}") + private int port; + + @Value("${spring.redis.search.host}") + private String host; + + @Bean + public RedisConnectionFactory redisConnectionFactory() { + return new LettuceConnectionFactory(host, port); + } + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){ + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(redisConnectionFactory); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(String.class)); + return redisTemplate; + } + + //레디스 캐시 + @Bean + public RedisCacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) { + RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() + .serializeKeysWith(RedisSerializationContext + .SerializationPair.fromSerializer(new StringRedisSerializer())) + .serializeValuesWith(RedisSerializationContext + .SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())); + + + return RedisCacheManager + .RedisCacheManagerBuilder + .fromConnectionFactory(redisConnectionFactory) + .cacheDefaults(redisCacheConfiguration) + .build(); + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 028e72b..41048d9 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -44,4 +44,6 @@ jwt.token-validity-in-seconds= ENC(xsVIzQpPsUo4EY+cEJhurA==) jwt.refresh-token-validity-in-seconds=ENC(20VMLHUk4uc8l3XHUvUd5w==) spring.redis.host=ENC(qyZ4dFhPZXCyAVeA905JeNiqXKoUe0Hf) -spring.redis.port=6379 \ No newline at end of file +spring.redis.port=6379 +spring.redis.search.host=ENC(qyZ4dFhPZXCyAVeA905JeNiqXKoUe0Hf) +spring.redis.search.port=6380 \ No newline at end of file