Check out my previous post about Redis.
In this post I build a very simple example of using Redis with Clojure. I will be using a client library for Redis written in Clojure called redis-clojure. You could also use the java library, to see complete list of supported languages go to this link.
So here we go..
- Create a simple clojure project (I personally use Leinigen), to create a new project execute ‘lein new com.dev/try-redis‘ this will create an entire project structure.
- Edit the project.clj file under the newly created project directory and add a new dependency for redis-clojure, the file should look close to this after you are done.
(defproject com.dev/try-redis "1.0.0-SNAPSHOT" :description "simple example of using redis" :dependencies [[org.clojure/clojure "1.1.0"] [org.clojure/clojure-contrib "1.1.0"] [redis-clojure "1.0.3-SNAPSHOT"]] :dev-dependencies [[swank-clojure "1.2.1"]])
- run ‘lein deps‘ so that all the dependencies are downloaded.
- Edit the file core.clj under the directory try-redis/src/com/dev/try_redis, and add the following.
(ns com.dev.try-redis.core (:require redis)) (defn test-redis [] (redis/with-server {:host "127.0.0.1" :port 6379 :db 0} (do (redis/set "foo" "bar") (println (redis/get "foo")))))
On lines 7 and 8 we are setting key value pair and retriving the value.
- Start the redis server ‘./redis-server redis.conf‘
- Now we are ready to execute the script, there are 2 ways to do this.
- The easiest way is just going to your porject root directory and run ‘lein repl‘ (see the below oouput) which opens a read evaluate loop and once you have that run ‘(load-file “src/com/dev/try_redis/core.clj”)‘ to load the file and then you can run ‘(com.dev.try-redis.core/test-redis)‘ to run the example.
- I personally use emacs/slime, but for this option you need to have emacs and slime-clojure installed (See my emacs page). Run ‘lein swank‘ in the project directory and then in your emacs connect to it using ‘M-x slime-connect‘, this will open up a repl, do a C-c C-k to compile the file and in the repl you can execute using ‘(com.dev.try-redis.core/test-redis)’
If everything has gone will you should see this output.
Clojure 1.1.0 user=> (load-file "src/com/dev/try_redis/core.clj") #'com.dev.try-redis.core/test-redis user=> (com.dev.try-redis.core/test-redis) bar nil user=>
Here is an updated version of redis-clojure that works with Clojure 1.2 and 1.3. https://github.com/tavisrudd/redis-clojure
The version show above is now out-dated and has some bugs.
Those brought here by google in 2016+: you probably want to look in to Carmine for Clojure/Redis work. https://github.com/ptaoussanis/carmine