Alexey Kutuzov
Alexey Kutuzov

Reputation: 689

Apache commons configuration cache

I'm looking how I can cache my property in apache-commons-configuration framework. It tooks a long time to obtain a property from different places, defined in my config.xml. So, is there a cached (by time, for example) implementation of Configuration interface?

Upvotes: 4

Views: 2871

Answers (3)

rjdkolb
rjdkolb

Reputation: 11888

I extends DatabaseConfiguration so it does not hit my database all the time. As for reloads, I instantiate my config where I need it and throw it away when I am done with it.

public class MyConfig extends DatabaseConfiguration {

    private WeakHashMap<String,Object> cache = new WeakHashMap<String,Object>();

    public MyConfig(String datasourceString,String section) throws NamingException {
        this((DataSource) new InitialContext().lookup(datasourceString),section);
    }

    protected MyConfig(DataSource datasource,String section) {
        super(datasource, "COMMON_CONFIG","PROP_SECTION", "PROP_KEY", "PROP_VALUE",section);
    }

    @Override
    public Object getProperty(String key){
        Object cachedValue = cache.get(key);
        if (cachedValue != null){
            return cachedValue;
        }
        Object databaseValue = super.getProperty(key);
        cache.put(key, databaseValue);
        return databaseValue;

    }
}

Upvotes: 1

tgkprog
tgkprog

Reputation: 4598

  • You can save the apache object in a static variable in some class and set to null when done. Have static getter to read it

  • Not sure about the apache config API but we use a static HashMap and store properties in it.

if all string:

private static Map data = new HashMap ();

can expose as properties so you can use anywhere

public class Props{

private static Map<String, String> data = new HashMap<String, String> ();

public static void put(String name, String val){
    data.put(name,val);
}

public static String  get(String name){
    return data.get(name)
}

public static  void load(){//todo }


public static  void save(){//todo if needed if few change and need persistence}

}

For any data type besides primitives

public class Props{

private static Map<String, Object> data = new HashMap<String, Object> ();

public static void put(String name, Object val){
    data.put(name,val);
}

public static String  get(String name){
    return data.get(name)
}

public static void load(){//todo }


public static void save(){//todo if needed if few change and need persistence}

}

If you wanted the objects to be dropped after sometime can use WhirlyCache instead of the HashMap. I dont see what can go wrong?

Upvotes: 1

Alexey Kutuzov
Alexey Kutuzov

Reputation: 689

Finally, i writed my own cache, using guava:

public class Cfg {
    private static Logger log = LoggerFactory.getLogger(Cfg.class);
    private Configuration cfg;
    private LoadingCache<String, Boolean> boolCache;
    private LoadingCache<String, String> stringCache;
    private LoadingCache<String, Float> floatCache;
    private LoadingCache<String, Integer> integerCache;
    private LoadingCache<String, List> listCache;

    @PostConstruct
    public void init() {
        boolCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, Boolean>() {
            @Override
            public Boolean load(String key) throws Exception {
                return check(cfg.getBoolean(key), key);
            }
        });
        stringCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, String>() {
            @Override
            public String load(String key) throws Exception {
                return check(cfg.getString(key), key);
            }
        });
        floatCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, Float>() {
            @Override
            public Float load(String key) throws Exception {
                return check(cfg.getFloat(key), key);
            }
        });
        integerCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, Integer>() {
            @Override
            public Integer load(String key) throws Exception {
                return check(cfg.getInt(key), key);
            }
        });
        listCache = CacheBuilder.newBuilder().expireAfterAccess(cfg.getInt("configuration.cache"), TimeUnit.MINUTES).build(new CacheLoader<String, List>() {
            @Override
            public List load(String key) throws Exception {
                return check(cfg.getList(key), key);
            }
        });
    }

    public boolean _bool(String key) {
        try {
            return boolCache.get(key);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public float _float(String key) {
        try {
            return floatCache.get(key);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public int _int(String key) {
        try {
            return integerCache.get(key);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public String _string(String key) {
        try {
            return stringCache.get(key);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public List<String> _list(String key) {
        try {
            //noinspection unchecked
            return listCache.get(key);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public void setCfg(Configuration cfg) {
        this.cfg = cfg;
    }

    private <T> T check(T el, String key) {
        if (el != null) {
            return el;
        }
        throw new KeyNotFound(key);
    }
}

Upvotes: 1

Related Questions