Skip to content

Commit

Permalink
Cache null values in EnvConfigSource. (#441)
Browse files Browse the repository at this point in the history
* Cache null values in EnvConfigSource.
Fixes #440.

Co-authored-by: David M. Lloyd <[email protected]>
  • Loading branch information
radcortez and dmlloyd authored Nov 3, 2020
1 parent 1349b1b commit 310727d
Showing 1 changed file with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static java.security.AccessController.doPrivileged;
import static java.util.Collections.unmodifiableMap;

import java.io.Serializable;
import java.security.PrivilegedAction;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -31,9 +32,11 @@
*/
public class EnvConfigSource extends AbstractConfigSource {
private static final long serialVersionUID = -4525015934376795496L;

private static final int DEFAULT_ORDINAL = 300;
private static final Object NULL_VALUE = new Object();

private final Map<String, String> cache = new ConcurrentHashMap<>(); //the regex match is expensive
private final Map<String, Object> cache = new ConcurrentHashMap<>();

protected EnvConfigSource() {
super("EnvConfigSource", getEnvOrdinal());
Expand All @@ -50,9 +53,12 @@ public String getValue(String name) {
return null;
}

String cachedValue = cache.get(name);
Object cachedValue = cache.get(name);
if (cachedValue != null) {
return cachedValue;
if (cachedValue == NULL_VALUE) {
return null;
}
return (String) cachedValue;
}

final Map<String, String> properties = getProperties();
Expand Down Expand Up @@ -80,6 +86,7 @@ public String getValue(String name) {
return value;
}

cache.put(name, NULL_VALUE);
return null;
}

Expand Down Expand Up @@ -133,4 +140,16 @@ private static int getEnvOrdinal() {

return DEFAULT_ORDINAL;
}

Object writeReplace() {
return new Ser();
}

static final class Ser implements Serializable {
private static final long serialVersionUID = 6812312718645271331L;

Object readResolve() {
return new EnvConfigSource();
}
}
}

0 comments on commit 310727d

Please sign in to comment.