This topic explains how context caching to local storage works for SDKs that support the feature.
Most newer client SDKs use local storage to cache flag values for unique contexts by default. On subsequent launches, the SDK reads the cached values before any network connection is established. The client still initializes and connects to LaunchDarkly’s service to fetch the most recent flag values, but it is not dependent on those values to reach its ready state. The SDK updates values in the background after connecting to LaunchDarkly.
Some SDKs let you configure the maximum number of unique contexts that can be cached locally by setting maxCachedContexts at startup. This allows you to control the amount of local storage used for storing contexts.
When you call identify, the number of cached contexts increments. If the number of cached contexts exceeds maxCachedContexts (5 contexts by default), the SDK deletes context data in excess of maxCachedContexts, starting with the oldest context first.
For SDKs that support configuration, set maxCachedContexts to 0 to disable caching flag values for contexts in local storage and clear any stored data. Setting disableCache to true disables caching context data to local storage but leaves any existing local storage in place.
These SDKs enable local storage caching by default, and you can optionally configure the size of the cache as necessary:
Some SDKs use the same implementation for both bootstrapping and local storage caching. For these SDKs you cannot configure both bootstrapped flag values and local storage caching. The following SDKs provide the option to use either bootstrap flag values or enable local storage caching, but not both:
For the above SDKs, local storage is not enabled by default. You can optionally enable local storage caching, but you cannot configure the cache size.
If you set bootstrap to the string "localStorage", the client tries to get flag values from persistent storage, using a unique key that is based on the user properties.
In Electron, persistent storage consists of files in the userData directory. If the client finds flag values stored for this user, it uses them and starts up immediately in a ready state, but also makes a background request to LaunchDarkly to get the latest values and stores them as soon as it receives them.
You can choose to enable caching to local storage, but you cannot configure the cache size.
Here’s how to use this mode:
If you enable local storage you cannot also provide bootstrap flag values. To learn more, read Bootstrapping.
The JavaScript client SDK v4.x stores flag values for unique contexts in local storage by default, with no configuration required. You can optionally set the maximum number of unique contexts cached locally by setting maxCachedContexts at startup.
To learn more, read Interface LDOptions.
JavaScript SDK v3.x requires you to enable caching contexts to local storage at initialization using the bootstrap option, and does not provide the ability to configure the cache size.
Here is an example of how to configure caching to local storage with JavaScript:
If you enable local storage with JavaScript SDK v3.x, you cannot also provide bootstrap flag values. To learn more, read Bootstrapping.
The Node.js (client-side) SDK requires you to configure local storage caching using the bootstrap option. In this mode, if no values have been cached yet, the SDK obtains flags from LaunchDarkly and then caches them in persistent storage. The next time you start, the cached flags are immediately available, and the SDK also contacts LaunchDarkly in the background to obtain updated values.
You can choose to enable caching to local storage, but you cannot configure the cache size.
To activate this mode, use the special string "localStorage":
If you enable local storage you cannot also provide bootstrap flag values. To learn more, read Bootstrapping.
The React Native SDK stores flag values for unique contexts in AsyncStorage by default, with no configuration required. You can optionally set the maximum number of unique contexts cached locally by setting maxCachedContexts at startup.
To learn more, read Interface LDOptions.
Here is an example of how to configure caching to local storage with React Native:
The iOS SDK stores flag values for unique contexts in local storage by default, with no configuration required. You can optionally set the maximum number of unique contexts cached locally by setting maxCachedContexts on your LDConfig before starting the client.
To learn more, read Struct LDConfig.
Here is an example of how to configure caching to local storage with iOS:
The Android SDK stores flag values for unique contexts in SharedPreferences by default, with no configuration required. You can optionally set the maximum number of unique contexts cached locally by setting maxCachedContexts in your LDConfig before starting the client.
To learn more, read Class LDConfig.Builder.
Here is an example of how to configure caching to local storage with Android:
The Flutter SDK stores flag values for unique contexts in on-device storage by default, with no configuration required. You can optionally set the maximum number of unique contexts cached locally by setting maxCachedContexts in a PersistenceConfig passed to your LDConfig.
To learn more, read Class PersistenceConfig.
Here is an example of how to configure caching to local storage with Flutter:
The React Web SDK stores flag values for unique contexts in local storage by default, with no configuration required. You can optionally set the maximum number of unique contexts cached locally by setting maxCachedContexts inside ldOptions when you create your provider.
To learn more, read Interface LDReactClientOptions.
Here is an example of how to configure caching to local storage with React Web: