T - - the cached object type.public class ReferenceCache<T> extends Object
A concurrent cache that holds references to objects so as to prevent unnecessary constructor calls. The cache does not have a maximum capacity or expulsion policy, but it uses a SoftReference for each stored object so that the garbage collector will clear the cache only before an OOM occurs.
private class Foo {
int id;
String label;
Bar bar;
private static final ReferenceCache <Foo> cache = new ReferenceCache <Foo>();
public static Foo newInstance(int id, String label, Bar bar){
Foo foo = cache.get(id, label, bar); // use the combination of id, label and bar as a cacheKey
if(foo == null){
foo = new Foo(id, label, bar);
cache.put(foo, id, label, bar);
}
return foo;
}
private Foo(int id, String label, Bar bar){
this.id = id;
this.label = label;
this.bar = bar;
}
}
| Constructor and Description |
|---|
ReferenceCache() |
| Modifier and Type | Method and Description |
|---|---|
T |
get(Object... args)
Return the cache value associated with the group of
args or
null if not value is found. |
void |
put(T value,
Object... args)
Cache
value and associated it with the group of args. |
void |
remove(Object... args)
Remove the value associated with the group of
args from the
cache. |
@Nullable public T get(Object... args)
args or
null if not value is found.args - public void put(T value, Object... args)
value and associated it with the group of args.
Each arg should be a value that is used to construct
the object.value - args - public void remove(Object... args)
args from the
cache.args -