io.lettuce.core.api.sync.RedisCommands Java Examples

The following examples show how to use io.lettuce.core.api.sync.RedisCommands. 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: LettuceTest.java    From java-specialagent with Apache License 2.0 10 votes vote down vote up
@Test
public void testPubSub(final MockTracer tracer) {
  final StatefulRedisPubSubConnection<String,String> connection = client.connectPubSub();
  connection.addListener(new RedisPubSubAdapter<>());

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

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

  await().atMost(15, TimeUnit.SECONDS).until(TestUtil.reportedSpansSize(tracer), equalTo(4));

  client.shutdown();

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(4, spans.size());
}
 
Example #2
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 #3
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 #4
Source File: RedisBackedSessionMap.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Override
public URI getUri(SessionId id) throws NoSuchSessionException {
  Require.nonNull("Session ID", id);

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

  List<KeyValue<String, String>> rawValues = commands.mget(uriKey(id), capabilitiesKey(id));

  String rawUri = rawValues.get(0).getValueOrElse(null);
  if (rawUri == null) {
    throw new NoSuchSessionException("Unable to find URI for session " + id);
  }

  try {
    return new URI(rawUri);
  } catch (URISyntaxException e) {
    throw new NoSuchSessionException(String.format("Unable to convert session id (%s) to uri: %s", id, rawUri), e);
  }
}
 
Example #5
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 #6
Source File: RedisSinkTest.java    From hazelcast-jet-contrib with Apache License 2.0 6 votes vote down vote up
@Test
public void stream() {
    IList<String> list = instance.getList("list");
    for (int i = 0; i < 10; i++) {
        list.add("key-" + i);
    }

    Pipeline p = Pipeline.create();
    p.readFrom(Sources.list(list))
            .writeTo(RedisSinks.stream("source", uri, "stream"));

    instance.newJob(p).join();

    RedisCommands<String, String> sync = connection.sync();
    List<StreamMessage<String, String>> messages = sync.xread(XReadArgs.StreamOffset.from("stream", "0"));
    assertEquals(list.size(), messages.size());
}
 
Example #7
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 #8
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 #9
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 #10
Source File: TracingLettuce50Test.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 #11
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 #12
Source File: TracingLettuce52Test.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 #13
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 #14
Source File: LettuceIntegrationLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenValues_thenSaveAsRedisHashSync() {

    RedisCommands<String, String> syncCommands = redisConnection.sync();

    String recordName = "record1";
    String name = "FirstName";
    String value = "John";
    String surname = "LastName";
    String value1 = "Smith";

    syncCommands.hset(recordName, name, value);
    syncCommands.hset(recordName, surname, value1);
    Map<String, String> record = syncCommands.hgetall(recordName);

    Assert.assertEquals(record.get(name), value);
    Assert.assertEquals(record.get(surname), value1);
}
 
Example #15
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 #16
Source File: RedisLettuceStarterTest.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Test
public void tests() throws Exception {
    if (RedisLettuceCacheTest.checkOS()) {
        System.setProperty("spring.profiles.active", "redislettuce-cluster");
    } else {
        System.setProperty("spring.profiles.active", "redislettuce");
    }
    context = SpringApplication.run(RedisLettuceStarterTest.class);
    doTest();
    A bean = context.getBean(A.class);
    bean.test();

    RedisClient t1 = (RedisClient) context.getBean("defaultClient");
    RedisClient t2 = (RedisClient) context.getBean("a1Client");
    Assert.assertNotNull(t1);
    Assert.assertNotNull(t2);
    Assert.assertNotSame(t1, t2);

    AutoConfigureBeans acb = context.getBean(AutoConfigureBeans.class);

    String key = "remote.A1";
    Assert.assertTrue(new LettuceFactory(acb, key, StatefulRedisConnection.class).getObject() instanceof StatefulRedisConnection);
    Assert.assertTrue(new LettuceFactory(acb, key, RedisCommands.class).getObject() instanceof RedisCommands);
    Assert.assertTrue(new LettuceFactory(acb, key, RedisAsyncCommands.class).getObject() instanceof RedisAsyncCommands);
    Assert.assertTrue(new LettuceFactory(acb, key, RedisReactiveCommands.class).getObject() instanceof RedisReactiveCommands);

    if (RedisLettuceCacheTest.checkOS()) {
        key = "remote.A2";
        Assert.assertTrue(new LettuceFactory(acb, key, RedisClusterClient.class).getObject() instanceof RedisClusterClient);
        Assert.assertTrue(new LettuceFactory(acb, key, RedisClusterCommands.class).getObject() instanceof RedisClusterCommands);
        Assert.assertTrue(new LettuceFactory(acb, key, RedisClusterAsyncCommands.class).getObject() instanceof RedisClusterAsyncCommands);
        Assert.assertTrue(new LettuceFactory(acb, key, RedisClusterReactiveCommands.class).getObject() instanceof RedisClusterReactiveCommands);

        key = "remote.A2_slave";
        Assert.assertTrue(new LettuceFactory(acb, key, RedisClusterClient.class).getObject() instanceof RedisClusterClient);
    }
}
 
Example #17
Source File: RedisPriorityQueue.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
private String getRequest(RedisCommands<String, String> commands, String spiderName) {

        String url;
        List<String> urls = commands.zrevrange(getZsetPriorityKey(spiderName), 0, 0);

        if (urls.isEmpty()) {
            url = commands.lpop(getQueueNormalKey(spiderName));
        } else {
            url = urls.toArray(new String[0])[0];
            commands.zrem(getZsetPriorityKey(spiderName), url);
        }

        return url;
    }
 
Example #18
Source File: LettuceSessionClient.java    From redis-session-manager with Apache License 2.0 5 votes vote down vote up
<T> T sync(Function<RedisCommands<String, Object>, T> s) {
    try (StatefulRedisConnection<String, Object> conn = pool.borrowObject()) {
        return s.apply(conn.sync());
    } catch (Exception e) {
        log.error("Failed to borrow a connection", e);
        return null;
    }
}
 
Example #19
Source File: HashEntry.java    From Arraybot with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes everything corresponding to the ID.
 * @param id The ID.
 */
@Override
public final void deleteGuild(long id) {
    RedisCommands resource = redis.getResource();
    Category parentCategory = getParent();
    if(parentCategory == null) {
        resource.del(UDatabase.getKey(category, id));
        return;
    }
    SetEntry parent = (SetEntry) parentCategory.getEntry();
    for(String key : parent.values(id)) {
        resource.del(UDatabase.getKey(category, id, key));
    }
    parent.internalDelete(id);
}
 
Example #20
Source File: HashEntry.java    From Arraybot with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes the specific entry.
 * @param guild The ID of the guild.
 * @param key The entry key. This can be null.
 */
public final void deleteSingleEntry(long guild, Object key) {
    RedisCommands resource = redis.getResource();
    resource.del(UDatabase.getKey(category, guild, key));
    Category parentCategory = getParent();
    if(parentCategory == null) {
        return;
    }
    SetEntry parent = (SetEntry) parentCategory.getEntry();
    parent.remove(guild, key);
}
 
Example #21
Source File: MessageListener.java    From Arraybot with Apache License 2.0 5 votes vote down vote up
/**
 * When a message occurs.
 * Used to log the amount of messages.
 * @param event The event.
 */
@SuppressWarnings("unchecked")
@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
    RedisCommands resource = Redis.INSTANCE.getResource();
    resource.incr(UDatabase.MESSAGES_KEY);
}
 
Example #22
Source File: RedisBackedSessionMap.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
public boolean add(Session session) {
  Require.nonNull("Session to add", session);

  RedisCommands<String, String> commands = connection.sync();
  commands.mset(
    ImmutableMap.of(
      uriKey(session.getId()), session.getUri().toString(),
      capabilitiesKey(session.getId()), JSON.toJson(session.getCapabilities())));

  return true;
}
 
Example #23
Source File: RedisSources.java    From hazelcast-jet-contrib with Apache License 2.0 5 votes vote down vote up
public boolean complete() {
    if (!emitFromTraverser(traverser)) {
        return false;
    }

    RedisCommands<K, V> sync = connection.sync();
    @SuppressWarnings("unchecked")
    XReadArgs.StreamOffset<K>[] streamOffsetArray = streamOffsets
            .entrySet()
            .stream()
            .map(entry -> XReadArgs.StreamOffset.from(entry.getKey(), entry.getValue()))
            .toArray(XReadArgs.StreamOffset[]::new);
    List<StreamMessage<K, V>> messages = sync.xread(xReadArgs, streamOffsetArray);

    traverser = isEmpty(messages) ? eventTimeMapper.flatMapIdle() :
            traverseIterable(messages)
                    .flatMap(message -> {
                        streamOffsets.put(message.getStream(), message.getId());
                        T projectedMessage = projectionFn.apply(message);
                        if (projectedMessage == null) {
                            return Traversers.empty();
                        }
                        return eventTimeMapper.flatMapEvent(projectedMessage, 0, NO_NATIVE_TIME);
                    });

    emitFromTraverser(traverser);

    return false;
}
 
Example #24
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 #25
Source File: RedisBackedSessionMap.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Override
public void remove(SessionId id) {
  Require.nonNull("Session ID", id);

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

  commands.del(uriKey(id), capabilitiesKey(id));
}
 
Example #26
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 #27
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 #28
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 #29
Source File: LettuceDriver.java    From redis-scheduler with MIT License 5 votes vote down vote up
@Override
public <T> T fetch(Function<Commands, T> block) {
    try (StatefulRedisConnection<String, String> connection = client.connect()) {
        RedisCommands<String, String> commands = connection.sync();
        return block.apply(new LettuceCommands(commands));
    } catch (RedisConnectionException e) {
        throw new RedisConnectException(e);
    }
}
 
Example #30
Source File: RedisPriorityQueue.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
private Request getExtrasInItem(RedisCommands<String, String> commands, String url, String spiderName) {

        String key = getItemKey(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);
    }