T - The key type for loading the metric id.public class MetricIdCache<T> extends Object
A utility class that simplifies mutating and storing a MetricId.
// example using Guava.
final MetricIdCache.CacheBuilder cacheBuilder = new MetricIdCache.CacheBuilder() {
public ConcurrentMap build() {
return CacheBuilder.newBuilder()
.expireAfterWrite(12, TimeUnit.HOURS).build().asMap()
}
};
final MetricIdCache.Builder builder = MetricIdCache.builder().cacheBuilder(cacheBuilder);
final MetricIdCache cache = builder.metricId(MetricId.build().tagged("foo", "bar"))
.loader(new MetricIdCache.Loader() {
public MetricId load(MetricId base, String key) {
base.tagged("key", key);
}
}).build();
cache.get("hello"); // MetricId(foo=bar, key=hello)
cache.get("world"); // MetricId(foo=bar, key=world)
// subsequent calls with cache.get("hello"), or cache.get("world") will return the same
MetricId instance.
| Modifier and Type | Class and Description |
|---|---|
static class |
MetricIdCache.Any
A metric id cache builder that can take any type.
|
static interface |
MetricIdCache.Cache<T> |
static interface |
MetricIdCache.CacheBuilder |
static interface |
MetricIdCache.Loader<T> |
static interface |
MetricIdCache.MapBuilder |
static class |
MetricIdCache.Typed<T>
A builder for which the metric id cache has a given type.
|
| Modifier and Type | Method and Description |
|---|---|
static MetricIdCache.Any |
builder() |
MetricId |
get(T key)
Get the value for the specified key.
|
void |
invalidate(T key) |
void |
invalidateAll() |
public static MetricIdCache.Any builder()
public MetricId get(T key)
This method is guaranteed to return the same instance of the associated MetricId, regardless of how many threads invoke it.
The configured Loader could be called multiple times, and should be idempotent to that.
key - The given key to get MetricId for.IllegalStateException - if loader returns null.public void invalidate(T key)
public void invalidateAll()
Copyright © 2017. All rights reserved.