From 2d0adce8c35f12b63144f8f2185813d0696943c0 Mon Sep 17 00:00:00 2001 From: Aeonax Date: Fri, 28 Mar 2025 11:46:00 +0100 Subject: [PATCH 1/5] Added `get-cache-keys` and `invalidate-keys` --- src/fmnoise/coldbrew.clj | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/fmnoise/coldbrew.clj b/src/fmnoise/coldbrew.clj index 37d5be2..3e48a3b 100644 --- a/src/fmnoise/coldbrew.clj +++ b/src/fmnoise/coldbrew.clj @@ -86,6 +86,11 @@ (.build cache-builder (cache-loader cache-fn)) (.build cache-builder)))) +(defn get-cache-keys + "Returns a list of cache keys" + [^Cache cache] + (.keySet (.asMap cache))) + (defn put "Insert/update a cache entry." [^Cache cache key val] @@ -102,6 +107,11 @@ [^Cache cache key] (.invalidate cache key)) +(defn invalidate-keys + "Invalidate a cache entries." + [^Cache cache keys] + (.invalidateAll cache keys)) + (defn lookup "Performs cache lookup. Accepts optional function which uses cache key to calculate missing value" ([^Cache cache key] From 9f77eb5cda2ce482fc36b9b9e4b1f81a6afa5643 Mon Sep 17 00:00:00 2001 From: Aeonax Date: Fri, 28 Mar 2025 11:54:45 +0100 Subject: [PATCH 2/5] Added `::cache` meta to `cached` and `defcached` --- src/fmnoise/coldbrew.clj | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/fmnoise/coldbrew.clj b/src/fmnoise/coldbrew.clj index 3e48a3b..3254a6e 100644 --- a/src/fmnoise/coldbrew.clj +++ b/src/fmnoise/coldbrew.clj @@ -149,6 +149,7 @@ (let [options (meta f) condition-fn (-> f meta :when) cache (build-cache options (when-not condition-fn f))] + ^{::cache cache} (fn [& args] (let [key (or args [])] (if condition-fn @@ -191,11 +192,10 @@ :else 3)) fdefn (filter some? (list name docstring args prepost))] `(do - (def ^:private ~fname - (cached - (with-meta - (fn [~@cache-key] - ~@body) - ~cache-options))) - (defn ~@fdefn (~fname ~@cache-key)) - (vary-meta ~name merge ~(meta name))))) + (def ^:private ~fname (cached + (with-meta + (fn [~@cache-key] + ~@body) + ~cache-options))) + (defn ~@fdefn (~fname ~@cache-key)) + (alter-meta! (var ~name) merge (meta ~fname) ~(meta name))))) From 57fd5aaeb3accc5aa5d2f2f891c60ef33de703dc Mon Sep 17 00:00:00 2001 From: Aeonax Date: Fri, 28 Mar 2025 13:01:06 +0100 Subject: [PATCH 3/5] Added tests for meta --- test/fmnoise/coldbrew_test.clj | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/test/fmnoise/coldbrew_test.clj b/test/fmnoise/coldbrew_test.clj index 7086713..c648ee3 100644 --- a/test/fmnoise/coldbrew_test.clj +++ b/test/fmnoise/coldbrew_test.clj @@ -1,6 +1,7 @@ (ns fmnoise.coldbrew-test (:require [clojure.test :refer [deftest is]] - [fmnoise.coldbrew :refer [defcached cached]])) + [fmnoise.coldbrew :refer [defcached cached] :as cb]) + (:import (com.github.benmanes.caffeine.cache Cache))) (deftest cached-fn-test (let [counter (atom 0) @@ -71,14 +72,14 @@ (is (= 0 @store) "function body isn't called")))) (deftest defcached-declaration-test - (defcached f1 [a b] - ^{:expire 10} [a b] - (- a b) - (+ a b)) + (defcached ^:test-meta f1 [a b] + ^{:expire 10} [a b] + (- a b) + (+ a b)) (defcached f2 "adds a and b" [a b] - ^{:expire 10} [a b] - (- a b) - (+ a b)) + ^{:expire 10} [a b] + (- a b) + (+ a b)) (defcached f3 [a b] {:pre [(pos? a) (pos? b)]} ^{:expire 10} [a b] @@ -89,6 +90,7 @@ ^{:expire 10} [a b] (- a b) (+ a b)) + (is (and (:test-meta (meta #'f1)) (instance? Cache (::cb/cache (meta #'f1)))) "meta attached correctly") (is (= 3 (f1 1 2) (f2 1 2) (f3 1 2) (f4 1 2)) "function produces correct result") (is (= "adds a and b" (:doc (meta #'f2)) (:doc (meta #'f4))) "docstring is added to function meta") (is (thrown? AssertionError (f3 0 0)) "pre-conditions are added to function") From 1defc8050df88d17371346a501de7be9e0b0fe57 Mon Sep 17 00:00:00 2001 From: Aeonax Date: Wed, 16 Apr 2025 10:48:30 +0200 Subject: [PATCH 4/5] Added helpers to access cache object --- src/fmnoise/coldbrew.clj | 7 +++++++ test/fmnoise/coldbrew_test.clj | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/fmnoise/coldbrew.clj b/src/fmnoise/coldbrew.clj index 3254a6e..cf2c9f7 100644 --- a/src/fmnoise/coldbrew.clj +++ b/src/fmnoise/coldbrew.clj @@ -156,6 +156,9 @@ (cond-lookup cache key condition-fn f args) (lookup cache key)))))) +(defn cached-cache [cached-fn] + (-> (meta cached-fn) ::cache)) + (defmacro defcached "Creates a function which uses Caffeine Loading Cache under the hood. Function declaration is similar to defn: @@ -199,3 +202,7 @@ ~cache-options))) (defn ~@fdefn (~fname ~@cache-key)) (alter-meta! (var ~name) merge (meta ~fname) ~(meta name))))) + +(defmacro defcached-cache [defcache-fn] + `(cached-cache (var ~defcache-fn))) + diff --git a/test/fmnoise/coldbrew_test.clj b/test/fmnoise/coldbrew_test.clj index c648ee3..9e506c0 100644 --- a/test/fmnoise/coldbrew_test.clj +++ b/test/fmnoise/coldbrew_test.clj @@ -15,6 +15,9 @@ c3 @counter r4 (cf 2 3) c4 @counter] + (is (and (instance? Cache (::cb/cache (meta cf))) + (instance? Cache (cb/cached-cache cf))) + "meta attached correctly") (is (= 3 r1 r2 r3) "function produces correct result") (is (= 1 c1 c2) "counter is affected only on first run") (is (= 2 c3) "counter is affected after expiration") @@ -90,7 +93,10 @@ ^{:expire 10} [a b] (- a b) (+ a b)) - (is (and (:test-meta (meta #'f1)) (instance? Cache (::cb/cache (meta #'f1)))) "meta attached correctly") + (is (and (:test-meta (meta #'f1)) + (instance? Cache (::cb/cache (meta #'f1))) + (instance? Cache (cb/defcached-cache f1))) + "meta attached correctly") (is (= 3 (f1 1 2) (f2 1 2) (f3 1 2) (f4 1 2)) "function produces correct result") (is (= "adds a and b" (:doc (meta #'f2)) (:doc (meta #'f4))) "docstring is added to function meta") (is (thrown? AssertionError (f3 0 0)) "pre-conditions are added to function") From 01adbc2f9ac3cc32df5be5da4128fc11a1a3a7a7 Mon Sep 17 00:00:00 2001 From: Aeonax Date: Wed, 16 Apr 2025 10:48:54 +0200 Subject: [PATCH 5/5] Renamed `get-cache-keys` to `cache-keys` --- src/fmnoise/coldbrew.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fmnoise/coldbrew.clj b/src/fmnoise/coldbrew.clj index cf2c9f7..007e79f 100644 --- a/src/fmnoise/coldbrew.clj +++ b/src/fmnoise/coldbrew.clj @@ -86,7 +86,7 @@ (.build cache-builder (cache-loader cache-fn)) (.build cache-builder)))) -(defn get-cache-keys +(defn cache-keys "Returns a list of cache keys" [^Cache cache] (.keySet (.asMap cache)))