Skip to content

Commit

Permalink
Don't use transients for cache expiry
Browse files Browse the repository at this point in the history
The library didn't expect another `clojure.core.cache` cache as it's
seed value. And it tried to do things with it that it didn't
support. I.e., it tried to build a transient value out of it, which
`clojure.core.cache` compatible caches don't support. This is easily
fixed by changing the library not to convert the cache info into a
transient value.

Using transients was supposed a performance optimization, but as far
as we've seen, it doesn't make a noticeable difference at all.
  • Loading branch information
iarenaza committed Apr 7, 2021
1 parent 97f1e1a commit 6e24bf7
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions src/coop/magnet/ttlcache.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,16 @@
(defn- setval [newkey newval ttl [info ttls :as cache]]
[(assoc info newkey newval) (assoc ttls newkey (expires ttl))])

(defn- expire*
([info ttls now]
(if (empty? ttls)
[(persistent! info) ttls]
(let [[key expires] (peek ttls)]
(if (> now expires)
(do
(comment println "Expiring " key)
(recur (dissoc! info key) (pop ttls) now))
[(persistent! info) ttls]
)))))


(defn- expire
([cache]
(expire cache (System/currentTimeMillis)))
([[info ttls :as cache] now]
(expire* (transient info) ttls now)))
(expire cache (System/currentTimeMillis)))
([[info ttls] now]
(if (empty? ttls)
[(persistent! info) ttls]
(let [[key expires] (peek ttls)]
(if (> now expires)
(recur [(dissoc info key) (pop ttls)] now)
[info ttls])))))

(defcache PerItemTTLCache [cache expiry-heap get-ttl]
CacheProtocol
Expand Down

0 comments on commit 6e24bf7

Please sign in to comment.