-
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.
- Loading branch information
1 parent
784c7f1
commit 19f9f2d
Showing
6 changed files
with
87 additions
and
8 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
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
6 changes: 6 additions & 0 deletions
6
app/backend/src/main/java/br/com/streampix/app/models/GetRedis.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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package br.com.streampix.app.models; | ||
|
||
public record GetRedis( | ||
String nickname | ||
) { | ||
} |
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
48 changes: 48 additions & 0 deletions
48
app/backend/src/main/java/br/com/streampix/app/services/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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package br.com.streampix.app.services; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class RedisService { | ||
|
||
private final RedisTemplate<String, String> redisTemplate; | ||
private final ObjectMapper objectMapper; | ||
|
||
public <T> void put(String key, T model) throws JsonProcessingException { | ||
String modelJson = objectMapper.writeValueAsString(model); | ||
redisTemplate.opsForValue().set(key, modelJson); | ||
} | ||
|
||
public <T> T get(String key, Class<T> clazz) throws JsonProcessingException { | ||
String modelJson = redisTemplate.opsForValue().get(key); | ||
if (modelJson != null) { | ||
return objectMapper.readValue(modelJson, clazz); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
public <T> List<T> findByAttribute(String prefix, String attributeName, String attributeValue, Class<T> clazz) throws JsonProcessingException, JsonMappingException { | ||
List<T> models = new ArrayList<>(); | ||
for (String key : Objects.requireNonNull(redisTemplate.keys(prefix + "*"))) { | ||
String modelJson = redisTemplate.opsForValue().get(key); | ||
if (modelJson != null) { | ||
T model = objectMapper.readValue(modelJson, clazz); | ||
String attributeJsonValue = objectMapper.convertValue(model, Map.class).get(attributeName).toString(); | ||
if (attributeValue.equals(attributeJsonValue)) { | ||
models.add(model); | ||
} | ||
} | ||
} | ||
return models; | ||
} | ||
} |