-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from TRIP-Side-Project/dev
Redis 설정 추가
- Loading branch information
Showing
4 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters