Skip to content

Commit

Permalink
Merge pull request #15 from AppsFlyer/caffeine-2.9.0
Browse files Browse the repository at this point in the history
caffeine 2.9.0
  • Loading branch information
barkanido authored Feb 18, 2021
2 parents dc72310 + fec1bef commit 8a54e6b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,4 @@ fabric.properties
.classpath
.project
.settings
.lsp
4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(defproject com.appsflyer/cloffeine "0.1.9"
(defproject com.appsflyer/cloffeine "0.2.0"
:description "A warpper over https://github.com/ben-manes/caffeine"
:url "https://github.com/AppsFlyer/cloffeine"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[com.github.ben-manes.caffeine/caffeine "2.8.8"]]
:dependencies [[com.github.ben-manes.caffeine/caffeine "2.9.0"]]
:plugins [[lein-codox "0.10.7"]]
:codox {:output-path "codox"
:source-uri "http://github.com/AppsFlyer/cloffeine/blob/{version}/{filepath}#L{line}"
Expand Down
33 changes: 13 additions & 20 deletions src/cloffeine/common.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:import [com.github.benmanes.caffeine.cache AsyncCacheLoader
Cache
CacheLoader
CacheWriter
RemovalListener
Caffeine
Weigher]
[com.github.benmanes.caffeine.cache.stats CacheStats]
Expand Down Expand Up @@ -49,16 +49,13 @@
* `:ticker` `com.github.benmanes.caffeine.cache.Ticker` Specifies a
nanosecond-precision time source for use in determining when entries should
be expired or refreshed. By default, System.nanoTime() is used.
* `:removalListener` `com.github.benmanes.caffeine.cache.RemovalListener`.
* `:weigher` `com.github.benmanes.caffeine.cache.Weigher` Specifies the weigher
to use in determining the weight of entries.
* `:evictionListener` `com.github.benmanes.caffeine.cache.RemovalListener`
Specifies a listener instance that caches should notify each time an entry
is removed for any reason. Each cache created by this builder will invoke
this listener as part of the routine maintenance described in the class
documentation above.
* `:weigher` `com.github.benmanes.caffeine.cache.Weigher` Specifies the weigher
to use in determining the weight of entries.
* `:writer` `com.github.benmanes.caffeine.cache.CacheWriter` Specifies a writer
instance that caches should notify each time an entry is explicitly created
or modified, or removed for any reason.
* `:timeUnit` ``[:ms :us :s :m :h :d]`` default is `:s`"
^Caffeine [settings]
(let [bldr (Caffeine/newBuilder)
Expand All @@ -83,7 +80,7 @@
(:ticker settings) (.ticker (:ticker settings))
(:removalListener settings) (.removalListener (:removalListener settings))
(:weigher settings) (.weigher (:weigher settings))
(:writer settings) (.writer (:writer settings)))))
(:evictionListener settings) (.evictionListener (:evictionListener settings)))))

(defn reify-async-cache-loader
"A helper for implementing an `AsyncCacheLoader`.
Expand Down Expand Up @@ -125,19 +122,15 @@
(reload [_this k v]
(reloading-fn k v)))))

(defn reify-cache-writer
"A helper for implementing `CacheWriter`
(defn reify-removal-listener
"A helper for implementing `RemovalListener`
* `delete-handler` - `(fn [this k v removal-cause])`. Deletes the value
corresponding to the key from the external resource.
* `write-handler` - `(fn [this k v])`. Writes the value corresponding to the
key to the external resource."
[delete-handler write-handler]
(reify CacheWriter
(delete ^void [this k v removal-cause]
(delete-handler this k v removal-cause))
(write ^void [this k v]
(write-handler this k v))))
* `removal-handler` - `(fn [k v removal-cause])`. React upon removal
of k. `cause` is a `RemovalCause` enum: {COLLECTED|EXPIRED|EXPLICIT|REPLACED|SIZE}"
[removal-handler]
(reify RemovalListener
(onRemoval [_this k v cause]
(removal-handler k v cause))))

(defn reify-weigher
"A helper for implementing `Weigher`.
Expand Down
11 changes: 10 additions & 1 deletion test/cloffeine/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[promesa.core :as p]
[clojure.string :as s])
(:import [com.google.common.testing FakeTicker]
[com.github.benmanes.caffeine.cache Ticker]
[com.github.benmanes.caffeine.cache Ticker RemovalCause]
[java.util.concurrent TimeUnit]
[java.util.logging Logger Level]
[clojure.lang ExceptionInfo]))
Expand Down Expand Up @@ -192,6 +192,7 @@
(let [loads (atom 0)
reloads (atom 0)
reload-fails? (atom false)
replaced (atom [])
db (atom {:key 17})
cl (common/reify-cache-loader
(fn [k]
Expand All @@ -202,9 +203,14 @@
(if @reload-fails?
(throw (Exception. (format "reload failed for key: %s" k)))
(get @db k))))
removal-listener (common/reify-removal-listener
(fn [_k v cause]
(when (= cause RemovalCause/REPLACED)
(swap! replaced conj v))))
ticker (FakeTicker.)
lcache (loading-cache/make-cache cl {:refreshAfterWrite 10
:timeUnit :s
:removalListener removal-listener
:ticker (reify-ticker ticker)})]
(testing "no key, load succeeds"
(is (= 17 (loading-cache/get lcache :key)))
Expand All @@ -222,6 +228,7 @@
(loading-cache/cleanup lcache)
(Thread/sleep 10)
(is (= 1 @reloads))
(is (= [17] @replaced))
(is (= 42 (loading-cache/get lcache :key))))
(testing "time to refresh again, but reload fails"
(reset! reload-fails? true)
Expand All @@ -231,12 +238,14 @@
(is (= 1 @reloads))
(is (= 42 (loading-cache/get lcache :key)))
(is (= 1 @reloads))
(is (= [17] @replaced))
(reset! reload-fails? false)
(.advance ticker 10 TimeUnit/SECONDS)
(is (= 42 (loading-cache/get lcache :key)))
(loading-cache/cleanup lcache)
(Thread/sleep 10)
(is (= 2 @reloads))
(is (= [17 42] @replaced))
(is (= 43 (loading-cache/get lcache :key)))
(is (= 2 @reloads)))))

Expand Down

0 comments on commit 8a54e6b

Please sign in to comment.