Skip to content

Commit 31592ef

Browse files
committed
address CCACHE-67 by applying patch from dharrigan
Signed-off-by: Sean Corfield <sean@corfield.org>
1 parent f47487c commit 31592ef

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ Change Log
174174
====================
175175

176176
* Release 1.2.next in progress
177+
* [CCACHE-67](http://clojure.atlassian.net/browse/CCACHE-67) Add `clojure.core.cache.wrapped/size` function to get the size of the cache without dereferencing the atom. (via [@dharrigan](https://github.com/dharrigan)).
177178
* [CCACHE-65](http://clojure.atlassian.net/browse/CCACHE-65) Use `delay` in `lookup-or-miss` to avoid cache-stampede.
178179
* Release 1.1.234 on 2024-02-19
179180
* Update parent pom and `data.priority-map` versions

src/main/clojure/clojure/core/cache/wrapped.clj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@
7676
[cache-atom e]
7777
(c/has? @cache-atom e))
7878

79+
(defn size
80+
"Obtain the approximate size/count of the wrapped cache.
81+
82+
The value returned is a point-in-time approximate count/size as the nature
83+
of certain caches don't reflect their true size until a 'lookup-or-miss'
84+
operation is invoked - such as the TTL cache.
85+
86+
Whilst it is an approximate value, the returned result can be useful for
87+
metric tracking or decision making.
88+
89+
Returns an approximate count/size of the wrapped cache.
90+
"
91+
[cache-atom]
92+
(count @cache-atom))
93+
7994
(defn hit
8095
"Is meant to be called if the cache is determined to contain a value
8196
associated with `e`.

src/test/clojure/clojure/core/cache/wrapped_test.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,25 @@
1414
(deftest basic-wrapped-test
1515
(let [cache (c/basic-cache-factory {})]
1616
(is (= nil (c/lookup cache :a)))
17+
;; the size of the cache is zero
18+
(is (= 0 (c/size cache)))
1719
(is (= ::missing (c/lookup cache :a ::missing)))
1820
;; mutating operation
1921
(is (= 42 (c/lookup-or-miss cache :a (constantly 42))))
2022
;; cache now contains :a
2123
(is (= 42 (c/lookup cache :a)))
24+
;; and it now has a size of one
25+
(is (= 1 (c/size cache)))
2226
;; :a is present, does not call value-fn
2327
(is (= 42 (c/lookup-or-miss cache :a #(throw (ex-info "bad" {:key %})))))
2428
;; cache still contains :a as 42
2529
(is (= 42 (c/lookup cache :a)))
30+
;; and it still has a size of one
31+
(is (= 1 (c/size cache)))
2632
(c/evict cache :a)
2733
(is (= nil (c/lookup cache :a)))
34+
;; and the cache now has a size of zero
35+
(is (= 0 (c/size cache)))
2836
(is (= ::missing (c/lookup cache :a ::missing)))))
2937

3038
(deftest wrapped-ttl-test

0 commit comments

Comments
 (0)