1515///**************************************************************************/
1616
1717import 'package:optimizely_flutter_sdk/optimizely_flutter_sdk.dart' ;
18+ import 'package:optimizely_flutter_sdk_example/custom_logger.dart' ;
19+
1820
1921/// CMAB (Contextual Multi-Armed Bandit) API usage examples
2022///
@@ -25,7 +27,7 @@ import 'package:optimizely_flutter_sdk/optimizely_flutter_sdk.dart';
2527/// - Combining CMAB with other decide options
2628class CmabSampleApi {
2729 // Replace with your actual SDK key
28- static const String SDK_KEY = 'Q9LjjQmUn5pDCjKMc1jFC ' ;
30+ static const String SDK_KEY = '2ExWptsTiSx1EZbpwSnoD ' ;
2931
3032 // Replace with your CMAB-enabled flag key
3133 static const String CMAB_FLAG_KEY = 'cmab-flag' ;
@@ -289,6 +291,65 @@ class CmabSampleApi {
289291 }
290292 }
291293
294+ static Future <void > testInvalidateUserCmabCacheOption () async {
295+ try {
296+ final sdk = OptimizelyFlutterSdk (SDK_KEY , cmabConfig: CmabConfig (), defaultLogLevel: OptimizelyLogLevel .debug, logger: CustomLogger ());
297+ await sdk.initializeClient ();
298+
299+ // Create two users and populate cache
300+ const Map <String , dynamic > cmabAttrUS = {'country' : 'us' };
301+ const user1Id = 'user_invalidate_1' ;
302+ final user1Context = (await sdk.createUserContext (
303+ userId: user1Id,
304+ attributes: cmabAttrUS,
305+ ))! ;
306+
307+ const user2Id = 'user_invalidate_2' ;
308+ final user2Context = (await sdk.createUserContext (
309+ userId: user2Id,
310+ attributes: cmabAttrUS,
311+ ))! ;
312+
313+ // Populate cache for both users
314+ await user1Context.decideAsync (
315+ CMAB_FLAG_KEY ,
316+ {
317+ OptimizelyDecideOption .ignoreUserProfileService,
318+ OptimizelyDecideOption .includeReasons,
319+ },
320+ );
321+
322+ await user2Context.decideAsync (
323+ CMAB_FLAG_KEY ,
324+ {
325+ OptimizelyDecideOption .ignoreUserProfileService,
326+ OptimizelyDecideOption .includeReasons,
327+ },
328+ );
329+
330+ // Invalidate cache for user1 only
331+ await user1Context.decideAsync (
332+ CMAB_FLAG_KEY ,
333+ {
334+ OptimizelyDecideOption .ignoreUserProfileService,
335+ OptimizelyDecideOption .includeReasons,
336+ OptimizelyDecideOption .invalidateUserCmabCache,
337+ },
338+ );
339+
340+ // User2's call should still use cache (not affected by user1 invalidation)
341+ await user2Context.decideAsync (
342+ CMAB_FLAG_KEY ,
343+ {
344+ OptimizelyDecideOption .ignoreUserProfileService,
345+ OptimizelyDecideOption .includeReasons,
346+ },
347+ );
348+
349+ } catch (e) {
350+ print ('Error in testInvalidateUserCmabCacheOption: $e ' );
351+ }
352+ }
292353 /// Run all CMAB examples sequentially
293354 ///
294355 /// This runs all the example methods in order, demonstrating
@@ -313,6 +374,9 @@ class CmabSampleApi {
313374
314375 await combinedOptionsExample ();
315376
377+ await Future .delayed (Duration (seconds: 1 ));
378+ await testInvalidateUserCmabCacheOption ();
379+
316380 print ('\n ╔════════════════════════════════════════════════════════╗' );
317381 print ('║ All CMAB Examples Completed Successfully! ✓ ║' );
318382 print ('╚════════════════════════════════════════════════════════╝\n ' );
0 commit comments