An idiomatic Clojure Redis client wrapping the Java client Lettuce.
to include all modules.
Or pick up only the ones you need:
celtuce-core: Main module with all the core functionalities (required)
celtuce-pool: Provides pooling for connections
celtuce-manifold: Implementation of asynchronous commands based on Manifold
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.
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.
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 do clean, install)
(cd modules/celtuce-pool/; lein do clean, install)
(cd modules/celtuce-manifold/; lein do clean, install)
lein test
Can you improve this documentation? These fine people already did:
lerouxrgd & Leroux RomainEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close