Skip to content

Commit 442652a

Browse files
committed
1 parent baeec4d commit 442652a

5 files changed

Lines changed: 34 additions & 8 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
.lein*
66
.lsp/.cache
77
.nrepl-port
8+
.portal/vs-code.edn
89
.project
910
.settings
1011
.vscode

README.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,26 @@ org.clojure/core.cache {:mvn/version "1.0.225"}
6262
Example Usage
6363
========================================
6464

65+
The [`clojure.core.cache` namespace](https://cljdoc.org/d/org.clojure/core.cache/CURRENT/api/clojure.core.cache)
66+
provides an API for immutable caches where
67+
it is expected that you would manage storage of these data structures.
68+
69+
The [`clojure.core.cache.wrapped` namespace](https://cljdoc.org/d/org.clojure/core.cache/CURRENT/api/clojure.core.cache.wrapped)
70+
provides the same API but over
71+
immutable caches already wrapped in an atom, which is generally a more
72+
intuitive API for straightforward use cases. In particular, this namespace
73+
adds a `lookup-or-miss` function which encapsulates the `has?`/`hit`/`miss`
74+
logic for the underlying cache as well as edge cases such as `lookup`
75+
returning `nil` if a cache item expires on fetch (e.g., TTL), and guaranteeing
76+
the value computing function is only called at most once.
77+
78+
The expectation is that you use _either_ `clojure.core.cache` _or_
79+
`clojure.core.cache.wrapped` and you do not try to mix them.
80+
6581
```clojure
6682
(require '[clojure.core.cache :as cache])
6783

84+
;; C1 is an immutable cache:
6885
(def C1 (cache/fifo-cache-factory {:a 1, :b 2}))
6986

7087
(def C1' (if (cache/has? C1 :c)
@@ -107,24 +124,28 @@ Example Usage
107124
;=> 1
108125

109126
;; or use the wrapped API instead:
110-
(require '[clojure.core.cache.wrapped :as c])
127+
(require '[clojure.core.cache.wrapped :as w])
111128

112-
(def C3 (c/fifo-cache-factory {:a 1, :b 2}))
129+
;; in this case C3 is an atom containing a cache:
130+
(def C3 (w/fifo-cache-factory {:a 1, :b 2}))
113131

114-
(c/through-cache C3 :d (constantly 13)) ; returns updated cache
132+
;; operations modify the atom and return the updated cache:
133+
(w/through-cache C3 :d (constantly 13))
115134

116135
;=> {:a 1, :b 3, :d 13}
117136

118-
(c/evict C3 :b)
137+
;; modifies the atom and returns the updated cache:
138+
(w/evict C3 :b)
119139

120140
;=> {:a 1, :d 13}
121141

122-
(c/lookup C3 :a) ; or (get @C3 :a)
142+
;; for some caches this is a mutating operation (e.g., TTL):
143+
(w/lookup C3 :a) ; or (get @C3 :a)
123144

124145
;=> 1
125146

126147
;; unique to the wrapped API:
127-
(c/lookup-or-miss C3 :b (constantly 42))
148+
(w/lookup-or-miss C3 :b (constantly 42))
128149

129150
;=> 42
130151

docs/Composing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Put in simpler terms, to create a cache instance composed of the seed data `{:a
1010
;; used right away
1111

1212
(assoc C :c 42)
13-
;;=> {:b 2, :c 42}
13+
;;=> {:b 2, :c 42}
1414

15-
;; used after 5 seconds
15+
;; used after 5 seconds
1616

1717
(assoc C :d 138)
1818
;;=> {:d 138}

docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
core.cache
22
==========
33

4+
> Note: the documentation here is for the `clojure.core.cache` namespace. See also the documentation for the [`clojure.core.cache.wrapped` namespace](https://cljdoc.org/d/org.clojure/core.cache/CURRENT/api/clojure.core.cache.wrapped) on cljdoc.org.
5+
46
## Table of Topics
57

68
* Overview (this page)

docs/Using.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
> Note: the documentation here is for the `clojure.core.cache` namespace. See also the documentation for the [`clojure.core.cache.wrapped` namespace](https://cljdoc.org/d/org.clojure/core.cache/CURRENT/api/clojure.core.cache.wrapped) on cljdoc.org.
2+
13
# Using core.cache
24

35
*note: see the page on [including core.cache](./Including.md) before you begin this section*

0 commit comments

Comments
 (0)