2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aaf.cadi.sidecar.fproxy.cache;
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;
29 import org.onap.aaf.cadi.sidecar.fproxy.data.CredentialCacheData;
31 public class InMemoryCredentialCache implements CredentialCache {
33 private final ConcurrentHashMap<String, SoftReference<CredentialCacheData>> cache = new ConcurrentHashMap<>();
34 private final DelayQueue<DelayedCacheObject> cleaningUpQueue = new DelayQueue<>();
36 public InMemoryCredentialCache() {
37 Thread cleanerThread = new Thread(() -> {
38 while (!Thread.currentThread().isInterrupted()) {
40 DelayedCacheObject delayedCacheObject = cleaningUpQueue.take();
41 cache.remove(delayedCacheObject.getKey(), delayedCacheObject.getReference());
42 } catch (InterruptedException e) {
43 Thread.currentThread().interrupt();
47 cleanerThread.setDaemon(true);
48 cleanerThread.start();
52 public void add(String key, CredentialCacheData value, long periodInMillis) {
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));
67 public void remove(String key) {
72 public CredentialCacheData get(String key) {
73 return Optional.ofNullable(cache.get(key)).map(SoftReference::get).orElse(null);
86 private static class DelayedCacheObject implements Delayed {
88 private final String key;
89 private final SoftReference<CredentialCacheData> reference;
90 private final long expiryTime;
92 public DelayedCacheObject(String key, SoftReference<CredentialCacheData> reference, long expiryTime) {
95 this.reference = reference;
96 this.expiryTime = expiryTime;
100 public long getDelay(TimeUnit unit) {
101 return unit.convert(expiryTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
105 public int compareTo(Delayed o) {
106 return Long.compare(expiryTime, ((DelayedCacheObject) o).expiryTime);
109 public String getKey() {
113 public SoftReference<CredentialCacheData> getReference() {