Git Product home page Git Product logo

java-redis-client's Introduction

java-redis-client

Low level Redis client (but you really won't need anything more than this!)

Usage

Either package the library using ./build.sh or whatever package manager you fancy, or just copy it into your source tree.

Then create a socket connection and start speaking Redis.

nl.melp.redis.Redis r = new nl.melp.redis.Redis(new Socket("127.0.0.1", 6379));
r.call("SET", "foo", "123");
r.call("INCRBY", "foo", "456");
System.out.println(r.call("GET", "foo")); // will print '579'

How data is parsed

  • Error responses are translated to an Exception (nl.melp.redis.Redis.Parser.ServerError)
  • Strings become byte[]
  • Numbers become Long
  • Arrays become List, where the entry can be any of String, Long or List<Object>.

    Since call uses a template return value, return type is statically inferred:

    redis.call("LPUSH", "mylist", "A", "B", "C", "D");
    List<Object> l = redis.call("LRANGE", "mylist", "0", "200");
    System.out.println("Size: " + l.size()); // prints "Size: 4"
    System.out.println((String)l.get(0)); // prints "A"

    You will have to do some casting yourself in case of List responses. The reasoning here is that you know what data to expect, so you're responsible for applying the correct casts in the correct context.

    Refer to the Redis documentation for the return types of all calls and nl.melp.redis.RedisTest for some working examples.

    Pipelining

    The idea of pipelining is that you can keep writing commands without having read the response. This is an inherent feature of the protocol. The call() method by default returns the response immediately, but if you wish to keep sending commands without reading the response, you can use the pipeline() call. This is typically useful for a MULTI/EXEC call where the responses aren't directly useful.

    redis.pipeline()
        .call("MULTI")
        .call("INCR", "A")
        .call("INCR", "A")
        .call("INCR", "A")
        .call("INCR", "A")
        .call("EXEC")
        .read()
    ;

    This will result in a List<Object> containing a list for each of the responses.

    Is the connection thread safe?

    No. You need to make sure to access the connection atomically. However, there is a Redis.run() method which you can use to do some simple redis operations in isolation. It creates a connection and closes it directly after:

    nl.melp.redis.Redis.run((redis) -> redis.call("INCR", "mycounter"));

    How should I manage my connections?

    However you wish. This library is a protocol implementation only. Managing a connection pool is not at all Redis-specific, so it doesn't belong here.

    Having said that, this mostly depends on your use case. Typically, if you have a webserver with 20 threads, you can have a socket per thread managed somewhere (e.g. ThreadLocal) and if you run a thread pool with 20 workers you can have one socket per thread there as well. This keeps things simple and practical to reason about, and you don't have to worry who needs to create the socket at what point in time. You do need some error handling which, in case of managing the sockets centrally is a tiny bit easier. On the other hand, if you expect to be creating a lot of threads and not a lot of these threads need the connection, you probably want to implement a pool.

    Summarizing, this is a trade-off which you can decide on for your own. Generally speaking: don't overcomplicate stuff without good reason.

    In terms of performance: creating socket connections on-the-fly is comparable to reusing connections. See the performance tests inside RedisTest for more information.

    See also

    See drm/java-redis-collections for collection implementations which can be used to manage your application runtime data in redis.

    Questions? Issues?

    Feel free to report issues here.

java-redis-client's People

Contributors

drm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.