Introduce caffeine cache
[cps.git] / cps-service / src / main / java / org / onap / cps / api / impl / YangTextSchemaSourceSetCache.java
1 package org.onap.cps.api.impl;
2 /*
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2020 Pantheon.tech
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        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  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 import com.google.errorprone.annotations.CanIgnoreReturnValue;
22 import java.util.Map;
23 import org.onap.cps.spi.CpsModulePersistenceService;
24 import org.onap.cps.yang.YangTextSchemaSourceSet;
25 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder;
26 import org.springframework.beans.factory.annotation.Autowired;
27 import org.springframework.cache.annotation.CacheConfig;
28 import org.springframework.cache.annotation.CachePut;
29 import org.springframework.cache.annotation.Cacheable;
30 import org.springframework.stereotype.Service;
31
32 /**
33  * Provides cached YangTextSchemaSourceSet.
34  */
35 @Service
36 @CacheConfig(cacheNames = {"yangSchema"})
37 public class YangTextSchemaSourceSetCache {
38
39     @Autowired
40     private CpsModulePersistenceService cpsModulePersistenceService;
41
42     /**
43      * Cache YangTextSchemaSourceSet.
44      *
45      * @param dataspaceName dataspace name
46      * @param schemaSetName schema set name
47      * @return YangTextSchemaSourceSet
48      */
49     @Cacheable(key = "#p0.concat('-').concat(#p1)")
50     public YangTextSchemaSourceSet get(final String dataspaceName, final String schemaSetName) {
51         final Map<String, String> yangResourceNameToContent =
52                 cpsModulePersistenceService.getYangSchemaResources(dataspaceName, schemaSetName);
53         return YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent);
54     }
55
56     /**
57      * Updates cache YangTextSchemaSourceSet.
58      *
59      * @param dataspaceName dataspace name
60      * @param schemaSetName schema set name
61      * @param yangTextSchemaSourceSet yangTextSchemaSourceSet
62      * @return YangTextSchemaSourceSet
63      */
64     @CachePut(key = "#p0.concat('-').concat(#p1)")
65     @CanIgnoreReturnValue
66     public YangTextSchemaSourceSet updateCache(final String dataspaceName, final String schemaSetName,
67             final YangTextSchemaSourceSet yangTextSchemaSourceSet) {
68         return yangTextSchemaSourceSet;
69     }
70 }