Skip to content

Commit

Permalink
Use a WeakReference to cache class loaders
Browse files Browse the repository at this point in the history
Fixes #171
  • Loading branch information
havocp committed Jun 23, 2014
1 parent 08c4fe3 commit 95b31cc
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions config/src/main/java/com/typesafe/config/impl/ConfigImpl.java
Expand Up @@ -4,6 +4,7 @@
package com.typesafe.config.impl;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -28,22 +29,22 @@ public class ConfigImpl {

private static class LoaderCache {
private Config currentSystemProperties;
private ClassLoader currentLoader;
private WeakReference<ClassLoader> currentLoader;
private Map<String, Config> cache;

LoaderCache() {
this.currentSystemProperties = null;
this.currentLoader = null;
this.currentLoader = new WeakReference<ClassLoader>(null);
this.cache = new HashMap<String, Config>();
}

// for now, caching as long as the loader remains the same,
// drop entire cache if it changes.
synchronized Config getOrElseUpdate(ClassLoader loader, String key, Callable<Config> updater) {
if (loader != currentLoader) {
if (loader != currentLoader.get()) {
// reset the cache if we start using a different loader
cache.clear();
currentLoader = loader;
currentLoader = new WeakReference<ClassLoader>(loader);
}

Config systemProperties = systemPropertiesAsConfig();
Expand Down

0 comments on commit 95b31cc

Please sign in to comment.