feat: cache topic and subscription reads

This commit is contained in:
richjohnrambo
2026-09-24 14:46:00 +08:00
parent 0881256103
commit f952249dc3
4 changed files with 74 additions and 12 deletions
@@ -409,3 +409,28 @@ func TestInvalidateGroupSettingsUsesTopicCacheKey(t *testing.T) {
t.Fatalf("unexpected deleted objects: %#v", runtime.deletedObjects)
}
}
func TestTopicAndSubscriptionCacheTTLIsShort(t *testing.T) {
runtime := &redisGroupMessageRuntime{cfg: redisConfig{CacheTTL: 300, ShortTTL: 600}}
for _, namespace := range []string{cacheNamespaceTopic, cacheNamespaceSub} {
if got := runtime.cacheTTL(namespace); got != time.Minute {
t.Fatalf("cache TTL for %s = %v, want 1m", namespace, got)
}
}
if got := runtime.cacheTTL(cacheNamespaceGroupSettings); got != 5*time.Minute {
t.Fatalf("cache TTL for group settings = %v, want 5m", got)
}
}
func TestInvalidateSubscriptionCacheWithZeroUserDeletesTopicPrefix(t *testing.T) {
oldRuntime := groupMsgRuntime
runtime := newTestGroupMessageRuntime()
groupMsgRuntime = runtime
t.Cleanup(func() { groupMsgRuntime = oldRuntime })
invalidateSubscriptionCache(7, "grpAlpha", types.ZeroUid)
want := []string{"grpAlpha:", "chnAlpha:"}
if len(runtime.deletedPrefixes) != len(want) || runtime.deletedPrefixes[0] != want[0] || runtime.deletedPrefixes[1] != want[1] {
t.Fatalf("unexpected deleted prefixes: %#v", runtime.deletedPrefixes)
}
}