io.lettuce.core.ReadFrom Java Examples

The following examples show how to use io.lettuce.core.ReadFrom. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProtobufRedisLoadingCache.java    From curiostack with MIT License 6 votes vote down vote up
/**
 * Constructs a new {@link ProtobufRedisLoadingCache} that can write protobuf {@link Message}
 * keys and values to remoteCache, with an optional local cache layer.
 *
 * @param name name of this cache, will be prefixed onto all keys.
 * @param keyPrototype a prototype for the key {@link Message}, usually gotten from {@code
 *     Key.getDefaultInstance()}.
 * @param valuePrototype a prototype for the value {@link Message}, usually gotten from {@code
 *     Value.getDefaultInstance()}.
 * @param redisTtl the time until expiration of a value in the remoteCache cache. The local
 *     cache should be considered in localCacheSpec.
 * @param redisMasterOnly whether remoteCache reads should only happen from master. Best-effort,
 *     temporary persistent storage should set this to {@code true}.
 * @param localCacheSpec a {@link CaffeineSpec} to control the local cache layer. If {@code
 *     null}, local caching will be disabled.
 */
// TODO(choko): Fix this later since it breaks the API.
@SuppressWarnings("InconsistentOverloads")
public <K extends Message, V extends Message> ProtobufRedisLoadingCache<K, V> create(
    String name,
    K keyPrototype,
    V valuePrototype,
    Duration redisTtl,
    boolean redisMasterOnly,
    @Nullable CaffeineSpec localCacheSpec) {
  return new ProtobufRedisLoadingCache<>(
      keyPrototype,
      valuePrototype,
      redisTtl,
      localCacheSpec,
      config.isNoop()
          ? new NoopRemoteCache<>()
          : config.isCluster()
              ? createRedisRemoteCache(
                  name,
                  redisClusterClient.get(),
                  keyPrototype,
                  valuePrototype,
                  redisMasterOnly ? ReadFrom.MASTER : ReadFrom.NEAREST)
              : createRedisRemoteCache(name, redisClient.get(), keyPrototype, valuePrototype));
}
 
Example #2
Source File: RedisLettuceCacheTest.java    From jetcache with Apache License 2.0 6 votes vote down vote up
@Test
public void testSentinel2() throws Exception {
    RedisURI redisUri = RedisURI.Builder
            .sentinel("127.0.0.1", 26379, "mymaster")
            .withSentinel("127.0.0.1", 26380)
            .withSentinel("127.0.0.1", 26381)
            .build();
    RedisClient client = RedisClient.create();
    StatefulRedisMasterSlaveConnection con = MasterSlave.connect(
            client, new JetCacheCodec(), redisUri);
    con.setReadFrom(ReadFrom.SLAVE_PREFERRED);
    cache = RedisLettuceCacheBuilder.createRedisLettuceCacheBuilder()
            .redisClient(client)
            .connection(con)
            .keyPrefix(new Random().nextInt() + "")
            .buildCache();
    cache.put("K1", "V1");
    Thread.sleep(100);
    Assert.assertEquals("V1", cache.get("K1"));
}
 
Example #3
Source File: RedisLettuceCacheTest.java    From jetcache with Apache License 2.0 6 votes vote down vote up
@Test
public void testCluster2() throws Exception {
    if (!checkOS()) {
        return;
    }
    RedisURI node1 = RedisURI.create("127.0.0.1", 7000);
    RedisURI node2 = RedisURI.create("127.0.0.1", 7001);
    RedisURI node3 = RedisURI.create("127.0.0.1", 7002);
    RedisClusterClient client = RedisClusterClient.create(Arrays.asList(node1, node2, node3));
    StatefulRedisClusterConnection con = client.connect(new JetCacheCodec());
    con.setReadFrom(ReadFrom.SLAVE_PREFERRED);
    cache = RedisLettuceCacheBuilder.createRedisLettuceCacheBuilder()
            .redisClient(client)
            .connection(con)
            .keyPrefix(new Random().nextInt() + "")
            .buildCache();
    cache.put("K1", "V1");
    Thread.sleep(100);
    Assert.assertEquals("V1", cache.get("K1"));
}
 
Example #4
Source File: ProtobufRedisLoadingCache.java    From curiostack with MIT License 5 votes vote down vote up
private <K extends Message, V extends Message> RemoteCache<K, V> createRedisRemoteCache(
    String name,
    RedisClusterClient redisClient,
    K keyPrototype,
    V valuePrototype,
    ReadFrom readFrom) {
  StatefulRedisClusterConnection<K, V> connection =
      redisClient.connect(
          new ProtobufRedisCodec<>(
              (name + ":").getBytes(StandardCharsets.UTF_8), keyPrototype, valuePrototype));
  connection.setReadFrom(readFrom);
  return new RedisRemoteCache<>(connection.async(), name, meterRegistry);
}
 
Example #5
Source File: LettuceSessionManager.java    From redis-session-manager with Apache License 2.0 5 votes vote down vote up
private GenericObjectPool<StatefulRedisConnection<String, Object>> createPool(List<String> nodes, RedisCodec<String, Object> codec) {
    GenericObjectPoolConfig<StatefulRedisConnection<String, Object>> cfg = new GenericObjectPoolConfig<>();
    cfg.setTestOnBorrow(true);
    cfg.setMinEvictableIdleTimeMillis(TimeUnit.MINUTES.toMillis(5));
    cfg.setMaxTotal(getMaxConnPoolSize());
    cfg.setMinIdle(getMinConnPoolSize());

    if (nodes.size() == 1) {
        return ConnectionPoolSupport.createGenericObjectPool(
            () -> client.connect(codec, RedisURI.create(nodes.get(0))),
            cfg
        );
    } else {
        List<RedisURI> uris = nodes.stream()
            .map(RedisURI::create)
            .collect(Collectors.toList());
        return ConnectionPoolSupport.createGenericObjectPool(
            () -> {
                StatefulRedisMasterReplicaConnection<String, Object> connection =
                    MasterReplica.connect(client, codec, uris);
                connection.setReadFrom(ReadFrom.MASTER_PREFERRED);
                return connection;
            },
            cfg
        );
    }
}
 
Example #6
Source File: RedisCacheImpl.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
private void connectToMasterSlave(String[] servers) {
			
       redisClient = RedisClient.create();

       List<RedisURI> nodes = new ArrayList<>();
       for(String s: servers) {
       	RedisURI sRedisURI = RedisURI.create(s);
       	nodes.add(sRedisURI);
       }

       StatefulRedisMasterSlaveConnection<String, String> connection = MasterSlave
               .connect(redisClient, new Utf8StringCodec(), nodes);
       connection.setReadFrom(ReadFrom.MASTER_PREFERRED);
	
}
 
Example #7
Source File: TracingStatefulRedisClusterConnection.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public void setReadFrom(ReadFrom readFrom) {
  connection.setReadFrom(readFrom);
}
 
Example #8
Source File: TracingStatefulRedisClusterConnection.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public ReadFrom getReadFrom() {
  return connection.getReadFrom();
}
 
Example #9
Source File: TracingStatefulRedisClusterConnection.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public void setReadFrom(ReadFrom readFrom) {
  connection.setReadFrom(readFrom);
}
 
Example #10
Source File: TracingStatefulRedisClusterConnection.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public ReadFrom getReadFrom() {
  return connection.getReadFrom();
}
 
Example #11
Source File: TracingStatefulRedisClusterConnection.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public void setReadFrom(ReadFrom readFrom) {
  connection.setReadFrom(readFrom);
}
 
Example #12
Source File: TracingStatefulRedisClusterConnection.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public ReadFrom getReadFrom() {
  return connection.getReadFrom();
}