clj-redis-session
A Redis backed Clojure/Ring session store
What is it?
clj-redis-session
uses redis as a Clojure/Ring's HTTP session
storage engine. What makes it different is its support for
hierarchical data, actually any *print-str*
able clojure data types.
Why?
The reason I wrote clj-redis-session
is that the only Redis-backed
sesssion store I could find (rrss)
doesn't support hierarchical data structures, e.g. lists, maps.
Installation
Add
[clojusc/clj-redis-session "2.1.4"]
to :dependencies
in your project.clj
.
Usage
clj-redis-session
is a drop-in replacement for Ring native session
stores. clj-redis-session
uses Carmine as its Redis client.
First, require the session namespaces:
(ns your-app
(:require [ring.middleware.session :as ring-session]
[clj-redis-session.core :refer [redis-store]]))
Then define the Redis connection options as you would when using Carmine directly. For example:
(def conn {:pool {}
:spec {:host "127.0.0.1"
:port 6379
:password "s3kr1t"
:timeout-ms 5000}})
At this point, you'll be ready to use clj-redis-session
to manage your
application sessions:
(def your-app
(-> your-routes
(... other middlewares ...)
(ring-session/wrap-session {:store (redis-store conn)})
(...)))
Automatically expire sessions after 12 hours:
(wrap-session your-app {:store (redis-store conn {:expire-secs (* 3600 12)})})
Automatically extend the session expiration time whenever the session data is read:
(wrap-session your-app {:store (redis-store conn {:expire-secs (* 3600 12)
:reset-on-read true})})
You can also change the default prefix, session
, for the keys in Redis to
something else:
(wrap-session your-app {:store (redis-store conn {:prefix "your-app-prefix"})})
License
Copyright © 2013 Zhe Wu wu@madk.org
Distributed under the Eclipse Public License, the same as Clojure.