Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

Dev guideRecipesAPI ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Configure the CMAB cache for the Swift SDK

How to configure caching behavior for Contextual Multi-Armed Bandit (CMAB) decisions in the Feature Experimentation Swift SDK.

👍

Beta

CMAB for Feature Experimentation is in beta. Contact your Customer Success Manager for more information.

The CMAB cache stores variation assignments to reduce latency and minimize API calls to the CMAB service.

Prerequisites

  • Swift SDK version v5.2.0+
  • A CMAB-enabled experiment in your Optimizely project
🚧

Important

To retrieve a CMAB decision, you must use the decideAsync method. The decide method does not fetch CMAB decisions to avoid blocking the user.

Minimum SDK version

5.2.0

Description

When a user is bucketed into a CMAB experiment, the SDK makes an API call to the CMAB service to determine which variation to show. To improve performance and reduce latency, the SDK caches these decisions based on:

  • User ID
  • Experiment ID
  • CMAB attribute values

The cache is automatically invalidated when CMAB attributes change for a user, ensuring fresh decisions when context changes.

By default, the SDK uses an in-memory LRU (Least Recently Used) cache with a maximum size of 100 entries and a TTL of 30 minutes (1800 seconds). You can customize these settings.

Configuration options

Configure CMAB caching when initializing your client using a CmabConfig struct.

Default configuration

The Swift SDK uses the following default values when no custom configuration is provided:

SettingDefault ValueDescription
Cache Size100Maximum number of cached decisions.
Cache TTL1800 seconds (30 minutes)Time-to-live for cache entries.
Prediction EndpointpredictionEndpointThe %@ placeholder is replaced with the rule ID. This is useful for proxying.
Cache ImplementationLruCacheThread-safe in-memory Least Recently Used (LRU) cache.

Examples

Basic configuration

Adjust cache size and TTL for your application's needs.

import Foundation
import Optimizely
import UIKit


// Configure CMAB settings
let cmabConfig = CmabConfig(
    cacheSize: 10,
    cacheTimeoutInSecs: 600
)

// Initialize client with custom cmab config
let client = OptimizelyClient(
    sdkKey: "<you_sdk_key>",
    cmabConfig: cmabConfig
)

Task {
    try await client.start()
    let user = OptimizelyUserContext(
        optimizely: client,
        userId: "USER_123",
        attributes: ["country": "us"]
    )
    let decision =  await user.decideAsync(key: "cmab_flag_key")
    // Cache will store this decision for 10 minutes
}

Cache behavior

Automatic cache invalidation

The cache is automatically invalidated when:

  • The cached entry's TTL expires.
  • CMAB attribute values change for a user (detected with MD5 hash comparison).
  • The ignoreCmabCache decide option is used.
  • The invalidateUserCmabCache decide option is used.
  • The resetCmabCache decide option is used.

CMAB-Specific decide options

Control cache behavior on a per-decision basis using decide options.

import Foundation
import Optimizely
import UIKit

// Ignore cache for this decision (always fetch fresh)
let decision =  await user.decideAsync(key: "cmab_flag_key", options: [.ignoreCmabCache])

// Invalidate cached decision for this user and experiment
let decision =  await user.decideAsync(key: "cmab_flag_key", options: [.invalidateUserCmabCache])

// Clear entire CMAB cache
let decision =  await user.decideAsync(key: "cmab_flag_key", options: [.resetCmabCache])

// Combine multiple options
let decision =  await user.decideAsync(key: "cmab_flag_key", options: [.resetCmabCache, .includeReasons])

Available CMAB decide options

Decide optionDescription
ignoreCmabCacheBypass cache and fetch a fresh decision from CMAB service.
invalidateUserCmabCacheRemove cached decision for this user and experiment before deciding.
resetCmabCacheClear all entries from the CMAB cache before deciding.