Java Code Examples for io.lettuce.core.api.StatefulRedisConnection#sync()

The following examples show how to use io.lettuce.core.api.StatefulRedisConnection#sync() . 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: RedisQueue.java    From NetDiscovery with Apache License 2.0 7 votes vote down vote up
@Override
protected void pushWhenNoDuplicate(Request request) {
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();
    try {
        commands.lpush(request.getSpiderName(), request.getUrl());

        if (hasExtraRequestInfo(request)) {
            String field = DigestUtils.sha1Hex(request.getUrl());
            String value = SerializableUtils.toJson(request);
            commands.hset((ITEM_PREFIX + request.getUrl()), field, value);
        }

    } finally {
        connection.close();
    }

}
 
Example 2
Source File: RedisQueue.java    From NetDiscovery with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isDuplicate(Request request) {
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();

    try {
        if (request.isCheckDuplicate()) {
            return commands.sadd(getSetKey(request), request.getUrl()) == 0;
        } else {
            commands.sadd(getSetKey(request), request.getUrl());
            return false;
        }
    } finally {
        connection.close();
    }
}
 
Example 3
Source File: RedisQueue.java    From NetDiscovery with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized Request poll(String spiderName) {
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();
    try {
        String url = commands.lpop(getQueueKey(spiderName));
        if (url == null) {
            return null;
        }

        String key = ITEM_PREFIX + url;
        String field = DigestUtils.sha1Hex(url);
        String result = commands.hget(key, field);

        if (result != null) {

            return SerializableUtils.fromJson(result, Request.class);
        }

        return new Request(url);
    } finally {
        connection.close();
    }
}
 
Example 4
Source File: RedisPriorityQueue.java    From NetDiscovery with Apache License 2.0 6 votes vote down vote up
@Override
protected void pushWhenNoDuplicate(Request request) {

    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();

    try {
        if (request.getPriority() > 0) {
            commands.zadd(getZsetPriorityKey(request.getSpiderName()), request.getPriority(), request.getUrl());
        } else {
            commands.lpush(getQueueNormalKey(request.getSpiderName()), request.getUrl());
        }

        setExtrasInItem(commands, request);
    } finally {
        connection.close();
    }
}
 
Example 5
Source File: TracingLettuce52Test.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: TracingLettuce50Test.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 7
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 8
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 9
Source File: TestRedisTemplate.java    From ad with Apache License 2.0 5 votes vote down vote up
@Test
public void nativeAPI() {
    RedisClient redisClient = RedisClient.create("redis://127.0.0.1:6379/0");
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();
    String set = commands.set("String_key", "String_value");
    String value = commands.get("String_key");
    log.debug(value);

    Object _value = redisTemplate.opsForValue().get("String_key");
    log.debug("{}", _value);
}
 
Example 10
Source File: StringStringSyncTest.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
@Prepare(global = true)
public void loadInitialData() {
    Random random = new Random();
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands sync = connection.sync();
    for (int k = 0; k < keyDomain; k++) {
        int r = random.nextInt(valueCount);
        sync.set(Long.toString(k), values[r]);
    }
}
 
Example 11
Source File: LuaScriptIsolationTest.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
    RedisURI url = RedisURI.Builder
            .redis(redis.getContainerIpAddress(), redis.getMappedPort(6379))
            .build();

    RedisClient client = RedisClient.create(url);
    StatefulRedisConnection<String, String> connection = client.connect();

    this.commands = connection.sync();
}
 
Example 12
Source File: RedisQueue.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
@Override
public int getLeftRequests(String spiderName) {
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();
    try {
        Long size = commands.llen(getQueueKey(spiderName));
        return size.intValue();
    } finally {
        connection.close();
    }
}
 
Example 13
Source File: RedisQueue.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
@Override
public int getTotalRequests(String spiderName) {
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();
    try {
        Long size = commands.scard(getSetKey(spiderName));
        return size.intValue();
    } finally {
        connection.close();
    }
}
 
Example 14
Source File: RedisPriorityQueue.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized Request poll(String spiderName) {
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();
    try {
        String url = getRequest(commands, spiderName);
        if (Preconditions.isBlank(url)) {
            return null;
        }

        return getExtrasInItem(commands, url, spiderName);
    } finally {
        connection.close();
    }
}
 
Example 15
Source File: StringStringSyncTest.java    From hazelcast-simulator with Apache License 2.0 4 votes vote down vote up
ThreadState() {
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    sync = connection.sync();
}
 
Example 16
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 17
Source File: RedisOnlineRetriever.java    From feast with Apache License 2.0 4 votes vote down vote up
private RedisOnlineRetriever(StatefulRedisConnection<byte[], byte[]> connection) {
  this.syncCommands = connection.sync();
}
 
Example 18
Source File: RedisBackedJobService.java    From feast with Apache License 2.0 4 votes vote down vote up
public RedisBackedJobService(StatefulRedisConnection<byte[], byte[]> connection) {
  this.syncCommand = connection.sync();
}