X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=auth%2Fauth-cass%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fauth%2Fdao%2FCached.java;h=a3fe17571b8e8780679dd3902e23e97d8fa11d99;hb=1296352d8eafee57f982a4342ad79ada4aa56d28;hp=1bda405c978ad2ac394a92ae869e35c78a2c410a;hpb=7e966914050e66219689001ff4ab601a49eef0ac;p=aaf%2Fauthz.git diff --git a/auth/auth-cass/src/main/java/org/onap/aaf/auth/dao/Cached.java b/auth/auth-cass/src/main/java/org/onap/aaf/auth/dao/Cached.java index 1bda405c..a3fe1757 100644 --- a/auth/auth-cass/src/main/java/org/onap/aaf/auth/dao/Cached.java +++ b/auth/auth-cass/src/main/java/org/onap/aaf/auth/dao/Cached.java @@ -3,13 +3,15 @@ * org.onap.aaf * =========================================================================== * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * + * Modification Copyright (c) 2019 IBM * =========================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -37,9 +39,8 @@ import org.onap.aaf.misc.env.Trans; public class Cached extends Cache { // Java does not allow creation of Arrays with Generics in them... - // private Map cache[]; protected final CIDAO info; - + private static Timer infoTimer; private Object cache[]; public final int segSize; @@ -47,19 +48,8 @@ public class Cached extends Cache info, String name, int segSize, long expireIn) { this.name =name; this.segSize = segSize; @@ -71,7 +61,19 @@ public class Cached extends Cache data) { @SuppressWarnings("unchecked") Map map = ((Map)cache[cacheIdx(key)]); @@ -83,14 +85,14 @@ public class Cached extends Cache map = ((Map)cache[cacheIdx]); -// if (map.remove(key)!=null) // Not seeming to remove all the time if (map!=null)map.clear(); -// System.err.println("Remove " + name + " " + key); return cacheIdx; } public Result invalidate(int segment) { - if (segment<0 || segment>=cache.length) return Result.err(Status.ERR_BadData,"Cache Segment %s is out of range",Integer.toString(segment)); + if (segment<0 || segment>=cache.length) { + return Result.err(Status.ERR_BadData,"Cache Segment %s is out of range",Integer.toString(segment)); + } @SuppressWarnings("unchecked") Map map = ((Map)cache[segment]); if (map!=null) { @@ -99,34 +101,33 @@ public class Cached extends Cache { + @FunctionalInterface + public interface Getter { public abstract Result> get(); }; - + // TODO utilize Segmented Caches, and fold "get" into "reads" @SuppressWarnings("unchecked") public Result> get(TRANS trans, String key, Getter getter) { List ld = null; Result> rld = null; - + int cacheIdx = cacheIdx(key); Map map = ((Map)cache[cacheIdx]); - + // Check for saved element in cache Dated cached = map.get(key); // Note: These Segment Timestamps are kept up to date with DB Date dbStamp = info.get(trans, name,cacheIdx); - + // Check for cache Entry and whether it is still good (a good Cache Entry is same or after DBEntry, so we use "before" syntax) - if (cached!=null && dbStamp.before(cached.timestamp)) { + if (cached!=null && dbStamp!=null && dbStamp.before(cached.timestamp)) { ld = (List)cached.data; rld = Result.ok(ld); } else { rld = getter.get(); if (rld.isOK()) { // only store valid lists map.put(key, new Dated(rld.value,expireIn)); // successful item found gets put in cache -// } else if (rld.status == Result.ERR_Backend){ -// map.remove(key); } } return rld; @@ -138,7 +139,7 @@ public class Cached extends Cache ... dao) { - for (CachedDAO d : dao) { + for (CachedDAO d : dao) { for (int i=0;i extends Cache void startRefresh(AuthzEnv env, CIDAO cidao) { if (infoTimer==null) { infoTimer = new Timer("CachedDAO Info Refresh Timer"); - int minRefresh = 10*1000*60; // 10 mins Integer.parseInt(env.getProperty(CACHE_MIN_REFRESH_INTERVAL,"2000")); // 2 second minimum refresh + int minRefresh = 10*1000*60; // 10 mins Integer.parseInt(env.getProperty(CACHE_MIN_REFRESH_INTERVAL,"2000")); // 2 second minimum refresh infoTimer.schedule(new Refresh(env,cidao, minRefresh), 1000, minRefresh); // note: Refresh from DB immediately } } - + public static void stopTimer() { Cache.stopTimer(); if (infoTimer!=null) { @@ -161,28 +162,30 @@ public class Cached extends Cache cidao; private int minRefresh; private long lastRun; - + public Refresh(AuthzEnv env, CIDAO cidao, int minRefresh) { this.env = env; this.cidao = cidao; this.minRefresh = minRefresh; - lastRun = System.currentTimeMillis()-maxRefresh-1000; + lastRun = System.currentTimeMillis()-MAXREFRESH-1000; } - + @Override public void run() { // Evaluate whether to refresh based on transaction rate long now = System.currentTimeMillis(); long interval = now-lastRun; - if (interval < minRefresh || interval < Math.min(env.transRate(),maxRefresh)) return; + if (interval < minRefresh || interval < Math.min(env.transRate(),MAXREFRESH)) { + return; + } lastRun = now; AuthzTrans trans = env.newTransNoAvg(); Result rv = cidao.check(trans);