Skip to content

Commit

Permalink
add collectMetrics()
Browse files Browse the repository at this point in the history
Change-Id: I486b8feefa76a7ac3b7ca44f23285c6e3ebe780f
  • Loading branch information
javeme committed Jun 24, 2021
1 parent d8cf2a2 commit d4bd40a
Showing 1 changed file with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,7 @@ public V get(K id) {
}

if (this.enabledMetrics) {
if (value == null) {
this.miss.add(1L);
if (LOG.isDebugEnabled()) {
LOG.debug("Cache missed '{}' (miss={}, hits={})",
id, this.miss, this.hits);
}
} else {
this.hits.add(1L);
if (LOG.isDebugEnabled()) {
LOG.debug("Cache cached '{}' (hits={}, miss={})",
id, this.hits, this.miss);
}
}
this.collectMetrics(id, value);
}
return value;
}
Expand All @@ -114,19 +102,7 @@ public V getOrFetch(K id, Function<K, V> fetcher) {
}

if (this.enabledMetrics) {
if (value == null) {
this.miss.add(1L);
if (LOG.isDebugEnabled()) {
LOG.debug("Cache missed '{}' (miss={}, hits={})",
id, this.miss, this.hits);
}
} else {
this.hits.add(1L);
if (LOG.isDebugEnabled()) {
LOG.debug("Cache cached '{}' (hits={}, miss={})",
id, this.hits, this.miss);
}
}
this.collectMetrics(id, value);
}

// Do fetch and update the cache if cache missed
Expand All @@ -138,6 +114,22 @@ public V getOrFetch(K id, Function<K, V> fetcher) {
return value;
}

private void collectMetrics(K key, V value) {
if (value == null) {
this.miss.add(1L);
if (LOG.isDebugEnabled()) {
LOG.debug("Cache missed '{}' (miss={}, hits={})",
key, this.miss, this.hits);
}
} else {
this.hits.add(1L);
if (LOG.isDebugEnabled()) {
LOG.debug("Cache cached '{}' (hits={}, miss={})",
key, this.hits, this.miss);
}
}
}

@Override
public boolean update(K id, V value) {
return this.update(id, value, 0L);
Expand Down

0 comments on commit d4bd40a

Please sign in to comment.