Skip to content

Commit

Permalink
Merge pull request #92 from TRIP-Side-Project/dev
Browse files Browse the repository at this point in the history
Redis 설정 추가
  • Loading branch information
don9m1n authored Dec 19, 2023
2 parents 24fce02 + 090d445 commit 19cbb97
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/api/trip/common/redis/RedisConfig.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/api/trip/common/redis/RedisService.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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);
}

}
5 changes: 4 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ spring:
web:
pageable:
one-indexed-parameters: true
redis:
host: localhost
port: 6379
servlet:
multipart:
max-file-size: 20MB
Expand All @@ -21,4 +24,4 @@ spring:
logging:
level:
root: info
com.api.trip: debug
com.api.trip: debug

0 comments on commit 19cbb97

Please sign in to comment.