mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
centralize ObjectCache into core templating API
This commit is contained in:
parent
f9e64ae0a5
commit
cec7f4017d
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user