centralize ObjectCache into core templating API

This commit is contained in:
Jonathan Shook 2021-09-13 09:42:21 -05:00
parent f9e64ae0a5
commit cec7f4017d

View File

@ -0,0 +1,27 @@
package io.nosqlbench.engine.api.templating;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
/**
* An object cache to memoize returned objects into a concurrent hash map by name.
* This is meant to be used when you want to lazily initialize an instance of something
* by name that is likely to be re-used over the lifetime of an owning object.
*
* @param <T> The type of object.
*/
public class ObjectCache<T> implements Function<String,T> {
private final ConcurrentHashMap<String,T> cache = new ConcurrentHashMap<>();
private final Function<String, T> newInstanceFunction;
public ObjectCache(Function<String,T> newInstanceFunction) {
this.newInstanceFunction = newInstanceFunction;
}
@Override
public T apply(String name) {
return cache.computeIfAbsent(name, newInstanceFunction);
}
}