added expiry options for cache
This commit is contained in:
parent
0cc4807d93
commit
b37a16bb89
@ -1,5 +1,6 @@
|
||||
package sarsoo.fmframework.cache;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
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 Puller<T, S> puller;
|
||||
private int expiryMinutes = 10;
|
||||
private boolean expires = false;
|
||||
|
||||
public StaticCache(Puller<T, S> puller) {
|
||||
pool = new ArrayList<CacheEntry<T>>();
|
||||
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) {
|
||||
this.puller = puller;
|
||||
}
|
||||
@ -30,7 +48,15 @@ public class StaticCache<T extends Cacheable, S> implements IStaticCache<T, S> {
|
||||
|
||||
if (item.isPresent()) {
|
||||
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();
|
||||
|
||||
} else {
|
||||
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();
|
||||
|
||||
Logger.getLog().log(new LogEntry("addCachedItem").addArg(input.toString()));
|
||||
|
||||
|
||||
if (item.isPresent()) {
|
||||
Logger.getLog().log(new LogEntry("addCachedItem").addArg("replaced").addArg(input.toString()));
|
||||
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));
|
||||
|
||||
}
|
||||
|
||||
protected void propagateCache(T in) {}
|
||||
|
||||
protected void propagateCache(T in) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
|
Loading…
Reference in New Issue
Block a user