Created
December 14, 2012 02:58
-
-
Save neilmock/4282272 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns com.neilmock.enduro-redis | |
(:require [alandipert.enduro :as enduro] | |
[clojure.string :as str]) | |
(:import (alandipert.enduro EnduroAtom) | |
java.net.URI | |
(redis.clients.jedis Jedis JedisPool JedisPoolConfig))) | |
(defn- jedis-pool [url] | |
(let [uri (URI. url) | |
host (.getHost uri) | |
port (if (pos? (.getPort uri)) (.getPort uri) 6379) | |
uinfo (.getUserInfo uri) | |
pass (and uinfo (last (str/split uinfo #":"))) | |
config (JedisPoolConfig.)] | |
(JedisPool. config host port 2000 pass))) | |
(defn- lease [^JedisPool p f] | |
(let [j (.getResource p)] | |
(try | |
(f j) | |
(finally | |
(.returnResource p j))))) | |
(defn- rget [p ^String k] | |
(lease p (fn [^Jedis j] (.get j k)))) | |
(defn- rset [p ^String k ^String v] | |
(lease p (fn [^Jedis j] (.set j k v)))) | |
(deftype RedisBackend [url key] | |
enduro/IDurableBackend | |
(-commit! [this value] | |
(let [db (jedis-pool url)] | |
(rset db key (pr-str value))))) | |
(defn redis-atom | |
[init url key & opts] | |
(enduro/atom* (let [db (jedis-pool url) | |
existing (rget db key)] | |
(if existing | |
(read-string existing) | |
init)) | |
(RedisBackend. url key) | |
(apply hash-map opts))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hm probably an unnecessary import of EnduroAtom. the deftype vs defprotocol import semantics are interesting, I think one gens a Java class that needs import and the other is a true Var.