generated from telekom/reuse-template
-
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
c858caf
commit 082a445
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
...zon-core/src/main/java/de/telekom/eni/pandora/horizon/cache/service/JsonCacheService.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,75 @@ | ||
package de.telekom.eni.pandora.horizon.cache.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.hazelcast.core.HazelcastJsonValue; | ||
import com.hazelcast.map.IMap; | ||
import de.telekom.eni.pandora.horizon.cache.util.Query; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
import static java.util.Optional.empty; | ||
import static java.util.Optional.of; | ||
|
||
@Slf4j | ||
public class JsonCacheService<T> { | ||
|
||
private final Class<T> mapClass; | ||
|
||
@Getter | ||
private final IMap<String, HazelcastJsonValue> map; | ||
|
||
private final ObjectMapper mapper; | ||
|
||
public JsonCacheService(Class<T> mapClass, IMap<String, HazelcastJsonValue> map, ObjectMapper mapper) { | ||
this.mapClass = mapClass; | ||
this.map = map; | ||
this.mapper = mapper; | ||
} | ||
|
||
public Optional<T> getByKey(String key) { | ||
var value = map.get(key); | ||
if (value != null) { | ||
try { | ||
var mappedValue = mapper.readValue(value.getValue(), mapClass); | ||
return of(mappedValue); | ||
} catch (JsonProcessingException e) { | ||
log.error("Could not map {} from hazelcast map {} to {}", key, map.getName(), mapClass.getName()); | ||
} | ||
} | ||
return empty(); | ||
} | ||
|
||
public List<T> getQuery(Query query) { | ||
var values = map.values(query.toSqlPredicate()); | ||
return values.stream().map( | ||
value -> { | ||
try { | ||
return mapper.readValue(value.getValue(), mapClass); | ||
} catch (JsonProcessingException e) { | ||
log.error("Could not map json {} from hazelcast map {} to {}", value.getValue(), map.getName(), mapClass.getName()); | ||
} | ||
return null; | ||
} | ||
).filter(Objects::nonNull).toList(); | ||
} | ||
|
||
public void set(String key, Object value) { | ||
try { | ||
var jsonValue = mapper.writeValueAsString(value); | ||
var hazelcastValue = new HazelcastJsonValue(jsonValue); | ||
map.set(key, hazelcastValue); | ||
} catch (JsonProcessingException e) { | ||
log.error("Could not set value of {} in hazelcast map {}: {}", key, map.getName(), e.getMessage()); | ||
} | ||
} | ||
|
||
public void remove(String key) { | ||
map.remove(key); | ||
} | ||
|
||
} |