2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4 * ===============================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END========================================================================
19 package com.oransc.rappmanager.models.cache;
21 import com.oransc.rappmanager.models.rapp.Rapp;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
26 import java.util.Optional;
27 import lombok.RequiredArgsConstructor;
28 import org.springframework.cache.Cache;
29 import org.springframework.cache.CacheManager;
30 import org.springframework.stereotype.Service;
33 @RequiredArgsConstructor
34 public class RappCacheService {
36 private final String RAPP_CACHE = "rapp-cache";
37 private final CacheManager cacheManager;
39 public Collection<Rapp> getAllRapp() {
40 Cache cache = cacheManager.getCache(RAPP_CACHE);
42 Map<String, Rapp> nativeCache = (Map<String, Rapp>) cache.getNativeCache();
43 return nativeCache.values();
48 public Optional<Rapp> getRapp(String rappId) {
49 final Cache cache = cacheManager.getCache(RAPP_CACHE);
50 return Optional.ofNullable(cache.get(rappId, Rapp.class));
53 public void putRapp(Rapp rapp) {
54 final Cache cache = cacheManager.getCache(RAPP_CACHE);
56 cache.put(rapp.getName(), rapp);
60 public void deleteRapp(Rapp rapp) {
61 final Cache cache = cacheManager.getCache(RAPP_CACHE);
63 cache.evict(rapp.getName());