/
core.clj
251 lines (202 loc) · 6.95 KB
/
core.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
(ns mcache.core
"Protocol for memory cache, and spymemcached implementation"
(:use [clojure.core.cache :only (defcache)]
[mcache.util :only (cache-update-multi)])
(:require [clojure.set]
[clojure.core.cache])
(:import [clojure.core.cache CacheProtocol]))
(def DEFAULT-EXP (* 60 60 24 30))
(defprotocol Memcache
"This is a protocol for memcached clients
The functions which insert values into the cache may optionally
accept an expiration, the semantics of which are assumed to
follow the memcached protocol, wherein the time unit is seconds,
and a value between 1 and 30 days is treated as time-to-live,
and any higher value is treated as a unix-style timestamp (seconds
since Jan 1 1970)."
(default-exp [mc]
"Return the default expiration")
(put-if-absent
[mc key value]
[mc key value exp]
"Add a key/value to the cache, if the key is not already there.")
(put-all-if-absent
[mc key-val-map]
[mc key-val-map exp]
"Add multiple keys/values to the cache, if not already there.")
(put
[mc key value]
[mc key value exp]
"Set a key/value. If a value is already cached with that key, it is replaced.")
(put-all
[mc key-val-map]
[mc key-val-map exp]
"Sets multiple keys/values, overwriting existing values if any.")
(put-if-present
[mc key value]
[mc key value exp]
"Replaces an object with the given value, but only if there is already a value for that key.")
(put-all-if-present
[mc key-val-map]
[mc key-val-map exp]
"Replaces multiple values")
(delete
[mc key]
"Deletes a key from the cache")
(delete-all [mc keys] ;; delete-all
"Deletes multiple keys")
(fetch
[mc key]
"Gets value for key.")
(fetch-all
[mc keys]
"Gets values for keys. Returns a map of keys to values, omitting
keys that are not in the cache.")
(clear [mc] "clear the cache"))
(defmacro with-cache
"key is a string, value is an sexpr. Returns keyed value from cache;
if not found, uses value to obtain the value and adds it to cache
before returning."
([cache key value]
`(if-let [cached-val# (fetch ~cache ~key)]
cached-val#
(let [val# ~value]
(if (nil? val#)
nil
(if (.get (put-if-absent ~cache ~key val#))
val#
(fetch ~cache ~key))))))
([cache key value exp]
`(if-let [cached-val# (fetch ~cache ~key)]
cached-val#
(let [val# ~value]
(if (nil? val#)
nil
(if (.get (put-if-absent ~cache ~key val# ~exp))
val#
(fetch ~cache ~key)))))))
(defn cache-backed-lookup
([cache id query-fn key-fn] (cache-backed-lookup cache id query-fn key-fn (default-exp cache)))
([cache id query-fn key-fn exp]
"To be used in conjunction with a persistent storage api. 'id' is
a unique identifier (e.g. a primary key) for a persisted object.
'query-fn' is a function accepting an id as argument, and
returning the persisted object or nil. 'key-fn' is a function which
accepts an id and returns a corresponding cache key. This function
will first attempt to locate the object in cache; if not found, it
uses query-fn to get the object and caches it before returning.
Returns nil if object is not found at all."
(if-let [cached-obj (fetch cache (key-fn id))]
cached-obj
(when-let [obj (query-fn id)]
(put-if-absent cache (key-fn id) obj exp)
obj))))
(defn- remove-nil-vals [map]
(reduce #(if (nil? (second %2)) %1 (assoc %1 (first %2) (second %2))) {} map))
(defn cache-backed-lookup-all
[cache ids query-fn key-fn]
"This is similar to cache-backed-lookup, except it handles a sequence of
ids. Returns a sequence containing the resolved objects, or nil for
not-found objects, in the same order as the original sequence of
ids."
(letfn [(add-to-cache
[cache id2val key-fn]
(doseq [[id val] id2val]
(put-if-absent cache (key-fn id) val)))
(from-cache
[cache ids key-fn]
(let [keys (map key-fn ids)]
(clojure.set/rename-keys (fetch-all cache keys) (zipmap keys ids))))]
(let [fromcache (from-cache cache ids key-fn)
ids-to-query (remove #(contains? fromcache %) ids)
fromquery (if (empty? ids-to-query) {} (remove-nil-vals (query-fn ids-to-query)))]
(add-to-cache cache fromquery key-fn)
(merge fromcache fromquery))))
(defcache MemcachedCache [mc exp]
CacheProtocol
(lookup
[this e]
(.. mc (get e)))
(has? [this e]
"Memcached does provide ability to test existence of a key without
fetching the entire value, so this function has no performance
advantage over lookup."
(not (nil? (.. mc (get e)))))
(hit [this e]
"Is meant to be called if the cache is determined to contain a value
associated with `e`"
this)
(miss [this e ret]
"Is meant to be called if the cache is determined to **not** contain a
value associated with `e`"
(when-not (nil? ret) (put mc e ret))
this)
(evict [this e]
"Removes an entry from the cache"
(delete mc e)
this)
(seed [this base]
"Is used to signal that the cache should be created with a seed.
The contract is that said cache should return an instance of its
own type."
(put-all mc base)
this)
Object
(toString [this] (str "MemcachedCache: " mc))
Memcache
(default-exp [this] DEFAULT-EXP)
(put-if-absent
[this key value exp]
(put-if-absent mc key value exp))
(put-if-absent
[this key value]
(put-if-absent mc key value))
(put-all-if-absent
[this key-val-map]
(put-all-if-absent mc key-val-map))
(put-all-if-absent
[this key-val-map exp]
(cache-update-multi put-if-absent this key-val-map exp))
(put
[this key value]
(put mc key value))
(put
[this key value exp]
(put mc key value exp))
(put-all
[this key-val-map]
(put-all this key-val-map DEFAULT-EXP))
(put-all
[this key-val-map exp]
(cache-update-multi put this key-val-map exp))
(put-if-present
[this key value]
(put-if-present this key value DEFAULT-EXP))
(put-if-present
[this key value exp]
(put-if-present mc key value exp))
(put-all-if-present
[this key-val-map]
(put-all-if-present this key-val-map DEFAULT-EXP))
(put-all-if-present
[this key-val-map exp]
(cache-update-multi put-if-present this key-val-map exp))
(delete
[this key]
(delete mc key))
(delete-all
[this keys]
(doall (map #(delete this %) keys)))
(fetch
[this key]
(fetch mc key))
(fetch-all
[this keys]
(fetch-all mc keys))
(clear
[this]
(clear mc)))
(defn memcached-cache-factory
"Returns a memcached-based cache using the given memcached client object"
([mc-client] (memcached-cache-factory mc-client (* 60 60 24 30)) )
([mc-client expiration] (MemcachedCache. mc-client expiration)))