An idiomatic Clojure Redis client wrapping the Java client Lettuce
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
modules
test/celtuce
.deps-versions.clj
.gitignore
LICENSE
README.md
project.clj

README.md

celtuce

An idiomatic Clojure Redis client wrapping the Java client Lettuce.

Usage

Clojars Project to include all modules.

Or pick up only the ones you need:

Redis Connectors

Connectors are available for both Redis Server and Cluster. They are defined in celtuce.connector namespace of celtuce-core module.

(require '[celtuce.connector :as conn])

(conn/redis-server "redis://localhost:6379")

(conn/redis-cluster "redis://localhost:30001")

Redis URI synthax details can be found in Lettuce Wiki.

Serialization defaults to Nippy, but other serializers are available in celtuce.codec. Especially Lettuce original String serializer can be used as follows:

(conn/redis-server
  "redis://localhost:6379"
  :codec (celtuce.codec/utf8-string-codec))

Other connector options:

  • :conn-options a map of connection options

    • :timeout timeout for executing commands
    • :unit corresponding TimeUnit in keyword (i.e. :milliseconds, etc)
    • :auto-flush automatically flush commands on the underlying Netty connection
  • :client-options: a map of client options

Note that you can find options default values in the tests.

Redis Commands

All Redis commands are implemented using protocols in celtuce.commands namespace of celtuce-core module.

(require '[celtuce.commands :as redis])

Sync Commands

(def connector (conn/redis-server "redis://localhost:6379"))
(def cmds (conn/commands-sync connector))

(redis/set cmds :foo "bar")
(redis/get cmds :foo)

(conn/shutdown connector)

PubSub Commands

Redis prevents publishing and subscribing on the same connection. The following contrive example demonstrates pubsub usage with two connections.

;; note that conn/as-pubsub also works on cluster connectors
(def conn-pub (conn/as-pubsub (conn/redis-server "redis://localhost:6379")))
(def conn-sub (conn/as-pubsub (conn/redis-server "redis://localhost:6379")))

(def pub (conn/commands-sync conn-pub))
(def sub (conn/commands-sync conn-sub))

(conn/add-listener! 
 conn-sub
 (reify redis/PubSubListener
   (message [_ channel message]
     (println "received message" message "from channel" channel))
   (message [_ pattern channel message])
   (subscribed [_ channel count]
     (println "new subscriber !"))
   (unsubscribed [_ channel count]
     (println "a subscriber left..."))
   (psubscribed [_ pattern count])
   (punsubscribed [_ pattern count])))

(redis/subscribe sub "foo-chan")
(redis/publish pub "foo-chan" "bar-msg")
(redis/unsubscribe sub "foo-chan")

(conn/shutdown conn-pub)
(conn/shutdown conn-sub)

Dynamic Commands

Starting from Lettuce 5 it is now possible to define commands dynamically by extending a Commands interface. Such commands are obtained as follows.

(conn/commands-dynamic connector some.interface.extending.Commands)

You can find basic examples in the tests.

Tests

To run unit tests you need to have both a redis server running on a localhost:6379, and a redis cluster running on localhost:30001.

Then build artifacts and run tests:

(cd modules/celtuce-core/; lein clean; lein install)
(cd modules/celtuce-pool/; lein clean; lein install)
(cd modules/celtuce-manifold/; lein clean; lein install)

lein test

License