Skip to content

Commit 4b5d3f0

Browse files
paulrdbbatsov
authored andcommitted
Use .artifacts-cache file to cache artifacts between runs
1 parent e6dbedc commit 4b5d3f0

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/refactor_nrepl/artifacts.clj

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,18 @@
1313
java.util.zip.GZIPInputStream
1414
java.util.jar.JarFile))
1515

16+
(def artifacts-file ".artifacts-cache")
17+
18+
(defn get-last-modified-from-file
19+
"Returns last modified time in milliseconds or nil if file does not exist."
20+
[file]
21+
(let [lm (.lastModified (io/file file))]
22+
(if (zero? lm) nil lm)))
23+
1624
;; structure here is {"prismatic/schem" ["0.1.1" "0.2.0" ...]}
17-
(defonce artifacts (atom {} :meta {:last-modified nil}))
25+
(defonce artifacts (atom (slurp artifacts-file)
26+
:meta {:last-modified
27+
(get-last-modified-from-file artifacts-file)}))
1828
(def millis-per-day (* 24 60 60 1000))
1929

2030
(defn- get-proxy-opts
@@ -28,7 +38,7 @@
2838
(defn- stale-cache?
2939
[]
3040
(or (empty? @artifacts)
31-
(if-let [last-modified (some-> artifacts meta :last-modified .getTime)]
41+
(if-let [last-modified (some-> artifacts meta :last-modified)]
3242
(neg? (- millis-per-day (- (.getTime (java.util.Date.)) last-modified)))
3343
true)))
3444

@@ -99,13 +109,15 @@
99109
(let [clojars-artifacts (future (get-artifacts-from-clojars!))
100110
maven-artifacts (future (get-artifacts-from-mvn-central!))]
101111
(reset! artifacts (into @clojars-artifacts @maven-artifacts))
102-
(alter-meta! artifacts update-in [:last-modified] (constantly (java.util.Date.)))))
112+
(spit artifacts-file @artifacts)
113+
(alter-meta! artifacts update-in [:last-modified]
114+
(constantly (get-last-modified-from-file artifacts-file)))))
103115

104116
(defn artifact-list
105117
[{:keys [force]}]
106118
(when (or (= force "true") (stale-cache?))
107119
(update-artifact-cache!)
108-
(spit ".artifacts-cache" @artifacts))
120+
(spit artifacts-file @artifacts))
109121
(->> @artifacts keys list*))
110122

111123
(defn artifact-versions

0 commit comments

Comments
 (0)