@@ -32,9 +32,10 @@ import (
3232
3333 "github.com/optimizely/agent/config"
3434 "github.com/optimizely/agent/pkg/syncer"
35+ "github.com/optimizely/agent/plugins/cmabcache"
3536 "github.com/optimizely/agent/plugins/odpcache"
3637 "github.com/optimizely/agent/plugins/userprofileservice"
37- odpCachePkg "github.com/optimizely/go-sdk/v2/pkg/cache"
38+ cachePkg "github.com/optimizely/go-sdk/v2/pkg/cache"
3839 "github.com/optimizely/go-sdk/v2/pkg/client"
3940 "github.com/optimizely/go-sdk/v2/pkg/cmab"
4041 sdkconfig "github.com/optimizely/go-sdk/v2/pkg/config"
@@ -52,6 +53,7 @@ import (
5253const (
5354 userProfileServicePlugin = "UserProfileService"
5455 odpCachePlugin = "ODP Cache"
56+ cmabCachePlugin = "CMAB Cache"
5557)
5658
5759// OptlyCache implements the Cache interface backed by a concurrent map.
@@ -61,6 +63,7 @@ type OptlyCache struct {
6163 optlyMap cmap.ConcurrentMap
6264 userProfileServiceMap cmap.ConcurrentMap
6365 odpCacheMap cmap.ConcurrentMap
66+ cmabCacheMap cmap.ConcurrentMap
6467 ctx context.Context
6568 wg sync.WaitGroup
6669}
@@ -75,13 +78,15 @@ func NewCache(ctx context.Context, conf config.AgentConfig, metricsRegistry *Met
7578
7679 userProfileServiceMap := cmap .New ()
7780 odpCacheMap := cmap .New ()
81+ cmabCacheMap := cmap .New ()
7882 cache := & OptlyCache {
7983 ctx : ctx ,
8084 wg : sync.WaitGroup {},
81- loader : defaultLoader (conf , metricsRegistry , tracer , userProfileServiceMap , odpCacheMap , cmLoader , event .NewBatchEventProcessor ),
85+ loader : defaultLoader (conf , metricsRegistry , tracer , userProfileServiceMap , odpCacheMap , cmabCacheMap , cmLoader , event .NewBatchEventProcessor ),
8286 optlyMap : cmap .New (),
8387 userProfileServiceMap : userProfileServiceMap ,
8488 odpCacheMap : odpCacheMap ,
89+ cmabCacheMap : cmabCacheMap ,
8590 }
8691
8792 return cache
@@ -155,6 +160,11 @@ func (c *OptlyCache) SetODPCache(sdkKey, odpCache string) {
155160 c .odpCacheMap .SetIfAbsent (sdkKey , odpCache )
156161}
157162
163+ // SetCMABCache sets CMAB cache for the given sdkKey
164+ func (c * OptlyCache ) SetCMABCache (sdkKey , cmabCache string ) {
165+ c .cmabCacheMap .SetIfAbsent (sdkKey , cmabCache )
166+ }
167+
158168// Wait for all optimizely clients to gracefully shutdown
159169func (c * OptlyCache ) Wait () {
160170 c .wg .Wait ()
@@ -178,6 +188,7 @@ func defaultLoader(
178188 tracer trace.Tracer ,
179189 userProfileServiceMap cmap.ConcurrentMap ,
180190 odpCacheMap cmap.ConcurrentMap ,
191+ cmabCacheMap cmap.ConcurrentMap ,
181192 pcFactory func (sdkKey string , options ... sdkconfig.OptionFunc ) SyncedConfigManager ,
182193 bpFactory func (options ... event.BPOptionConfig ) * event.BatchEventProcessor ) func (clientKey string ) (* OptlyClient , error ) {
183194 clientConf := agentConf .Client
@@ -276,12 +287,12 @@ func defaultLoader(
276287 }
277288 }
278289
279- var clientODPCache odpCachePkg .Cache
290+ var clientODPCache cachePkg .Cache
280291 var rawODPCache = getServiceWithType (odpCachePlugin , sdkKey , odpCacheMap , clientConf .ODP .SegmentsCache )
281292 // Check if odp cache was provided by user
282293 if rawODPCache != nil {
283294 // convert odpCache to Cache interface
284- if convertedODPCache , ok := rawODPCache .(odpCachePkg .Cache ); ok && convertedODPCache != nil {
295+ if convertedODPCache , ok := rawODPCache .(cachePkg .Cache ); ok && convertedODPCache != nil {
285296 clientODPCache = convertedODPCache
286297 }
287298 }
@@ -322,21 +333,20 @@ func defaultLoader(
322333 log .Info ().Str ("endpoint" , cmabEndpoint ).Msg ("Using custom CMAB prediction endpoint" )
323334 }
324335
325- // Parse CMAB cache configuration
326- cacheSize := clientConf . CMAB . Cache . Size
327- if cacheSize == 0 {
328- cacheSize = cmab . DefaultCacheSize
329- }
330-
331- cacheTTL := clientConf . CMAB . Cache . TTL
332- if cacheTTL == 0 {
333- cacheTTL = cmab . DefaultCacheTTL
336+ // Get CMAB cache from service configuration
337+ var clientCMABCache cachePkg. CacheWithRemove
338+ var rawCMABCache = getServiceWithType ( cmabCachePlugin , sdkKey , cmabCacheMap , clientConf . CMAB . Cache )
339+ // Check if CMAB cache was provided by user
340+ if rawCMABCache != nil {
341+ // convert cmabCache to CacheWithRemove interface
342+ if convertedCMABCache , ok := rawCMABCache .(cachePkg. CacheWithRemove ); ok && convertedCMABCache != nil {
343+ clientCMABCache = convertedCMABCache
344+ }
334345 }
335346
336- // Create CMAB config using client API (RetryConfig now handled internally by go-sdk)
347+ // Create CMAB config using client API with custom cache
337348 cmabConfig := client.CmabConfig {
338- CacheSize : cacheSize ,
339- CacheTTL : cacheTTL ,
349+ Cache : clientCMABCache ,
340350 HTTPTimeout : clientConf .CMAB .RequestTimeout ,
341351 }
342352
@@ -366,6 +376,10 @@ func getServiceWithType(serviceType, sdkKey string, serviceMap cmap.ConcurrentMa
366376 if odpCreator , ok := odpcache .Creators [serviceName ]; ok {
367377 serviceInstance = odpCreator ()
368378 }
379+ case cmabCachePlugin :
380+ if cmabCreator , ok := cmabcache .Creators [serviceName ]; ok && cmabCreator != nil {
381+ serviceInstance = cmabCreator ()
382+ }
369383 default :
370384 }
371385
0 commit comments