Skip to content

Commit e222b2e

Browse files
committed
Recommend approach for not caching certain results
1 parent 5e23c64 commit e222b2e

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

docs/Using.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ Finally, to explicitly evict an element in a cache, use the `evict` function:
4848

4949
For specific information about eviction policies and thresholds, view the specific documentation for each cache type listed in the next section.
5050

51+
Sometimes you may wish to cache a function except when it returns certain values,
52+
such as a resource-loading function that may return `nil` if the resource is
53+
(temporarily) unavailable. Because of the edge cases that can arise with doing
54+
multiple lookups on a cache -- protections from which are baked into `through`,
55+
`through-cache`, and `lookup-or-miss` -- it is better to write your code to
56+
simply cache all results and then, post-retrieval, `evict` the key if the
57+
result is something you don't want cached:
58+
59+
```clojure
60+
(let [result (wrapped/lookup-or-miss C :c load-my-resource)]
61+
(when (nil? result)
62+
(wrapped/evict C :c))
63+
result)
64+
```
65+
5166
## Builtin cache implementations
5267

5368
core.cache comes with a number of builtin immutable cache implementations, including (*click through for specific information*):

0 commit comments

Comments
 (0)