Skip to content

Commit

Permalink
refactor: redis 관련 클래스 리팩토링 (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
choidongkuen committed Jan 22, 2024
1 parent 2739bea commit addaccc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/main/java/net/teumteum/core/config/RedisConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ public RedisConnectionFactory redisConnectionFactory() {
}

@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
public RedisTemplate<String, String> redisTemplate() {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
redisTemplate.setDefaultSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
52 changes: 47 additions & 5 deletions src/main/java/net/teumteum/core/security/service/RedisService.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,72 @@
package net.teumteum.core.security.service;

import static java.util.Objects.requireNonNull;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import java.io.IOException;
import java.time.Duration;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import net.teumteum.teum_teum.domain.UserLocation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class RedisService {

private final RedisTemplate<String, Object> redisTemplate;
private static final String HASH_KEY = "userLocation";
private final RedisTemplate<String, String> redisTemplate;
private final ObjectMapper objectMapper;
private ValueOperations<String, String> valueOperations;

@PostConstruct
void init() {
valueOperations = redisTemplate.opsForValue();
}


public String getData(String key) {
return (String) redisTemplate.opsForValue().get(key);
return valueOperations.get(key);
}

public void setData(String key, String value) {
redisTemplate.opsForValue().set(key, value);
valueOperations.set(key, value);
}

public void setDataWithExpiration(String key, String value, Long duration) {
Duration expireDuration = Duration.ofSeconds(duration);
redisTemplate.opsForValue().set(key, value, expireDuration);
valueOperations.set(key, value, expireDuration);
}

public void deleteData(String key) {
redisTemplate.delete(key);
valueOperations.getOperations().delete(key);
}

public void setUserLocation(UserLocation userLocation, Long duration) {
String key = HASH_KEY + userLocation.id();
String value;
try {
value = objectMapper.writeValueAsString(userLocation);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException(e);
}
valueOperations.set(key, value, duration);
}

public Set<UserLocation> getAllUserLocations() {
Set<String> keys = redisTemplate.keys(HASH_KEY + ":*");
return requireNonNull(keys).stream().map(key -> {
String value = valueOperations.get(key);
try {
return objectMapper.readValue(value, UserLocation.class);
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}).collect(Collectors.toSet());
}
}

0 comments on commit addaccc

Please sign in to comment.