Skip to content

Commit d2a4fa0

Browse files
committed
Show how caching works
1 parent 6d835f8 commit d2a4fa0

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Diff for: notebooks/how_clerk_works.clj

+14-3
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,27 @@
3737

3838
;; ### Step 3: Hashing
3939
;; Then we can use this information to hash each expression.
40-
41-
(nextjournal.clerk.hashing/hash "notebooks/how_clerk_works.clj")
40+
(def hashes
41+
(nextjournal.clerk.hashing/hash "notebooks/how_clerk_works.clj"))
4242

4343
;; ### Step 4: Caching
4444
;; Clerk uses the hashes as filenames and only re-evaluates forms that haven't been seen before. The cache is currently using edn with `pr-str` and `read-string`.
45+
(def rand-three
46+
(shuffle (range 3)))
47+
48+
;; We can look up the cache key using the var name in the hashes map.
49+
(->> (get hashes #'how-clerk-works/rand-three)
50+
(str ".cache/")
51+
slurp
52+
read-string)
4553

4654
;; As an escape hatch, you can tag a form or var with `:clerk/no-cache` to always reevalaute it. he following form will never be cached.
4755
^:clerk/no-cache (shuffle (range 42))
4856

4957
;; For side effectful functions that should be cached, like a database query, you can add a value like this `#inst` to control when evaluation should happen.
5058
(def query-results
5159
(let [_run-at #inst "2021-05-20T08:28:29.445-00:00"]
52-
))
60+
#_
61+
(let [my-datasource (jdbc/get-datasource {:dbtype "..." :dbname "..."})]
62+
(with-open [connection (jdbc/get-connection my-datasource)]
63+
(jdbc/execute! connection [...])))))

0 commit comments

Comments
 (0)