package com.oransc.rappmanager.models.cache;
-import com.oransc.rappmanager.models.Rapp;
-import com.oransc.rappmanager.models.statemachine.RappStateMachine;
+import com.oransc.rappmanager.models.rapp.Rapp;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.Cache;
@RequiredArgsConstructor
public class RappCacheService {
- private final String RAPP_CACHE = "rapp-cache";
+ private static final String RAPP_CACHE = "rapp-cache";
private final CacheManager cacheManager;
- private final RappStateMachine rappStateMachine;
- public Cache getAllRapp() {
- return cacheManager.getCache(RAPP_CACHE);
+ public Collection<Rapp> getAllRapp() {
+ Cache cache = cacheManager.getCache(RAPP_CACHE);
+ if(cache != null) {
+ Map<String, Rapp> nativeCache = (Map<String, Rapp>) cache.getNativeCache();
+ return nativeCache.values();
+ }
+ return List.of();
}
public Optional<Rapp> getRapp(String rappId) {
- final Cache cache = cacheManager.getCache(RAPP_CACHE);
- Optional<Rapp> rappOptional = Optional.ofNullable(cache.get(rappId, Rapp.class));
- rappOptional.ifPresent(rapp -> rapp.setState(rappStateMachine.getRappState(rapp.getRappId())));
- return rappOptional;
+ return Optional.ofNullable(cacheManager.getCache(RAPP_CACHE)).map(cache -> cache.get(rappId, Rapp.class));
}
public void putRapp(Rapp rapp) {