-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2739bea
commit addaccc
Showing
2 changed files
with
50 additions
and
9 deletions.
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
52 changes: 47 additions & 5 deletions
52
src/main/java/net/teumteum/core/security/service/RedisService.java
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 |
---|---|---|
@@ -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()); | ||
} | ||
} |