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.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;
28 import org.onap.aaf.fproxy.data.CredentialCacheData;
30 public class InMemoryCredentialCache implements CredentialCache {
32 private final ConcurrentHashMap<String, SoftReference<CredentialCacheData>> cache = new ConcurrentHashMap<>();
33 private final DelayQueue<DelayedCacheObject> cleaningUpQueue = new DelayQueue<>();
35 public InMemoryCredentialCache() {
36 Thread cleanerThread = new Thread(() -> {
37 while (!Thread.currentThread().isInterrupted()) {
39 DelayedCacheObject delayedCacheObject = cleaningUpQueue.take();
40 cache.remove(delayedCacheObject.getKey(), delayedCacheObject.getReference());
41 } catch (InterruptedException e) {
42 Thread.currentThread().interrupt();
46 cleanerThread.setDaemon(true);
47 cleanerThread.start();
51 public void add(String key, CredentialCacheData value, long periodInMillis) {
58 long expiryTime = System.currentTimeMillis() + periodInMillis;
59 SoftReference<CredentialCacheData> reference = new SoftReference<>(value);
60 cache.put(key, reference);
61 cleaningUpQueue.put(new DelayedCacheObject(key, reference, expiryTime));
66 public void remove(String key) {
71 public CredentialCacheData get(String key) {
72 return Optional.ofNullable(cache.get(key)).map(SoftReference::get).orElse(null);
85 private static class DelayedCacheObject implements Delayed {
87 private final String key;
88 private final SoftReference<CredentialCacheData> reference;
89 private final long expiryTime;
91 public DelayedCacheObject(String key, SoftReference<CredentialCacheData> reference, long expiryTime) {
94 this.reference = reference;
95 this.expiryTime = expiryTime;
99 public long getDelay(TimeUnit unit) {
100 return unit.convert(expiryTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
104 public int compareTo(Delayed o) {
105 return Long.compare(expiryTime, ((DelayedCacheObject) o).expiryTime);
108 public String getKey() {
112 public SoftReference<CredentialCacheData> getReference() {