diff --git a/build.gradle b/build.gradle index 1c0c1b5..b8f3764 100644 --- a/build.gradle +++ b/build.gradle @@ -52,6 +52,13 @@ dependencies { runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.5' runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.5' + // redis + implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-redis', version: '2.4.10' + + // Embedded Redis + testImplementation ('it.ozimov:embedded-redis:0.7.2') { exclude group: "org.slf4j", module: "slf4j-simple" } + implementation group: 'it.ozimov', name: 'embedded-redis', version: '0.7.2' + runtimeOnly 'com.mysql:mysql-connector-j' runtimeOnly 'com.h2database:h2' diff --git a/src/main/java/com/api/trip/common/redis/RedisConfig.java b/src/main/java/com/api/trip/common/redis/RedisConfig.java new file mode 100644 index 0000000..f3bf51e --- /dev/null +++ b/src/main/java/com/api/trip/common/redis/RedisConfig.java @@ -0,0 +1,42 @@ +package com.api.trip.common.redis; + +import lombok.RequiredArgsConstructor; +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.RedisStandaloneConfiguration; +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.GenericToStringSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +@RequiredArgsConstructor +@Configuration +@EnableRedisRepositories +public class RedisConfig { + + @Value("${spring.data.redis.host}") + private String redisHost; + + @Value("${spring.data.redis.port}") + private int redisPort; + + @Bean + public LettuceConnectionFactory redisConnectionFactory() { + RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); + config.setHostName(redisHost); + config.setPort(redisPort); + return new LettuceConnectionFactory(config); + } + + @Bean + public RedisTemplate redisTemplate() { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(redisConnectionFactory()); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new GenericToStringSerializer(Long.class)); + redisTemplate.setValueSerializer(new StringRedisSerializer()); + return redisTemplate; + } +} \ No newline at end of file diff --git a/src/main/java/com/api/trip/common/redis/RedisService.java b/src/main/java/com/api/trip/common/redis/RedisService.java new file mode 100644 index 0000000..b529e18 --- /dev/null +++ b/src/main/java/com/api/trip/common/redis/RedisService.java @@ -0,0 +1,37 @@ +package com.api.trip.common.redis; + + +import jakarta.annotation.PostConstruct; +import lombok.RequiredArgsConstructor; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@RequiredArgsConstructor +@Service +public class RedisService { + + private final RedisTemplate redisTemplate; + + + public void setData(String key, Object value, Long time, TimeUnit timeUnit) { + redisTemplate.opsForValue().set(key, value.toString(), time, timeUnit); + } + + public Object getData(String key) { + return redisTemplate.opsForValue().get(key); + } + + + public void deleteData(String key) { + redisTemplate.delete(key); + } + + + public void increaseData(String key) { + redisTemplate.opsForValue().increment(key); + } + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 366d765..085b034 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -13,6 +13,9 @@ spring: web: pageable: one-indexed-parameters: true + redis: + host: localhost + port: 6379 servlet: multipart: max-file-size: 20MB @@ -21,4 +24,4 @@ spring: logging: level: root: info - com.api.trip: debug \ No newline at end of file + com.api.trip: debug