diff --git a/coherence/cache.go b/coherence/cache.go index 1b8e87e..82acc04 100644 --- a/coherence/cache.go +++ b/coherence/cache.go @@ -198,6 +198,9 @@ type NamedMap[K comparable, V any] interface { // GetNearCacheStats returns the [CacheStats] for a near cache for a [NamedMap] or [NamedCache]. // If no near cache is defined, nil is returned. GetNearCacheStats() CacheStats + + // GetCacheName returns the cache name of the [NamedMap] or [NamedCache]. + GetCacheName() string } // NamedCache is syntactically identical in behaviour to a NamedMap, but additionally implements diff --git a/coherence/common.go b/coherence/common.go index 2403a73..927bab9 100644 --- a/coherence/common.go +++ b/coherence/common.go @@ -132,7 +132,7 @@ func executeClear[K comparable, V any](ctx context.Context, bc *baseClient[K, V] _, err = bc.client.Clear(newCtx, &clearRequest) // clear the near cache - if err != nil && nearCache != nil { + if nearCache != nil { nearCache.Clear() } @@ -238,7 +238,7 @@ func executeTruncate[K comparable, V any](ctx context.Context, bc *baseClient[K, _, err = bc.client.Truncate(newCtx, &request) // clear the near cache - if err != nil && nearCache != nil { + if nearCache != nil { nearCache.Clear() } diff --git a/coherence/event.go b/coherence/event.go index 3b44d55..dac33c0 100644 --- a/coherence/event.go +++ b/coherence/event.go @@ -186,7 +186,7 @@ func (l *mapLifecycleEvent[K, V]) Source() NamedMap[K, V] { // String returns a string representation of a MapLifecycleEvent. func (l *mapLifecycleEvent[K, V]) String() string { - return fmt.Sprintf("MapLifecycleEvent{source=%v, type=%s}", l.Source(), l.Type()) + return fmt.Sprintf("MapLifecycleEvent{source=%v, type=%s}", l.Source().GetCacheName(), l.Type()) } // MapEvent an event which indicates that the content of the NamedMap or @@ -321,8 +321,8 @@ func (e *mapEvent[K, V]) String() string { } return *val } - return fmt.Sprintf("MapEvent{source=%v, name=%s, type=%s, key=%s, oldValue=%s, newValue=%s}", - source, source.Name(), e.eventType, keyEval(key, keyErr), valueEval(oldValue, oldErr), valueEval(newValue, newErr)) + return fmt.Sprintf("MapEvent{source=%v, name=%s, type=%s, key=%v, oldValue=%v, newValue=%v}", + source.GetCacheName(), source.Name(), e.eventType, keyEval(key, keyErr), valueEval(oldValue, oldErr), valueEval(newValue, newErr)) } type SessionLifecycleListener interface { diff --git a/coherence/named_cache_client.go b/coherence/named_cache_client.go index a162f62..25e4688 100644 --- a/coherence/named_cache_client.go +++ b/coherence/named_cache_client.go @@ -28,6 +28,11 @@ func (nc *NamedCacheClient[K, V]) getBaseClient() *baseClient[K, V] { // nolint return &nc.baseClient } +// GetCacheName returns the cache name of the [NamedCache]. +func (nc *NamedCacheClient[K, V]) GetCacheName() string { + return nc.name +} + // AddLifecycleListener Adds a [MapLifecycleListener] that will receive events (truncated or released) that occur // against the [NamedCache]. func (nc *NamedCacheClient[K, V]) AddLifecycleListener(listener MapLifecycleListener[K, V]) { diff --git a/coherence/named_map_client.go b/coherence/named_map_client.go index 36358e0..18c825f 100644 --- a/coherence/named_map_client.go +++ b/coherence/named_map_client.go @@ -248,6 +248,11 @@ func RemoveIndex[K comparable, V, T, E any](ctx context.Context, nm NamedMap[K, return executeRemoveIndex(ctx, nm.getBaseClient(), extractor) } +// GetCacheName returns the cache name of the [NamedMap]. +func (nm *NamedMapClient[K, V]) GetCacheName() string { + return nm.name +} + // AddLifecycleListener adds a [MapLifecycleListener] that will receive events (truncated or released) that occur // against the [NamedMap]. func (nm *NamedMapClient[K, V]) AddLifecycleListener(listener MapLifecycleListener[K, V]) { diff --git a/test/e2e/standalone/near_cache_test.go b/test/e2e/standalone/near_cache_test.go index 24522de..719d1a0 100644 --- a/test/e2e/standalone/near_cache_test.go +++ b/test/e2e/standalone/near_cache_test.go @@ -39,8 +39,10 @@ func TestNearCacheOperationsAgainstMapAndCache(t *testing.T) { nameMap coherence.NamedMap[int, Person] test func(t *testing.T, namedCache coherence.NamedMap[int, Person]) }{ - {"TestNearCacheBasicNamedMap", GetNearCacheNamedMap[int, Person](g, session, "near-cache-basic-map", coherence.WithNearCache(&nearCacheOptions10Seconds)), RunTestNearCacheBasic}, - {"TestNearCacheBasicNamedCache", GetNearCacheNamedCache[int, Person](g, session, "near-cache-basic-cache", coherence.WithNearCache(&nearCacheOptions10Seconds)), RunTestNearCacheBasic}, + {"RunTestNearCacheBasicNamedMap", GetNearCacheNamedMap[int, Person](g, session, "near-cache-basic-map", coherence.WithNearCache(&nearCacheOptions10Seconds)), RunTestNearCacheBasic}, + {"RunTestNearCacheBasicNamedCache", GetNearCacheNamedCache[int, Person](g, session, "near-cache-basic-cache", coherence.WithNearCache(&nearCacheOptions10Seconds)), RunTestNearCacheBasic}, + {"RunTestNearWithClearNamedCache", GetNearCacheNamedCache[int, Person](g, session, "near-cache-clear-cache", coherence.WithNearCache(&nearCacheOptions10Seconds)), RunTestNearWithClear}, + {"RunTestNearWithClearNamedMap", GetNearCacheNamedCache[int, Person](g, session, "near-cache-clear-map", coherence.WithNearCache(&nearCacheOptions10Seconds)), RunTestNearWithClear}, {"RunTestNearCacheRemovesNamedMap", GetNearCacheNamedMap[int, Person](g, session, "near-cache-removes-map", coherence.WithNearCache(&nearCacheOptions120Seconds)), RunTestNearCacheRemoves}, {"RunTestNearCacheRemovesNamedCache", GetNearCacheNamedCache[int, Person](g, session, "near-cache-removes-cache", coherence.WithNearCache(&nearCacheOptions120Seconds)), RunTestNearCacheRemoves}, {"RunTestNearCacheContainsKeyNamedMap", GetNearCacheNamedMap[int, Person](g, session, "near-cache-removes-map", coherence.WithNearCache(&nearCacheOptions120Seconds)), RunTestNearCacheContainsKey}, @@ -347,6 +349,51 @@ func RunTestNearCacheWithHighUnits(t *testing.T, namedMap coherence.NamedMap[int g.Expect(namedMap.GetNearCacheStats().GetCachePrunes()).To(gomega.Equal(int64(1))) } +// RunTestNearWithClear tests a near cache that issues puts, get then clears.. +func RunTestNearWithClear(t *testing.T, namedMap coherence.NamedMap[int, Person]) { + var ( + g = gomega.NewWithT(t) + err error + p1Get *Person + p2Get *Person + ) + + err = namedMap.Clear(ctx) + g.Expect(err).ShouldNot(gomega.HaveOccurred()) + + p1 := Person{ID: 1, Name: "p1"} + p2 := Person{ID: 2, Name: "p2"} + + for i := 1; i < 100; i++ { + _, err = namedMap.Put(ctx, p1.ID, p1) + g.Expect(err).ShouldNot(gomega.HaveOccurred()) + + _, err = namedMap.Put(ctx, p2.ID, p2) + g.Expect(err).ShouldNot(gomega.HaveOccurred()) + + AssertSize[int, Person](g, namedMap, 2) + + p1Get, err = namedMap.Get(ctx, p1.ID) + g.Expect(err).ShouldNot(gomega.HaveOccurred()) + g.Expect(*p1Get).Should(gomega.Equal(p1)) + + p2Get, err = namedMap.Get(ctx, p2.ID) + g.Expect(err).ShouldNot(gomega.HaveOccurred()) + g.Expect(*p2Get).Should(gomega.Equal(p2)) + + err = namedMap.Clear(ctx) + g.Expect(err).ShouldNot(gomega.HaveOccurred()) + + p1Get, err = namedMap.Get(ctx, p1.ID) + g.Expect(err).ShouldNot(gomega.HaveOccurred()) + g.Expect(p1Get).Should(gomega.BeNil()) + + p2Get, err = namedMap.Get(ctx, p2.ID) + g.Expect(err).ShouldNot(gomega.HaveOccurred()) + g.Expect(p2Get).Should(gomega.BeNil()) + } +} + // RunTestNearCacheWithHighUnits tests near cache with high units of 100 and accessing entries to ensure they are not removed. func RunTestNearCacheWithHighUnitsAccess(t *testing.T, namedMap coherence.NamedMap[int, Person]) { var (