Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis Lettuce: Sending Custom Commands

Tags:

java

redis

I'm using tile38.com with lettuce.io on Java. I'm trying to send a custom NEARBY fleet FENCE POINT 33.462 -112.268 6000 command as per the docs enter link description here, but I don't know how to.

I've been using the CommandType in Lettuce, but I can't find a way of sending a NEARBY. Does someone know how I could do it?

Thanks

like image 323
nevi_me Avatar asked Feb 02 '18 05:02

nevi_me


People also ask

What is lettuce in Redis?

Overview This article is an introduction to Lettuce, a Redis Java client. Redis is an in-memory key-value store that can be used as a database, cache or message broker. Data is added, queried, modified, and deleted with commands that operate on keys in Redis' in-memory data structure.

Is Redis lettuce synchronous or asynchronous?

Synchronous Commands Similar to Jedis, Lettuce provides a complete Redis command set in the form of methods. However, Lettuce implements both synchronous and asynchronous versions. We’ll look at the synchronous version briefly, and then use the asynchronous implementation for the rest of the tutorial.

What is redisadvancedclustercommands in lettuce?

RedisAdvancedClusterCommands holds the set of Redis commands supported by the cluster, routing them to the instance that holds the key. A complete specification is available here. 10. Conclusion In this tutorial, we looked at how to use Lettuce to connect and query a Redis server from within our application.

How do I connect to a Redis server?

Connecting to a Server RedisClient redisClient = RedisClient .create ( "redis://password@localhost:6379/" ); StatefulRedisConnection<String, String> connection = redisClient.connect ();


1 Answers

You have multiple options to send custom commands:

Custom Commands

With custom commands, you basically define a type implementing the ProtocolKeyword interface which helps you as a single point of reference for all your keywords involved in your commands. You can use synchronous, asynchronous, or reactive APIs to invoke the command:

enum MyKeywords implements ProtocolKeyword {
    NEARBY, FENCE, POINT;

    private final byte name[];

    MyKeywords() {
        // cache the bytes for the command name. Reduces memory and cpu pressure when using commands.
        name = name().getBytes();
    }

    @Override
    public byte[] getBytes() {
        return name;
    }
}

CommandArgs<String, String> args = new CommandArgs<>(codec).addKey(key).add(MyKeywords.FENCE).add("POINT").add(lon).add(lat)
List<Object> response = connection.sync().dispatch(MyCommands.FENCE, new NestedMultiOutput<>(codec), args);

Command Interfaces

Command interfaces provide you a higher level of abstraction by declaring command methods on a Java interface. It's declared by a method signature that matches your command to invoke and less verbose than custom commands:

interface Tile38 {

  @Command("NEARBY ?0 FENCE POINT ?1 ?2")
  List<Object> nearByFence(String key, double lon, double lat);
}

RedisClient client = …
RedisCommandFactory factory = new RedisCommandFactory(client.connect());
Tile38 commands = factory.getCommands(Tile38.class);

Please note that I'm not familiar with Tile38 command responses. Therefore, all code uses List<Object> which is the most generic return type.

See also

  • Lettuce Documentation: Custom Commands.
  • Lettuce Documentation: Command Interfaces.
  • Lettuce JavaDoc: BaseRedisCommands.dispatch(…).
  • Lettuce JavaDoc: CommandOutput.
like image 156
mp911de Avatar answered Oct 07 '22 13:10

mp911de