io.lettuce.core.codec.StringCodec Java Examples

The following examples show how to use io.lettuce.core.codec.StringCodec. 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: RedisPositionsConfiguration.java    From liiklus with MIT License 6 votes vote down vote up
@Override
public void initialize(GenericApplicationContext applicationContext) {
    var environment = applicationContext.getEnvironment();

    var type = environment.getRequiredProperty("storage.positions.type");
    if(!"REDIS".equals(type)) {
        return;
    }

    var redisProperties = PropertiesUtil.bind(environment, new RedisProperties());

    applicationContext.registerBean(RedisPositionsStorage.class, () -> {
        var redisURI = RedisURI.builder()
                .withHost(redisProperties.getHost())
                .withPort(redisProperties.getPort())
                .build();

        return new RedisPositionsStorage(
                Mono
                        .fromCompletionStage(() -> RedisClient.create().connectAsync(StringCodec.UTF8, redisURI))
                        .cache(),
                redisProperties.getPositionsProperties().getPrefix()
        );
    });
}
 
Example #2
Source File: LettuceTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectAsync(final MockTracer tracer) throws Exception {
  final ConnectionFuture<StatefulRedisConnection<String,String>> connectionFuture = client.connectAsync(StringCodec.UTF8, RedisURI.create(address));
  try (final StatefulRedisConnection<String,String> connection = connectionFuture.get(10, TimeUnit.SECONDS)) {
    final RedisCommands<String,String> commands = connection.sync();
    assertEquals("OK", commands.set("key", "value"));
    assertEquals("value", commands.get("key"));
  }

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(3, spans.size());
}
 
Example #3
Source File: WrapperTests.java    From sherlock with GNU General Public License v3.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testBaseAccessorUsesProducer() {
    inject(Client.class, "client", null);
    ConnectionProducer stringProducer = mock(ConnectionProducer.class);
    ConnectionProducer byteProducer = mock(ConnectionProducer.class);
    RedisConnection<String> strConn = (RedisConnection<String>) mock(RedisConnection.class);
    RedisConnection<byte[]> binConn = (RedisConnection<byte[]>) mock(RedisConnection.class);
    when(stringProducer.produce(any())).thenAnswer(iom -> {
            assertTrue(iom.getArguments()[0] instanceof StringCodec);
            return strConn;
        }
    );
    when(byteProducer.produce(any())).thenAnswer(iom -> {
            assertTrue(iom.getArguments()[0] instanceof ByteArrayCodec);
            return binConn;
        }
    );
    BaseAccessor acc = new BaseAccessorTestImpl();
    inject(acc, BaseAccessor.class, "producer", stringProducer);
    assertEquals(strConn, acc.connect());
    verify(stringProducer).produce(any(StringCodec.class));
    inject(acc, BaseAccessor.class, "producer", byteProducer);
    assertEquals(binConn, acc.binary());
    verify(byteProducer).produce(any(ByteArrayCodec.class));
    inject(Client.class, "client", null);
}
 
Example #4
Source File: BaseAccessor.java    From sherlock with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @return a string connection
 */
public RedisConnection<String> connect() {
    return producer.produce(new StringCodec());
}
 
Example #5
Source File: RedisSinks.java    From hazelcast-jet-contrib with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a {@link Sink} which pushes data items into a specified Redis
 * Sorted Set. Assumes that the sorted set identifier and stored values are
 * all {@code String}s and the incoming data items are
 * {@link io.lettuce.core.ScoredValue}s.
 *
 * @param name name of the source being created
 * @param uri  URI of the Redis server
 * @param set  identifier of the Redis Sorted Set being used
 * @return sink to use in {@link com.hazelcast.jet.pipeline.Pipeline#writeTo}
 */
@Nonnull
public static Sink<ScoredValue<String>> sortedSet(
        @Nonnull String name,
        @Nonnull RedisURI uri,
        @Nonnull String set
) {
    return sortedSet(name, uri, set, StringCodec::new, ScoredValue::getScore, ScoredValue::getValue);
}
 
Example #6
Source File: RedisSinks.java    From hazelcast-jet-contrib with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a {@link Sink} which pushes data items into a specified Redis
 * Sorted Set. Assumes that the sorted set identifier and stored values are
 * all {@code String}s.
 *
 * @param name    name of the source being created
 * @param uri     URI of the Redis server
 * @param set     identifier of the Redis Sorted Set being used
 * @param scoreFn function that specifies how to extract the scores from
 *                incoming data items
 * @param valueFn function that specifies how to extract stored values from
 *                incoming data items
 * @param <T>     type of data items coming into the sink
 * @return sink to use in {@link com.hazelcast.jet.pipeline.Pipeline#writeTo}
 */
@Nonnull
public static <T> Sink<T> sortedSet(
        @Nonnull String name,
        @Nonnull RedisURI uri,
        @Nonnull String set,
        @Nonnull FunctionEx<T, Double> scoreFn,
        @Nonnull FunctionEx<T, String> valueFn
) {
    return sortedSet(name, uri, set, StringCodec::new, scoreFn, valueFn);
}
 
Example #7
Source File: RedisSources.java    From hazelcast-jet-contrib with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a {@link BatchSource} which queries a Redis Sorted Set for a
 * range of elements having their scores between the two limit values
 * provided (from & to). The returned elements will be emitted as they
 * arrive, the batch ends when all elements have been received. All keys and
 * values are assumed to be {@link String}s.
 *
 * @param name name of the source being created
 * @param uri  URI of the Redis server
 * @param key  identifier of the Redis Sorted Set
 * @param from start of the score range we are interested in (INCLUSIVE)
 * @param to   end of the score range we are interested in (INCLUSIVE)
 * @return source to use in {@link com.hazelcast.jet.pipeline.Pipeline#readFrom}
 */
@Nonnull
public static BatchSource<ScoredValue<String>> sortedSet(
        @Nonnull String name,
        @Nonnull RedisURI uri,
        @Nonnull String key,
        long from,
        long to
) {
    return sortedSet(name, uri, StringCodec::new, key, from, to);
}
 
Example #8
Source File: RedisSources.java    From hazelcast-jet-contrib with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a {@link StreamSource} which reads all items from multiple Redis
 * Streams (starting from given indexes) and emits them as they arrive.
 * Assumes all keys and values are {@code String}s and assumes a projection
 * function which just emits message bodies from stream elements received.
 *
 * @param name          name of the source being created
 * @param uri           URI of the Redis server
 * @param streamOffsets map keyed by stream identifiers, containing offset
 *                      values from which to start element retrieval of each
 *                      stream
 * @return source to use in {@link com.hazelcast.jet.pipeline.Pipeline#readFrom}
 */
@Nonnull
public static StreamSource<Map<String, String>> stream(
        @Nonnull String name,
        @Nonnull RedisURI uri,
        @Nonnull Map<String, String> streamOffsets
) {
    return stream(name, uri, streamOffsets, StringCodec::new, StreamMessage::getBody);
}
 
Example #9
Source File: RedisSources.java    From hazelcast-jet-contrib with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a {@link StreamSource} which reads all items from multiple Redis
 * Streams (starting from given indexes) and emits them as they arrive.
 * Assumes all keys and values are {@code String}s.
 *
 * @param <T>           type of data coming out of the stream, the result of
 *                      applying the projection function over {@link
 *                      StreamMessage} instances
 * @param name          name of the source being created
 * @param uri           URI of the Redis server
 * @param streamOffsets map keyed by stream identifiers, containing offset
 *                      values from which to start element retrieval of each
 *                      stream
 * @param projectionFn  built in mapping function of the source which can be
 *                      used to map {@link StreamMessage} instances received
 *                      from Redis to an arbitrary type of output; this
 *                      could be done by an external mapping function in the
 *                      pipeline too, but it's included for convenience
 * @return source to use in {@link com.hazelcast.jet.pipeline.Pipeline#readFrom}
 */
@Nonnull
public static <T> StreamSource<T> stream(
        @Nonnull String name,
        @Nonnull RedisURI uri,
        @Nonnull Map<String, String> streamOffsets,
        @Nonnull FunctionEx<? super StreamMessage<String, String>, ? extends T> projectionFn
) {
    return stream(name, uri, streamOffsets, StringCodec::new, projectionFn);
}