Class PersistentDataStoreBuilder

  • All Implemented Interfaces:
    ComponentConfigurer<DataStore>

    public abstract class PersistentDataStoreBuilder
    extends java.lang.Object
    implements ComponentConfigurer<DataStore>
    A configurable factory for a persistent data store.

    Several database integrations exist for the LaunchDarkly SDK, each with its own behavior and options specific to that database; this is described via some implementation of PersistentDataStore. There is also universal behavior that the SDK provides for all persistent data stores, such as caching; the PersistentDataStoreBuilder adds this.

    After configuring this object, pass it to LDConfig.Builder.dataStore(ComponentConfigurer) to use it in the SDK configuration. For example, using the Redis integration:

    
         LDConfig config = new LDConfig.Builder()
             .dataStore(
                 Components.persistentDataStore(
                     Redis.dataStore().url("redis://my-redis-host")
                 ).cacheSeconds(15)
             )
             .build();
     
    In this example, .url() is an option specifically for the Redis integration, whereas cacheSeconds() is an option that can be used for any persistent data store.

    Note that this class is abstract; the actual implementation is created by calling Components.persistentDataStore(ComponentConfigurer).

    Since:
    4.12.0
    • Field Detail

      • DEFAULT_CACHE_TTL

        public static final java.time.Duration DEFAULT_CACHE_TTL
        The default value for the cache TTL.
      • cacheTime

        protected java.time.Duration cacheTime
      • recordCacheStats

        protected boolean recordCacheStats
    • Constructor Detail

      • PersistentDataStoreBuilder

        protected PersistentDataStoreBuilder​(ComponentConfigurer<PersistentDataStore> persistentDataStoreConfigurer)
        Creates a new builder.
        Parameters:
        persistentDataStoreConfigurer - the factory implementation for the specific data store type
    • Method Detail

      • noCaching

        public PersistentDataStoreBuilder noCaching()
        Specifies that the SDK should not use an in-memory cache for the persistent data store. This means that every feature flag evaluation will trigger a data store query.
        Returns:
        the builder
      • cacheTime

        public PersistentDataStoreBuilder cacheTime​(java.time.Duration cacheTime)
        Specifies the cache TTL. Items will be evicted or refreshed (depending on the StaleValuesPolicy) after this amount of time from the time when they were originally cached.

        If the value is zero, caching is disabled (equivalent to noCaching()).

        If the value is negative, data is cached forever (equivalent to cacheForever()).

        Parameters:
        cacheTime - the cache TTL; null to use the default
        Returns:
        the builder
      • cacheForever

        public PersistentDataStoreBuilder cacheForever()
        Specifies that the in-memory cache should never expire. In this mode, data will be written to both the underlying persistent store and the cache, but will only ever be read from the persistent store if the SDK is restarted.

        Use this mode with caution: it means that in a scenario where multiple processes are sharing the database, and the current process loses connectivity to LaunchDarkly while other processes are still receiving updates and writing them to the database, the current process will have stale data.

        Returns:
        the builder
      • recordCacheStats

        public PersistentDataStoreBuilder recordCacheStats​(boolean recordCacheStats)
        Enables monitoring of the in-memory cache.

        If set to true, this makes caching statistics available through the DataStoreStatusProvider that you can obtain from the client instance. This may slightly decrease performance, due to the need to record statistics for each cache operation.

        By default, it is false: statistics will not be recorded and the DataStoreStatusProvider.getCacheStats() method will return null.
        Parameters:
        recordCacheStats - true to record caching statiistics
        Returns:
        the builder
        Since:
        5.0.0