Java Code Examples for io.lettuce.core.RedisClient#create()

The following examples show how to use io.lettuce.core.RedisClient#create() . 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: LettuceController.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@RequestMapping("/lettuce-case")
@ResponseBody
public String lettuceCase() {
    RedisClient redisClient = RedisClient.create("redis://" + address);
    StatefulRedisConnection<String, String> connection0 = redisClient.connect();
    RedisCommands<String, String> syncCommand = connection0.sync();
    syncCommand.get("key");

    StatefulRedisConnection<String, String> connection1 = redisClient.connect();
    RedisAsyncCommands<String, String> asyncCommands = connection1.async();
    asyncCommands.setAutoFlushCommands(false);
    List<RedisFuture<?>> futures = new ArrayList<>();
    futures.add(asyncCommands.set("key0", "value0"));
    futures.add(asyncCommands.set("key1", "value1"));
    asyncCommands.flushCommands();
    LettuceFutures.awaitAll(5, TimeUnit.SECONDS, futures.toArray(new RedisFuture[futures.size()]));

    connection0.close();
    connection1.close();
    redisClient.shutdown();
    return "Success";
}
 
Example 2
Source File: RedisDataSource.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private RedisClient getRedisStandaloneClient(RedisConnectionConfig connectionConfig) {
    char[] password = connectionConfig.getPassword();
    String clientName = connectionConfig.getClientName();
    RedisURI.Builder redisUriBuilder = RedisURI.builder();
    redisUriBuilder.withHost(connectionConfig.getHost())
        .withPort(connectionConfig.getPort())
        .withDatabase(connectionConfig.getDatabase())
        .withTimeout(Duration.ofMillis(connectionConfig.getTimeout()));
    if (password != null) {
        redisUriBuilder.withPassword(connectionConfig.getPassword());
    }
    if (StringUtil.isNotEmpty(connectionConfig.getClientName())) {
        redisUriBuilder.withClientName(clientName);
    }
    return RedisClient.create(redisUriBuilder.build());
}
 
Example 3
Source File: RedisRateLimiterConfiguration.java    From daming with Apache License 2.0 6 votes vote down vote up
@Bean
@ConditionalOnMissingBean(value = RequestRateLimiterFactory.class)
public RedisRateLimiterFactory redisRateLimiterFactory(ClientResources redisClientResources, RedisProperties redisProperties) {
    RedisURI.Builder builder = RedisURI.builder()
            .withHost(redisProperties.getHost())
            .withPort(redisProperties.getPort());
    if (!StringUtils.isEmpty(redisProperties.getPassword())) {
        builder = builder.withPassword(redisProperties.getPassword());
    }
    if (null != redisProperties.getTimeout()) {
        builder = builder.withTimeout(redisProperties.getTimeout());
    }
    builder = builder.withDatabase(redisProperties.getDatabase())
            .withSsl(redisProperties.isSsl());
    RedisURI redisUri = builder.build();
    return new RedisRateLimiterFactory(RedisClient.create(redisClientResources, redisUri));
}
 
Example 4
Source File: TracingLettuce51Test.java    From java-redis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void pubSub() {
  RedisClient client = RedisClient.create("redis://localhost");
  StatefulRedisPubSubConnection<String, String> connection =
      new TracingStatefulRedisPubSubConnection<>(client.connectPubSub(),
          new TracingConfiguration.Builder(mockTracer).build());

  connection.addListener(new RedisPubSubAdapter<>());

  RedisPubSubCommands<String, String> commands = connection.sync();
  commands.subscribe("channel");

  final RedisCommands<String, String> commands2 = client.connect().sync();
  commands2.publish("channel", "msg");

  client.shutdown();

  await().atMost(15, TimeUnit.SECONDS).until(reportedSpansSize(), equalTo(3));

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(3, spans.size());
}
 
Example 5
Source File: TracingLettuce51Test.java    From java-redis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void sync() {
  RedisClient client = RedisClient.create("redis://localhost");

  StatefulRedisConnection<String, String> connection =
      new TracingStatefulRedisConnection<>(client.connect(),
          new TracingConfiguration.Builder(mockTracer).build());
  RedisCommands<String, String> commands = connection.sync();

  assertEquals("OK", commands.set("key", "value"));
  assertEquals("value", commands.get("key"));

  connection.close();

  client.shutdown();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 6
Source File: LettuceConnectionManagerTest.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    RedisClient client = RedisClient.create("redis://127.0.0.1");
    LettuceConnectionManager m = LettuceConnectionManager.defaultManager();
    m.init(client, null);
    Assert.assertSame(m.commands(client), m.commands(client));
    Assert.assertSame(m.asyncCommands(client), m.asyncCommands(client));
    Assert.assertSame(m.reactiveCommands(client), m.reactiveCommands(client));
    m.removeAndClose(client);
}
 
Example 7
Source File: RequiresRedisSentinel.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
private boolean isAvailable(RedisNode node) {

		RedisClient redisClient = RedisClient.create(ManagedClientResources.getClientResources(),
				RedisURI.create(node.getHost(), node.getPort()));

		try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
			connection.sync().ping();
		} catch (Exception e) {
			return false;
		} finally {
			redisClient.shutdown(Duration.ZERO, Duration.ZERO);
		}

		return true;
	}
 
Example 8
Source File: LettuceBenchmark.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp(Blackhole blackhole) throws IOException {
    super.setUp(blackhole);
    int redisPort = getAvailablePort();
    server = RedisServer.builder()
        .setting("bind 127.0.0.1")
        .port(redisPort)
        .build();
    server.start();
    RedisClient client = RedisClient.create(RedisURI.create("localhost", redisPort));
    connection = client.connect();
    sync = connection.sync();
    sync.set("foo", "bar");
}
 
Example 9
Source File: RedisLettuceCacheTest.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Test
public void testSentinel() 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(redisUri);
    test(client, null);
}
 
Example 10
Source File: RedisKeyValueStoreTest.java    From cava with Apache License 2.0 5 votes vote down vote up
@Test
void testPutAndGet(@RedisPort Integer redisPort) throws Exception {
  KeyValueStore store = RedisKeyValueStore.open(redisPort);
  AsyncCompletion completion = store.putAsync(Bytes.of(123), Bytes.of(10, 12, 13));
  completion.join();
  Bytes value = store.getAsync(Bytes.of(123)).get();
  assertNotNull(value);
  assertEquals(Bytes.of(10, 12, 13), value);
  RedisClient client =
      RedisClient.create(RedisURI.create(InetAddress.getLoopbackAddress().getHostAddress(), redisPort));
  try (StatefulRedisConnection<Bytes, Bytes> conn = client.connect(new RedisBytesCodec())) {
    assertEquals(Bytes.of(10, 12, 13), conn.sync().get(Bytes.of(123)));
  }
}
 
Example 11
Source File: TracingLettuce51Test.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Test
public void async() throws Exception {
  RedisClient client = RedisClient.create("redis://localhost");

  StatefulRedisConnection<String, String> connection =
      new TracingStatefulRedisConnection<>(client.connect(),
          new TracingConfiguration.Builder(mockTracer).build());

  RedisAsyncCommands<String, String> commands = connection.async();

  assertEquals("OK", commands.set("key2", "value2").get(15, TimeUnit.SECONDS));

  assertEquals("value2", commands.get("key2").get(15, TimeUnit.SECONDS));

  connection.close();

  client.shutdown();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 12
Source File: RedisBackedCache.java    From testcontainers-java with MIT License 4 votes vote down vote up
public RedisBackedCache(String hostname, Integer port) {
    RedisClient client = RedisClient.create(String.format("redis://%s:%d/0", hostname, port));
    connection = client.connect();
}
 
Example 13
Source File: RedisSinks.java    From hazelcast-jet-contrib with Apache License 2.0 4 votes vote down vote up
HashContext(RedisURI uri, K hash, RedisCodec<K, V> codec, FunctionEx<T, K> keyFn,
            FunctionEx<T, V> valueFn) {
    this.client = RedisClient.create(uri);
    this.connection = client.connect(codec);
    this.commands = connection.async();
    this.hash = hash;
    this.keyFn = keyFn;
    this.valueFn = valueFn;
}
 
Example 14
Source File: RedisFeatureSinkTest.java    From feast with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws IOException {
  redis = new RedisServer(REDIS_PORT);
  redis.start();
  redisClient =
      RedisClient.create(new RedisURI(REDIS_HOST, REDIS_PORT, java.time.Duration.ofMillis(2000)));
  StatefulRedisConnection<byte[], byte[]> connection = redisClient.connect(new ByteArrayCodec());
  sync = connection.sync();

  FeatureSetSpec spec1 =
      FeatureSetSpec.newBuilder()
          .setName("fs")
          .setProject("myproject")
          .addEntities(EntitySpec.newBuilder().setName("entity").setValueType(Enum.INT64).build())
          .addFeatures(
              FeatureSpec.newBuilder().setName("feature").setValueType(Enum.STRING).build())
          .build();

  FeatureSetSpec spec2 =
      FeatureSetSpec.newBuilder()
          .setName("feature_set")
          .setProject("myproject")
          .addEntities(
              EntitySpec.newBuilder()
                  .setName("entity_id_primary")
                  .setValueType(Enum.INT32)
                  .build())
          .addEntities(
              EntitySpec.newBuilder()
                  .setName("entity_id_secondary")
                  .setValueType(Enum.STRING)
                  .build())
          .addFeatures(
              FeatureSpec.newBuilder().setName("feature_1").setValueType(Enum.STRING).build())
          .addFeatures(
              FeatureSpec.newBuilder().setName("feature_2").setValueType(Enum.INT64).build())
          .build();

  specMap =
      ImmutableMap.of(
          FeatureSetReference.of("myproject", "fs", 1), spec1,
          FeatureSetReference.of("myproject", "feature_set", 1), spec2);
  StoreProto.Store.RedisConfig redisConfig =
      StoreProto.Store.RedisConfig.newBuilder().setHost(REDIS_HOST).setPort(REDIS_PORT).build();

  redisFeatureSink = RedisFeatureSink.builder().setRedisConfig(redisConfig).build();
  redisFeatureSink.prepareWrite(p.apply("Specs-1", Create.of(specMap)));
}
 
Example 15
Source File: JbootLettuceImpl.java    From jboot with Apache License 2.0 4 votes vote down vote up
public JbootLettuceImpl(JbootRedisConfig config) {
    this.config = config;
    this.redisClient = RedisClient.create();
}
 
Example 16
Source File: RedisBackedCache.java    From testcontainers-java with MIT License 4 votes vote down vote up
public RedisBackedCache(String hostname, Integer port) {
    RedisClient client = RedisClient.create(String.format("redis://%s:%d/0", hostname, port));
    connection = client.connect();
}
 
Example 17
Source File: RedisBackedCache.java    From testcontainers-java with MIT License 4 votes vote down vote up
public RedisBackedCache(String hostname, Integer port) {
    RedisClient client = RedisClient.create(String.format("redis://%s:%d/0", hostname, port));
    connection = client.connect();
}
 
Example 18
Source File: Lettuce5InstrumentationTest.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@Before
public void setUpLettuce() {
    RedisClient client = RedisClient.create(RedisURI.create("localhost", redisPort));
    connection = client.connect();
    reporter.disableDestinationAddressCheck();
}
 
Example 19
Source File: LettuceTest.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
  redisServer = new RedisServer();
  TestUtil.retry(redisServer::start, 10);
  client = RedisClient.create(address);
}
 
Example 20
Source File: RedisBackedSessionMap.java    From selenium with Apache License 2.0 4 votes vote down vote up
public RedisBackedSessionMap(Tracer tracer, URI serverUri) {
  super(tracer);

  client = RedisClient.create(RedisURI.create(serverUri));
  connection = client.connect();
}