@@ -86,7 +86,7 @@ type baseClient[K comparable, V any] struct {
8686 mutex * sync.RWMutex
8787 nearCache * localCacheImpl [K , V ]
8888 nearCacheListener * namedCacheNearCacheListener [K , V ]
89- nearCacheLifecycleListener * namedCacheNearLifecyleListener [K , V ]
89+ nearCacheLifecycleListener * namedCacheNearLifestyleListener [K , V ]
9090
9191 // gRPC v1 listeners registered
9292 keyListenersV1 map [K ]* listenerGroupV1 [K , V ]
@@ -103,15 +103,26 @@ type CacheOptions struct {
103103
104104// NearCacheOptions defines options when creating a near cache.
105105type NearCacheOptions struct {
106- TTL time.Duration
107- HighUnits int64
108- HighUnitsMemory int64
106+ // TTL is the maximum time to keep the entry in the near cache. When this time has been reached it will be expired.
107+ TTL time.Duration
108+
109+ // HighUnits is the maximum number of cache entries to keep in the near cache.
110+ HighUnits int64
111+
112+ // HighUnitsMemory is the maximum amount of memory to use for entries in the near cache.
113+ HighUnitsMemory int64
114+
109115 InvalidationStrategy InvalidationStrategyType // currently only supports ListenAll
116+
117+ // PruneFactor indicates the percentage of the total number of units that will remain
118+ // after the cache manager prunes the near cache(i.e. this is the "low watermark" value)
119+ // this value is in the range 0.1 to 1.0 and the default is 0.8 or 80%.
120+ PruneFactor float32
110121}
111122
112123func (n NearCacheOptions ) String () string {
113- return fmt .Sprintf ("NearCacheOptions{TTL=%v, HighUnits=%v, HighUnitsMemory=%v, invalidationStrategy=%v}" ,
114- n .TTL , n .HighUnits , n .HighUnitsMemory , getInvalidationStrategyString (n .InvalidationStrategy ))
124+ return fmt .Sprintf ("NearCacheOptions{TTL=%v, HighUnits=%v, HighUnitsMemory=%v, PruneFactor=%.2f, invalidationStrategy=%v}" ,
125+ n .TTL , n .HighUnits , n .HighUnitsMemory , n . PruneFactor , getInvalidationStrategyString (n .InvalidationStrategy ))
115126}
116127
117128// WithExpiry returns a function to set the default expiry for a [NamedCache]. This option is not valid on [NamedMap].
@@ -134,6 +145,7 @@ func executeClear[K comparable, V any](ctx context.Context, bc *baseClient[K, V]
134145 err = bc .ensureClientConnection ()
135146 nearCache = bc .nearCache
136147 )
148+
137149 if err != nil {
138150 return err
139151 }
@@ -152,7 +164,7 @@ func executeClear[K comparable, V any](ctx context.Context, bc *baseClient[K, V]
152164 }
153165
154166 // clear the near cache
155- if nearCache != nil {
167+ if bc . session . GetProtocolVersion () == 0 && nearCache != nil {
156168 nearCache .Clear ()
157169 }
158170
@@ -272,7 +284,7 @@ func executeTruncate[K comparable, V any](ctx context.Context, bc *baseClient[K,
272284 _ , err = bc .client .Truncate (newCtx , & request )
273285 }
274286
275- // clear the near cache
287+ // clear the near cache as the lifecycle listeners are not synchronous
276288 if nearCache != nil {
277289 nearCache .Clear ()
278290 }
@@ -1396,8 +1408,8 @@ func executePutAll[K comparable, V any](ctx context.Context, bc *baseClient[K, V
13961408 }
13971409 }
13981410
1399- // if we have near cache and the entry exists then update
1400- if nearCache != nil {
1411+ // if we have near cache and the entry exists then update (gRPC v0)
1412+ if bc . session . GetProtocolVersion () == 0 && nearCache != nil {
14011413 for k , v := range entries {
14021414 if oldVal := nearCache .Get (k ); oldVal != nil {
14031415 nearCache .Put (k , v )
@@ -1510,8 +1522,8 @@ func executePutWithExpiry[K comparable, V any](ctx context.Context, bc *baseClie
15101522 }
15111523
15121524 // if we have near cache and the entry exists then update this as well because we do
1513- // not use synchronous listener like we do on Java
1514- if nearCache != nil {
1525+ // not use synchronous listener in gRPC v0
1526+ if bc . session . GetProtocolVersion () == 0 && nearCache != nil {
15151527 if oldValue := nearCache .Get (key ); oldValue != nil {
15161528 nearCache .Put (key , value )
15171529 }
@@ -1580,7 +1592,7 @@ func executeRemove[K comparable, V any](ctx context.Context, bc *baseClient[K, V
15801592 }
15811593 }
15821594
1583- if nearCache != nil {
1595+ if bc . session . GetProtocolVersion () == 0 && nearCache != nil {
15841596 nearCache .Remove (key )
15851597 }
15861598
@@ -1632,7 +1644,7 @@ func executeRemoveMapping[K comparable, V any](ctx context.Context, bc *baseClie
16321644 }
16331645 }
16341646
1635- if result .Value && nearCache != nil {
1647+ if bc . session . GetProtocolVersion () == 0 && result .Value && nearCache != nil {
16361648 nearCache .Remove (key )
16371649 }
16381650
@@ -1738,7 +1750,7 @@ func executeReplaceMapping[K comparable, V any](ctx context.Context, bc *baseCli
17381750 }
17391751 }
17401752
1741- if nearCache != nil && result .Value {
1753+ if bc . session . GetProtocolVersion () == 0 && nearCache != nil && result .Value {
17421754 if old := nearCache .Get (key ); old != nil {
17431755 nearCache .Put (key , newValue )
17441756 }
0 commit comments