2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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====================================================
22 package org.onap.aaf.auth.cache;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.List;
31 import java.util.Timer;
32 import java.util.TimerTask;
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.logging.Level;
36 import org.onap.aaf.misc.env.Env;
37 import org.onap.aaf.misc.env.Trans;
40 * Create and maintain a Map of Maps used for Caching
47 public class Cache<TRANS extends Trans, DATA> {
48 private static Clean clean;
49 private static Timer cleanseTimer;
51 public static final String CACHE_HIGH_COUNT = "CACHE_HIGH_COUNT";
52 public static final String CACHE_CLEAN_INTERVAL = "CACHE_CLEAN_INTERVAL";
54 private static final Map<String,Map<String,Dated>> cacheMap;
57 cacheMap = new HashMap<>();
61 * Dated Class - store any Data with timestamp
66 public static final class Dated {
67 public Date timestamp;
69 private long expireIn;
71 public Dated(List<?> data, long expireIn) {
72 timestamp = new Date(System.currentTimeMillis()+expireIn);
74 this.expireIn = expireIn;
77 public <T> Dated(T t, long expireIn) {
78 timestamp = new Date(System.currentTimeMillis()+expireIn);
79 ArrayList<T> al = new ArrayList<>(1);
82 this.expireIn = expireIn;
86 timestamp = new Date(System.currentTimeMillis()+expireIn);
90 public static Map<String,Dated> obtain(String key) {
91 Map<String, Dated> m = cacheMap.get(key);
93 m = new ConcurrentHashMap<>();
94 synchronized(cacheMap) {
102 * Clean will examine resources, and remove those that have expired.
104 * If "highs" have been exceeded, then we'll expire 10% more the next time. This will adjust after each run
105 * without checking contents more than once, making a good average "high" in the minimum speed.
110 private static final class Clean extends TimerTask {
111 private final Env env;
112 private Set<String> set;
114 // The idea here is to not be too restrictive on a high, but to Expire more items by
115 // shortening the time to expire. This is done by judiciously incrementing "advance"
116 // when the "highs" are exceeded. This effectively reduces numbers of cached items quickly.
117 private final int high;
118 private long advance;
119 private final long timeInterval;
121 public Clean(Env env, long cleanInterval, int highCount) {
124 timeInterval = cleanInterval;
126 set = new HashSet<>();
129 public synchronized void add(String key) {
136 // look at now. If we need to expire more by increasing "now" by "advance"
137 Date now = new Date(System.currentTimeMillis() + advance);
140 for(String name : set) {
141 Map<String,Dated> map = cacheMap.get(name);
146 for(Map.Entry<String,Dated> me : map.entrySet()) {
148 if (me.getValue().timestamp.before(now)) {
149 map.remove(me.getKey());
156 env.info().log(Level.INFO, "Cache removed",count,"expired Cached Elements out of", total);
159 // If High (total) is reached during this period, increase the number of expired services removed for next time.
160 // There's no point doing it again here, as there should have been cleaned items.
162 // advance cleanup by 10%, without getting greater than timeInterval.
163 advance = Math.min(timeInterval, advance+(timeInterval/10));
165 // reduce advance by 10%, without getting lower than 0.
166 advance = Math.max(0, advance-(timeInterval/10));
171 public static synchronized void startCleansing(Env env, String ... keys) {
172 if(cleanseTimer==null) {
173 cleanseTimer = new Timer("Cache Cleanup Timer");
174 int cleanInterval = Integer.parseInt(env.getProperty(CACHE_CLEAN_INTERVAL,"60000")); // 1 minute clean cycles
175 int highCount = Integer.parseInt(env.getProperty(CACHE_HIGH_COUNT,"5000"));
176 cleanseTimer.schedule(clean = new Clean(env, cleanInterval, highCount), cleanInterval, cleanInterval);
179 for(String key : keys) {
184 public static void stopTimer() {
185 if(cleanseTimer!=null) {
186 cleanseTimer.cancel();
191 public static void addShutdownHook() {
192 Runtime.getRuntime().addShutdownHook(new Thread() {