forked from ninjudd/masai
/
memcached.clj
64 lines (48 loc) · 1.87 KB
/
memcached.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(ns flatland.masai.memcached
(:use [flatland.useful.map :only [into-map]])
(:require flatland.masai.db)
(:import net.spy.memcached.MemcachedClient
[java.net InetSocketAddress InetAddress]))
;; TODO fix this to reify a transcoder that throws an exception if the val is
;; not a byte array and to base64 encode the keys. Then reenable and fix the
;; tests.
(defn key-format [^String s] (identity s))
(deftype DB [^MemcachedClient mdb endpoints]
flatland.masai.db/EphemeralDB
(add-expiry! [db key val exp] (.get (.add mdb (key-format key) exp val)))
(put-expiry! [db key val exp] (.get (.set mdb (key-format key) exp val)))
flatland.masai.db/DB
(close [db]
(.shutdown mdb))
(unique-id [db]
endpoints)
(fetch [db key]
(.get mdb (key-format key)))
(add! [db key val] (flatland.masai.db/add-expiry! db key val 0))
(put! [db key val] (flatland.masai.db/put-expiry! db key val 0))
(append! [db key val]
(let [fkey (key-format key)]
(if-let [appended (when-let [cas (.gets mdb key)]
(.get (.append mdb (.getCas cas) fkey val)))]
appended
(flatland.masai.db/add! db key val))))
(len [db key]
(if-let [record (flatland.masai.db/fetch db key)]
(count (str record))
-1))
(exists? [db key]
(not= nil (flatland.masai.db/fetch db key)))
(inc! [db key i]
(if (> 0 i)
(.decr mdb (key-format key) (Math/abs ^Integer i))
(.incr mdb (key-format key) i)))
(delete! [db key]
(.get (.delete mdb (key-format key))))
(truncate! [db]
(.get (.flush mdb))))
(defn make [& opts]
(let [{:keys [addresses]
:or {addresses {"localhost" 11211}}} (into-map opts)
^java.util.List addrs (for [[addr ^Integer port] addresses]
(InetSocketAddress. (InetAddress/getByName addr) port))]
(DB. (MemcachedClient. addrs) (set addrs))))