All to 2.1.7-SNAPSHOT
[aaf/cadi.git] / sidecar / fproxy / src / main / java / org / onap / aaf / cadi / sidecar / fproxy / cache / InMemoryCredentialCache.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aaf
4  * ================================================================================
5  * Copyright © 2018 European Software Marketing Ltd.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *       http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aaf.cadi.sidecar.fproxy.cache;
21
22 import java.lang.ref.SoftReference;
23 import java.util.Optional;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.DelayQueue;
26 import java.util.concurrent.Delayed;
27 import java.util.concurrent.TimeUnit;
28
29 import org.onap.aaf.cadi.sidecar.fproxy.data.CredentialCacheData;
30
31 public class InMemoryCredentialCache implements CredentialCache {
32
33     private final ConcurrentHashMap<String, SoftReference<CredentialCacheData>> cache = new ConcurrentHashMap<>();
34     private final DelayQueue<DelayedCacheObject> cleaningUpQueue = new DelayQueue<>();
35
36     public InMemoryCredentialCache() {
37         Thread cleanerThread = new Thread(() -> {
38             while (!Thread.currentThread().isInterrupted()) {
39                 try {
40                     DelayedCacheObject delayedCacheObject = cleaningUpQueue.take();
41                     cache.remove(delayedCacheObject.getKey(), delayedCacheObject.getReference());
42                 } catch (InterruptedException e) {
43                     Thread.currentThread().interrupt();
44                 }
45             }
46         });
47         cleanerThread.setDaemon(true);
48         cleanerThread.start();
49     }
50
51     @Override
52     public void add(String key, CredentialCacheData value, long periodInMillis) {
53         if (key == null) {
54             return;
55         }
56         if (value == null) {
57             cache.remove(key);
58         } else {
59             long expiryTime = System.currentTimeMillis() + periodInMillis;
60             SoftReference<CredentialCacheData> reference = new SoftReference<>(value);
61             cache.put(key, reference);
62             cleaningUpQueue.put(new DelayedCacheObject(key, reference, expiryTime));
63         }
64     }
65
66     @Override
67     public void remove(String key) {
68         cache.remove(key);
69     }
70
71     @Override
72     public CredentialCacheData get(String key) {
73         return Optional.ofNullable(cache.get(key)).map(SoftReference::get).orElse(null);
74     }
75
76     @Override
77     public void clear() {
78         cache.clear();
79     }
80
81     @Override
82     public long size() {
83         return cache.size();
84     }
85
86     private static class DelayedCacheObject implements Delayed {
87
88         private final String key;
89         private final SoftReference<CredentialCacheData> reference;
90         private final long expiryTime;
91
92         public DelayedCacheObject(String key, SoftReference<CredentialCacheData> reference, long expiryTime) {
93             super();
94             this.key = key;
95             this.reference = reference;
96             this.expiryTime = expiryTime;
97         }
98
99         @Override
100         public long getDelay(TimeUnit unit) {
101             return unit.convert(expiryTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
102         }
103
104         @Override
105         public int compareTo(Delayed o) {
106             return Long.compare(expiryTime, ((DelayedCacheObject) o).expiryTime);
107         }
108
109         public String getKey() {
110             return key;
111         }
112
113         public SoftReference<CredentialCacheData> getReference() {
114             return reference;
115         }
116     }
117 }