Search in sources :

Example 6 with ClientOptions

use of io.lettuce.core.ClientOptions in project spring-parent by mingyang66.

the class RedisDbLettuceConnectionConfiguration method createClientOptions.

/**
 * ClientOptions 用于控制客户端行为的客户端选项
 *
 * @return
 */
private ClientOptions createClientOptions() {
    ClientOptions.Builder builder = this.initializeClientOptionsBuilder();
    Duration connectTimeout = this.getProperties().getConnectTimeout();
    if (connectTimeout != null) {
        builder.socketOptions(SocketOptions.builder().connectTimeout(connectTimeout).build());
    }
    return builder.timeoutOptions(TimeoutOptions.enabled()).build();
}
Also used : ClientOptions(io.lettuce.core.ClientOptions) ClusterClientOptions(io.lettuce.core.cluster.ClusterClientOptions) Duration(java.time.Duration)

Example 7 with ClientOptions

use of io.lettuce.core.ClientOptions in project lettuce-core by lettuce-io.

the class ClientsideCachingIntegrationTests method clientCachingResp2.

@Test
void clientCachingResp2() {
    ClientOptions resp2 = ClientOptions.builder().protocolVersion(ProtocolVersion.RESP2).build();
    redisClient.setOptions(resp2);
    StatefulRedisConnection<String, String> data = redisClient.connect();
    RedisCommands<String, String> commands = data.sync();
    StatefulRedisPubSubConnection<String, String> pubSub = redisClient.connectPubSub();
    List<String> invalidations = new CopyOnWriteArrayList<>();
    commands.clientTracking(TrackingArgs.Builder.enabled().redirect(pubSub.sync().clientId()));
    pubSub.addListener(new RedisPubSubAdapter<String, String>() {

        @Override
        public void message(String channel, String message) {
            if (channel.equals("__redis__:invalidate")) {
                invalidations.add(message);
            }
        }
    });
    pubSub.sync().subscribe("__redis__:invalidate");
    commands.get("key1");
    commands.get("key2");
    assertThat(invalidations).isEmpty();
    Map<String, String> keys = new HashMap<>();
    keys.put("key1", "value1");
    keys.put("key2", "value2");
    commands.mset(keys);
    Wait.untilEquals(2, invalidations::size).waitOrTimeout();
    assertThat(invalidations).contains("key1", "key2");
    data.close();
    pubSub.close();
}
Also used : ClientOptions(io.lettuce.core.ClientOptions) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Example 8 with ClientOptions

use of io.lettuce.core.ClientOptions in project lettuce-core by lettuce-io.

the class ClientsideCachingIntegrationTests method clientCachingResp3.

@Test
void clientCachingResp3() {
    ClientOptions resp2 = ClientOptions.builder().protocolVersion(ProtocolVersion.RESP3).build();
    redisClient.setOptions(resp2);
    StatefulRedisConnection<String, String> data = redisClient.connect();
    RedisCommands<String, String> commands = data.sync();
    List<String> invalidations = new CopyOnWriteArrayList<>();
    commands.clientTracking(TrackingArgs.Builder.enabled());
    data.addListener(message -> {
        if (message.getType().equals("invalidate")) {
            invalidations.addAll((List) message.getContent(StringCodec.UTF8::decodeKey).get(1));
        }
    });
    commands.get("key1");
    commands.get("key2");
    assertThat(invalidations).isEmpty();
    Map<String, String> keys = new HashMap<>();
    keys.put("key1", "value1");
    keys.put("key2", "value2");
    commands.mset(keys);
    Wait.untilEquals(2, invalidations::size).waitOrTimeout();
    assertThat(invalidations).contains("key1", "key2");
    data.close();
}
Also used : ClientOptions(io.lettuce.core.ClientOptions) StringCodec(io.lettuce.core.codec.StringCodec) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Aggregations

ClientOptions (io.lettuce.core.ClientOptions)8 ClusterClientOptions (io.lettuce.core.cluster.ClusterClientOptions)4 SocketOptions (io.lettuce.core.SocketOptions)3 HashMap (java.util.HashMap)3 Test (org.junit.jupiter.api.Test)3 RedisClient (io.lettuce.core.RedisClient)2 Duration (java.time.Duration)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Provides (com.google.inject.Provides)1 Singleton (com.google.inject.Singleton)1 AbstractRedisClient (io.lettuce.core.AbstractRedisClient)1 RedisURI (io.lettuce.core.RedisURI)1 ClusterTopologyRefreshOptions (io.lettuce.core.cluster.ClusterTopologyRefreshOptions)1 RedisClusterClient (io.lettuce.core.cluster.RedisClusterClient)1 StatefulRedisClusterConnection (io.lettuce.core.cluster.api.StatefulRedisClusterConnection)1 ByteArrayCodec (io.lettuce.core.codec.ByteArrayCodec)1 StringCodec (io.lettuce.core.codec.StringCodec)1 ClientResources (io.lettuce.core.resource.ClientResources)1 DefaultClientResources (io.lettuce.core.resource.DefaultClientResources)1