-
Notifications
You must be signed in to change notification settings - Fork 0
/
krulak.clj
191 lines (161 loc) · 4.88 KB
/
krulak.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
(ns krulak
"Utility functions and macros."
(:require [cheshire.core :as json]
[clj-http.client :as client]
[clojure.core.cache :as cache]
[clojure.core.memoize :as memo]
[clojure.string :as str]
[ring.util.codec :as codec])
(:import clojure.core.memoize.PluggableMemoization))
(defn take-map [n m]
(if (> 0 n)
(empty m)
(if (< n (count m))
(->> m first key (dissoc m) (recur n))
m)))
(defn merge-meta [x m]
(vary-meta x merge m))
(defn lru-ttl-cache [base threshold ttl]
(-> (take-map threshold base)
(cache/lru-cache-factory :threshold threshold)
(cache/ttl-cache-factory :ttl ttl)))
(def soft-lru-ttl-cache (comp cache/soft-cache-factory lru-ttl-cache))
(defn memo-build-helper [cache-factory f base & args]
(memo/build-memoizer
#(PluggableMemoization. %1 (apply cache-factory %2 %3))
f
(#'memo/derefable-seed base)
args))
(defn lru-ttl-memo [f base threshold ttl]
(merge-meta
(memo-build-helper lru-ttl-cache f base threshold ttl)
{::cache-threshold threshold
::cache-ttl ttl}))
(defn soft-lru-ttl-memo [f base threshold ttl]
(merge-meta
(memo-build-helper soft-lru-ttl-cache f base threshold ttl)
{::cache-threshold threshold
::cache-ttl ttl}))
(defn memo-cache-swap!
"Replaces the entire cache of a function backed by
clojure.core.memoize, where clojure.core.memoize/cache-swap! only
replaces the data inside the cache. Used when you want to adjust
parameters of the cache (like size) on the fly."
[f new-cache]
(when-let [old-cache (#'memo/cache-id f)]
(swap! old-cache (constantly new-cache))))
(defn memo-evict!
"Evicts a set of arguments from the cache of a function backed by
clojure.core.memoize. Returns the function."
[f & args]
(-> f (#'clojure.core.memoize/cache-id) (swap! cache/evict args))
f)
(defn memo-update!
"Update the cached return value for a seq of args for a function backed
by clojure.core.memoize."
[f args v]
(-> f (#'memo/cache-id) (swap! cache/miss args (delay v))))
(defn memo-fresh
"Takes a function backed by clojure.core.memoize and args, gets a
fresh result, then swaps the new result into the cache."
[f & args]
(let [g (memo/memo-unwrap f)
result (apply g args)]
(memo-update! f args result)
result))
(defn deep-merge [& args]
(if (every? #(or (map? %) (nil? %)) args)
(apply merge-with deep-merge args)
(last args)))
(defn rmerge [& ms]
"Like clojure.core/merge, but in reverse order."
(apply merge (reverse ms)))
(defn maybe-deref [id]
(when id @id))
(defn rpartial [f & args]
#(apply f (concat %& args)))
(defn update-alias-meta [old-name old-meta]
(fn [new-meta]
(merge new-meta
(update old-meta :doc #(str "Alias for " old-name "." (if % "\n\n") %)))))
(defmacro defalias [new-name old-name]
`(alter-meta!
(def ~new-name)
(update-alias-meta '~old-name (meta (var ~old-name)))))
(defmacro defaliases [& syms]
`(do
~@(map
(fn [[new-name old-name]]
`(defalias ~new-name ~old-name))
(partition 2 syms))))
(defn uuid [] (str (java.util.UUID/randomUUID)))
(defn rand-bytes [n]
{:pre [(number? n)]}
(let [arr (byte-array n)]
(.nextBytes
(java.security.SecureRandom/getInstance "SHA1PRNG")
arr)
arr))
(defn secret-key [& [num-bytes]]
(-> (or num-bytes 16) rand-bytes codec/base64-encode))
(defn base64url-encode
"Return a base64url encoded string as defined by RFC 4648 §5 at
https://tools.ietf.org/html/rfc4648#section-5"
[byte-arr]
(.encodeToString (java.util.Base64/getUrlEncoder) byte-arr))
(defn base64url-uuid
"Return a random URL-safe base64 UUID. The default length is 21 characters,
which represent 126 bits, 2 less bits than a normal UUID."
([] (base64url-uuid 21))
([length]
(->> (rand-bytes length)
base64url-encode
(take length)
(apply str))))
(def url-slug-replacements {\space \-, \( \_, \) \_})
(def url-slug-allowed-chars
(set "0123456789abcdefghijklmnopqrstuvwxyz-_~"))
(defn to-url-slug [& xs]
(->> (apply str xs)
str/lower-case
(map #(url-slug-replacements % %))
(filter url-slug-allowed-chars)
(apply str)))
(defn json-get [url & [req & r]]
(update
(client/get
url
(assoc req :accept :json)
r)
:body
#(json/parse-string % true)))
(defn json-post [url & [req & r]]
(update
(client/post
url
(assoc req
:accept :json
:content-type :json)
r)
:body
#(json/parse-string % true)))
(defn json-put [url & [req & r]]
(update
(client/put
url
(assoc req
:accept :json
:content-type :json)
r)
:body
#(json/parse-string % true)))
(defn json-delete [url & [req & r]]
(update
(client/delete
url
(assoc req
:accept :json
:content-type :json)
r)
:body
#(json/parse-string % true)))