X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=auth%2Fauth-core%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fauth%2Fcache%2FCache.java;h=6a8ccf1e8e5fb88c82f52be4c9a93860a337f90e;hb=ba989d05eca8d2a98c51ed9d38c4c3345db23349;hp=17368031cc3f0f9fb33fd65fc7c2d8ca03fdbd44;hpb=ceda6e8bc270202bcb24340b86617110289c902e;p=aaf%2Fauthz.git diff --git a/auth/auth-core/src/main/java/org/onap/aaf/auth/cache/Cache.java b/auth/auth-core/src/main/java/org/onap/aaf/auth/cache/Cache.java index 17368031..6a8ccf1e 100644 --- a/auth/auth-core/src/main/java/org/onap/aaf/auth/cache/Cache.java +++ b/auth/auth-core/src/main/java/org/onap/aaf/auth/cache/Cache.java @@ -31,7 +31,6 @@ import java.util.Set; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Level; import org.onap.aaf.misc.env.Env; import org.onap.aaf.misc.env.Trans; @@ -45,156 +44,156 @@ import org.onap.aaf.misc.env.Trans; * @param */ public class Cache { - private static Clean clean; - private static Timer cleanseTimer; - - public static final String CACHE_HIGH_COUNT = "CACHE_HIGH_COUNT"; - public static final String CACHE_CLEAN_INTERVAL = "CACHE_CLEAN_INTERVAL"; -// public static final String CACHE_MIN_REFRESH_INTERVAL = "CACHE_MIN_REFRESH_INTERVAL"; - - private static final Map> cacheMap; - - static { - cacheMap = new HashMap>(); - } - - /** - * Dated Class - store any Data with timestamp - * - * @author Jonathan - * - */ - public final static class Dated { - public Date timestamp; - public List data; - private long expireIn; - - public Dated(List data, long expireIn) { - timestamp = new Date(System.currentTimeMillis()+expireIn); - this.data = data; - this.expireIn = expireIn; - } - - public Dated(T t, long expireIn) { - timestamp = new Date(System.currentTimeMillis()+expireIn); - ArrayList al = new ArrayList(1); - al.add(t); - data = al; - this.expireIn = expireIn; - } - - public void touch() { - timestamp = new Date(System.currentTimeMillis()+expireIn); - } - } - - public static Map obtain(String key) { - Map m = cacheMap.get(key); - if(m==null) { - m = new ConcurrentHashMap(); - synchronized(cacheMap) { - cacheMap.put(key, m); - } - } - return m; - } - - /** - * Clean will examine resources, and remove those that have expired. - * - * If "highs" have been exceeded, then we'll expire 10% more the next time. This will adjust after each run - * without checking contents more than once, making a good average "high" in the minimum speed. - * - * @author Jonathan - * - */ - private final static class Clean extends TimerTask { - private final Env env; - private Set set; - - // The idea here is to not be too restrictive on a high, but to Expire more items by - // shortening the time to expire. This is done by judiciously incrementing "advance" - // when the "highs" are exceeded. This effectively reduces numbers of cached items quickly. - private final int high; - private long advance; - private final long timeInterval; - - public Clean(Env env, long cleanInterval, int highCount) { - this.env = env; - high = highCount; - timeInterval = cleanInterval; - advance = 0; - set = new HashSet(); - } - - public synchronized void add(String key) { - set.add(key); - } - - public void run() { - int count = 0; - int total = 0; - // look at now. If we need to expire more by increasing "now" by "advance" - Date now = new Date(System.currentTimeMillis() + advance); - - - for(String name : set) { - Map map = cacheMap.get(name); - if(map!=null) for(Map.Entry me : map.entrySet()) { - ++total; - if(me.getValue().timestamp.before(now)) { - map.remove(me.getKey()); - ++count; - } - } -// if(count>0) { -// env.info().log(Level.INFO, "Cache removed",count,"expired",name,"Elements"); -// } - } - - if(count>0) { - env.info().log(Level.INFO, "Cache removed",count,"expired Cached Elements out of", total); - } - - // If High (total) is reached during this period, increase the number of expired services removed for next time. - // There's no point doing it again here, as there should have been cleaned items. - if(total>high) { - // advance cleanup by 10%, without getting greater than timeInterval. - advance = Math.min(timeInterval, advance+(timeInterval/10)); - } else { - // reduce advance by 10%, without getting lower than 0. - advance = Math.max(0, advance-(timeInterval/10)); - } - } - } - - public static synchronized void startCleansing(Env env, String ... keys) { - if(cleanseTimer==null) { - cleanseTimer = new Timer("Cache Cleanup Timer"); - int cleanInterval = Integer.parseInt(env.getProperty(CACHE_CLEAN_INTERVAL,"60000")); // 1 minute clean cycles - int highCount = Integer.parseInt(env.getProperty(CACHE_HIGH_COUNT,"5000")); - cleanseTimer.schedule(clean = new Clean(env, cleanInterval, highCount), cleanInterval, cleanInterval); - } - - for(String key : keys) { - clean.add(key); - } - } - - public static void stopTimer() { - if(cleanseTimer!=null) { - cleanseTimer.cancel(); - cleanseTimer = null; - } - } - - public static void addShutdownHook() { - Runtime.getRuntime().addShutdownHook(new Thread() { - @Override - public void run() { - Cache.stopTimer(); - } - }); - } + private static Clean clean; + private static Timer cleanseTimer; + + public static final String CACHE_HIGH_COUNT = "CACHE_HIGH_COUNT"; + public static final String CACHE_CLEAN_INTERVAL = "CACHE_CLEAN_INTERVAL"; + + private static final Map> cacheMap; + + static { + cacheMap = new HashMap<>(); + } + + /** + * Dated Class - store any Data with timestamp + * + * @author Jonathan + * + */ + public static final class Dated { + public Date timestamp; + public List data; + private long expireIn; + + public Dated(List data, long expireIn) { + timestamp = new Date(System.currentTimeMillis()+expireIn); + this.data = data; + this.expireIn = expireIn; + } + + public Dated(T t, long expireIn) { + timestamp = new Date(System.currentTimeMillis()+expireIn); + ArrayList al = new ArrayList<>(1); + al.add(t); + data = al; + this.expireIn = expireIn; + } + + public void touch() { + timestamp = new Date(System.currentTimeMillis()+expireIn); + } + } + + public static Map obtain(String key) { + Map m = cacheMap.get(key); + if (m==null) { + m = new ConcurrentHashMap<>(); + synchronized(cacheMap) { + cacheMap.put(key, m); + } + } + return m; + } + + /** + * Clean will examine resources, and remove those that have expired. + * + * If "highs" have been exceeded, then we'll expire 10% more the next time. This will adjust after each run + * without checking contents more than once, making a good average "high" in the minimum speed. + * + * @author Jonathan + * + */ + private static final class Clean extends TimerTask { + private final Env env; + private Set set; + + // The idea here is to not be too restrictive on a high, but to Expire more items by + // shortening the time to expire. This is done by judiciously incrementing "advance" + // when the "highs" are exceeded. This effectively reduces numbers of cached items quickly. + private final int high; + private long advance; + private final long timeInterval; + + public Clean(Env env, long cleanInterval, int highCount) { + this.env = env; + high = highCount; + timeInterval = cleanInterval; + advance = 0; + set = new HashSet<>(); + } + + public synchronized void add(String key) { + set.add(key); + } + + public void run() { + int count = 0; + int total = 0; + // look at now. If we need to expire more by increasing "now" by "advance" + Date now = new Date(System.currentTimeMillis() + advance); + + + for (String name : set) { + Map map = cacheMap.get(name); + if (map==null) { + continue; + } + + for (Map.Entry me : map.entrySet()) { + ++total; + if (me.getValue().timestamp.before(now)) { + map.remove(me.getKey()); + ++count; + } + } + } + + if (count>0) { + env.debug().log("Cache removed",count,"expired Cached Elements out of", total); + } + + // If High (total) is reached during this period, increase the number of expired services removed for next time. + // There's no point doing it again here, as there should have been cleaned items. + if (total>high) { + // advance cleanup by 10%, without getting greater than timeInterval. + advance = Math.min(timeInterval, advance+(timeInterval/10)); + } else { + // reduce advance by 10%, without getting lower than 0. + advance = Math.max(0, advance-(timeInterval/10)); + } + } + } + + public static synchronized void startCleansing(Env env, String ... keys) { + if (cleanseTimer==null) { + cleanseTimer = new Timer("Cache Cleanup Timer"); + int cleanInterval = Integer.parseInt(env.getProperty(CACHE_CLEAN_INTERVAL,"60000")); // 1 minute clean cycles + int highCount = Integer.parseInt(env.getProperty(CACHE_HIGH_COUNT,"5000")); + cleanseTimer.schedule(clean = new Clean(env, cleanInterval, highCount), cleanInterval, cleanInterval); + } + + for (String key : keys) { + clean.add(key); + } + } + + public static void stopTimer() { + if (cleanseTimer!=null) { + cleanseTimer.cancel(); + cleanseTimer = null; + } + } + + public static void addShutdownHook() { + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + Cache.stopTimer(); + } + }); + } }