added expiry options for cache

This commit is contained in:
aj 2019-05-22 13:21:31 +01:00
parent 0cc4807d93
commit b37a16bb89

View File

@ -1,5 +1,6 @@
package sarsoo.fmframework.cache; package sarsoo.fmframework.cache;
import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Optional; import java.util.Optional;
@ -13,12 +14,29 @@ public class StaticCache<T extends Cacheable, S> implements IStaticCache<T, S> {
private ArrayList<CacheEntry<T>> pool; private ArrayList<CacheEntry<T>> pool;
private Puller<T, S> puller; private Puller<T, S> puller;
private int expiryMinutes = 10;
private boolean expires = false;
public StaticCache(Puller<T, S> puller) { public StaticCache(Puller<T, S> puller) {
pool = new ArrayList<CacheEntry<T>>(); pool = new ArrayList<CacheEntry<T>>();
this.puller = puller; this.puller = puller;
} }
public StaticCache(Puller<T, S> puller, int expiry) {
pool = new ArrayList<CacheEntry<T>>();
this.puller = puller;
this.expires = true;
this.expiryMinutes = expiry;
}
public void setExpiryMinutes(int minutes) {
this.expiryMinutes = minutes;
}
public void setExpires(boolean choice) {
this.expires = choice;
}
public void setPuller(Puller<T, S> puller) { public void setPuller(Puller<T, S> puller) {
this.puller = puller; this.puller = puller;
} }
@ -30,7 +48,15 @@ public class StaticCache<T extends Cacheable, S> implements IStaticCache<T, S> {
if (item.isPresent()) { if (item.isPresent()) {
Logger.getLog().log(new LogEntry("getCachedItem").addArg("found").addArg(input.toString())); Logger.getLog().log(new LogEntry("getCachedItem").addArg("found").addArg(input.toString()));
if (expires) {
if (item.get().getTime().isBefore(LocalDateTime.now().minusMinutes(expiryMinutes))) {
pool.remove(item.get());
return get(input);
}
}
return item.get().getSubject(); return item.get().getSubject();
} else { } else {
Logger.getLog().log(new LogEntry("getCachedItem").addArg("pulling").addArg(input.toString())); Logger.getLog().log(new LogEntry("getCachedItem").addArg("pulling").addArg(input.toString()));
@ -78,7 +104,7 @@ public class StaticCache<T extends Cacheable, S> implements IStaticCache<T, S> {
Optional<CacheEntry<T>> item = pool.stream().filter(i -> i.getSubject().matches(input)).findFirst(); Optional<CacheEntry<T>> item = pool.stream().filter(i -> i.getSubject().matches(input)).findFirst();
Logger.getLog().log(new LogEntry("addCachedItem").addArg(input.toString())); Logger.getLog().log(new LogEntry("addCachedItem").addArg(input.toString()));
if (item.isPresent()) { if (item.isPresent()) {
Logger.getLog().log(new LogEntry("addCachedItem").addArg("replaced").addArg(input.toString())); Logger.getLog().log(new LogEntry("addCachedItem").addArg("replaced").addArg(input.toString()));
pool.remove(item.get()); pool.remove(item.get());
@ -87,8 +113,9 @@ public class StaticCache<T extends Cacheable, S> implements IStaticCache<T, S> {
pool.add(new CacheEntry<T>(input)); pool.add(new CacheEntry<T>(input));
} }
protected void propagateCache(T in) {} protected void propagateCache(T in) {
}
@Override @Override
public void flush() { public void flush() {