All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.launchdarkly.client.integrations.RedisDataStoreBuilder Maven / Gradle / Ivy

package com.launchdarkly.client.integrations;

import com.google.common.base.Joiner;
import com.launchdarkly.client.LDConfig;
import com.launchdarkly.client.interfaces.DiagnosticDescription;
import com.launchdarkly.client.interfaces.PersistentDataStoreFactory;
import com.launchdarkly.client.utils.FeatureStoreCore;
import com.launchdarkly.client.value.LDValue;

import java.net.URI;
import java.util.concurrent.TimeUnit;

import static com.google.common.base.Preconditions.checkNotNull;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;

/**
 * A builder for configuring the Redis-based persistent data store.
 * 

* Obtain an instance of this class by calling {@link Redis#dataStore()}. After calling its methods * to specify any desired custom settings, you can pass it directly into the SDK configuration with * {@link com.launchdarkly.client.LDConfig.Builder#dataStore(com.launchdarkly.client.FeatureStoreFactory)}. * You do not need to call {@link #createPersistentDataStore()} yourself to build the actual data store; that * will be done by the SDK. *

* Builder calls can be chained, for example: * *


   *     LDConfig config = new LDConfig.Builder()
   *         .dataStore(
   *             Components.persistentDataStore(
   *                 Redis.dataStore()
   *                     .url("redis://my-redis-host")
   *                     .database(1)
   *             ).cacheSeconds(15)
   *         )
   *         .build();
 * 
* * @since 4.12.0 */ public final class RedisDataStoreBuilder implements PersistentDataStoreFactory, DiagnosticDescription { /** * The default value for the Redis URI: {@code redis://localhost:6379} */ public static final URI DEFAULT_URI = makeDefaultRedisURI(); /** * The default value for {@link #prefix(String)}. */ public static final String DEFAULT_PREFIX = "launchdarkly"; URI uri = DEFAULT_URI; String prefix = DEFAULT_PREFIX; int connectTimeout = Protocol.DEFAULT_TIMEOUT; int socketTimeout = Protocol.DEFAULT_TIMEOUT; Integer database = null; String password = null; boolean tls = false; JedisPoolConfig poolConfig = null; private static URI makeDefaultRedisURI() { // This ungainly logic is a workaround for the overly aggressive behavior of the Shadow plugin, which // wants to transform any string literal starting with "redis" because the root package of Jedis is // "redis". return URI.create(Joiner.on("").join("r", "e", "d", "i", "s") + "://localhost:6379"); } // These constructors are called only from Implementations RedisDataStoreBuilder() { } /** * Specifies the database number to use. *

* The database number can also be specified in the Redis URI, in the form {@code redis://host:port/NUMBER}. Any * non-null value that you set with {@link #database(Integer)} will override the URI. * * @param database the database number, or null to fall back to the URI or the default * @return the builder */ public RedisDataStoreBuilder database(Integer database) { this.database = database; return this; } /** * Specifies a password that will be sent to Redis in an AUTH command. *

* It is also possible to include a password in the Redis URI, in the form {@code redis://:PASSWORD@host:port}. Any * password that you set with {@link #password(String)} will override the URI. * * @param password the password * @return the builder */ public RedisDataStoreBuilder password(String password) { this.password = password; return this; } /** * Optionally enables TLS for secure connections to Redis. *

* This is equivalent to specifying a Redis URI that begins with {@code rediss:} rather than {@code redis:}. *

* Note that not all Redis server distributions support TLS. * * @param tls true to enable TLS * @return the builder */ public RedisDataStoreBuilder tls(boolean tls) { this.tls = tls; return this; } /** * Specifies a Redis host URI other than {@link #DEFAULT_URI}. * * @param redisUri the URI of the Redis host * @return the builder */ public RedisDataStoreBuilder uri(URI redisUri) { this.uri = checkNotNull(redisUri); return this; } /** * Optionally configures the namespace prefix for all keys stored in Redis. * * @param prefix the namespace prefix * @return the builder */ public RedisDataStoreBuilder prefix(String prefix) { this.prefix = prefix; return this; } /** * Optional override if you wish to specify your own configuration to the underlying Jedis pool. * * @param poolConfig the Jedis pool configuration. * @return the builder */ public RedisDataStoreBuilder poolConfig(JedisPoolConfig poolConfig) { this.poolConfig = poolConfig; return this; } /** * Optional override which sets the connection timeout for the underlying Jedis pool which otherwise defaults to * {@link redis.clients.jedis.Protocol#DEFAULT_TIMEOUT} * * @param connectTimeout the timeout * @param timeUnit the time unit for the timeout * @return the builder */ public RedisDataStoreBuilder connectTimeout(int connectTimeout, TimeUnit timeUnit) { this.connectTimeout = (int) timeUnit.toMillis(connectTimeout); return this; } /** * Optional override which sets the connection timeout for the underlying Jedis pool which otherwise defaults to * {@link redis.clients.jedis.Protocol#DEFAULT_TIMEOUT} * * @param socketTimeout the socket timeout * @param timeUnit the time unit for the timeout * @return the builder */ public RedisDataStoreBuilder socketTimeout(int socketTimeout, TimeUnit timeUnit) { this.socketTimeout = (int) timeUnit.toMillis(socketTimeout); return this; } @Override public FeatureStoreCore createPersistentDataStore() { return new RedisDataStoreImpl(this); } @Override public LDValue describeConfiguration(LDConfig config) { return LDValue.of("Redis"); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy