Add (remote) cache metrics (#3033)

This only applies to the CacheModule-provided caches because we don't
want to have to deal with all the various other caches. We'll want to
know the various ratios between types of cache hits/misses when
evaluating the usefulness of the remote caching.
This commit is contained in:
gbrodman
2026-05-11 18:19:52 +00:00
committed by GitHub
parent b69d51add1
commit 5854ccf00d
7 changed files with 101 additions and 23 deletions
@@ -43,11 +43,12 @@ public class MultilayerDomainCacheTest {
private final SimplifiedJedisClient jedisClient = mock(SimplifiedJedisClient.class);
private final FakeClock clock = new FakeClock();
private final CacheMetrics cacheMetrics = mock(CacheMetrics.class);
private MultilayerDomainCache cache;
@BeforeEach
void beforeEach() {
cache = new MultilayerDomainCache(jedisClient, clock);
cache = new MultilayerDomainCache(jedisClient, clock, cacheMetrics);
createTld("tld");
}
@@ -59,10 +60,13 @@ public class MultilayerDomainCacheTest {
// We should have filled the caches after one attempt to load from Valkey
verify(jedisClient).get(Domain.class, "example.tld");
verify(jedisClient).set(new SimplifiedJedisClient.JedisResource<>("example.tld", domain));
verify(cacheMetrics).recordLookup("Domain", CacheMetrics.CacheHitType.MISS);
// Further loads hit the local cache
assertThat(cache.loadByDomainName("example.tld")).hasValue(domain);
verify(cacheMetrics).recordLookup("Domain", CacheMetrics.CacheHitType.LOCAL);
verifyNoMoreInteractions(jedisClient);
verifyNoMoreInteractions(cacheMetrics);
}
@Test
@@ -72,6 +76,8 @@ public class MultilayerDomainCacheTest {
// We hit the Valkey cache first
when(jedisClient.get(Domain.class, "example.tld")).thenReturn(Optional.of(domain));
assertThat(cache.loadByDomainName("example.tld")).hasValue(domain);
verify(cacheMetrics).recordLookup("Domain", CacheMetrics.CacheHitType.REMOTE);
verifyNoMoreInteractions(cacheMetrics);
}
@Test
@@ -83,11 +89,15 @@ public class MultilayerDomainCacheTest {
// This time, we don't populate the remote cache because it's prober data
verify(jedisClient).get(Domain.class, "example.tld");
verify(cacheMetrics).recordLookup("Domain", CacheMetrics.CacheHitType.MISS);
verifyNoMoreInteractions(jedisClient);
verifyNoMoreInteractions(cacheMetrics);
}
@Test
void testLoad_missing() {
assertThat(cache.loadByDomainName("nonexistent.tld")).isEmpty();
verify(cacheMetrics).recordLookup("Domain", CacheMetrics.CacheHitType.MISS_NONEXISTENT);
verifyNoMoreInteractions(cacheMetrics);
}
}
@@ -38,11 +38,12 @@ public class MultilayerHostCacheTest {
new JpaTestExtensions.Builder().buildIntegrationTestExtension();
private final SimplifiedJedisClient jedisClient = mock(SimplifiedJedisClient.class);
private final CacheMetrics cacheMetrics = mock(CacheMetrics.class);
private MultilayerHostCache cache;
@BeforeEach
void beforeEach() {
cache = new MultilayerHostCache(jedisClient);
cache = new MultilayerHostCache(jedisClient, cacheMetrics);
}
@Test
@@ -53,10 +54,13 @@ public class MultilayerHostCacheTest {
// We should have filled the caches after one attempt to load from Valkey
verify(jedisClient).get(Host.class, host.getRepoId());
verify(jedisClient).set(new SimplifiedJedisClient.JedisResource<>(host.getRepoId(), host));
verify(cacheMetrics).recordLookup("Host", CacheMetrics.CacheHitType.MISS);
// Further loads hit the local cache
assertThat(cache.loadByRepoId(host.getRepoId())).hasValue(host);
verify(cacheMetrics).recordLookup("Host", CacheMetrics.CacheHitType.LOCAL);
verifyNoMoreInteractions(jedisClient);
verifyNoMoreInteractions(cacheMetrics);
}
@Test
@@ -66,10 +70,14 @@ public class MultilayerHostCacheTest {
// We hit the Valkey cache first
when(jedisClient.get(Host.class, host.getRepoId())).thenReturn(Optional.of(host));
assertThat(cache.loadByRepoId(host.getRepoId())).hasValue(host);
verify(cacheMetrics).recordLookup("Host", CacheMetrics.CacheHitType.REMOTE);
verifyNoMoreInteractions(cacheMetrics);
}
@Test
void testLoad_missing() {
assertThat(cache.loadByRepoId("nonexistent")).isEmpty();
verify(cacheMetrics).recordLookup("Host", CacheMetrics.CacheHitType.MISS_NONEXISTENT);
verifyNoMoreInteractions(cacheMetrics);
}
}