com.maxmind.db.CHMCache Maven / Gradle / Ivy
package com.maxmind.db;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import com.fasterxml.jackson.databind.JsonNode;
/**
* A simplistic cache using a {@link ConcurrentHashMap}. There's no eviction
* policy, it just fills up until reaching the specified capacity (or
* close enough at least, bounds check is not atomic :)
*/
public class CHMCache implements NodeCache {
private static final int DEFAULT_CAPACITY = 4096;
private final int capacity;
private final ConcurrentHashMap cache;
private boolean cacheFull = false;
public CHMCache() {
this(DEFAULT_CAPACITY);
}
public CHMCache(int capacity) {
this.capacity = capacity;
this.cache = new ConcurrentHashMap(capacity);
}
@Override
public JsonNode get(int key, Loader loader) throws IOException {
Integer k = key;
JsonNode value = cache.get(k);
if (value == null) {
value = loader.load(key);
if (!cacheFull) {
if (cache.size() < capacity) {
cache.put(k, value);
} else {
cacheFull = true;
}
}
}
return value;
}
}