Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
To comply with Clojure community style guide.
  • Loading branch information
iarenaza committed Apr 7, 2021
1 parent 424e124 commit f8c78d9
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions src/coop/magnet/ttlcache.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
;; Copyright 2014 Rob Day
;; Copyright 2021 Magnet. S. Coop.
;; Released under the EPL


(ns coop.magnet.ttlcache
(:require [clojure.data.priority-map :refer [priority-map]]
[clojure.core.cache :refer [CacheProtocol defcache]]))
(:require [clojure.core.cache :refer [CacheProtocol defcache]]
[clojure.data.priority-map :refer [priority-map]]))

(defn- expires [ttl] (+ ttl (System/currentTimeMillis)))
(defn- expires
[ttl]
(+ ttl (System/currentTimeMillis)))

(defn- setval [newkey newval ttl [info ttls :as cache]]
(defn- setval
[newkey newval ttl [info ttls]]
[(assoc info newkey newval) (assoc ttls newkey (expires ttl))])

(defn- expire
Expand All @@ -34,50 +36,48 @@
(has? [_ item]
(let [expires (get expiry-heap item 0)]
(< (System/currentTimeMillis) expires)))
(hit [this item] this)
(miss [this item result]
(let [start (System/currentTimeMillis)
updated-cache (setval item result (get-ttl item result) [cache expiry-heap])
_ (comment println "Updated cache after " (- (System/currentTimeMillis) start))
after-expiry (expire updated-cache)
_ (comment println "Expired cache after " (- (System/currentTimeMillis) start))
]
(hit [this _]
this)
(miss [_ item result]
(let [updated-cache (setval item result (get-ttl item result) [cache expiry-heap])
after-expiry (expire updated-cache)]
(PerItemTTLCache. (first after-expiry)
(second after-expiry)
get-ttl)))
(seed [_ base]
(let [now (System/currentTimeMillis)]
(PerItemTTLCache. base
(into (priority-map) (for [x base] [(key x) (+ now (get-ttl (key x) (val x)) )]))
get-ttl)))
(let [now (System/currentTimeMillis)]
(PerItemTTLCache. base
(into (priority-map) (for [x base]
[(key x) (+ now (get-ttl (key x) (val x)))]))
get-ttl)))
(evict [_ key]
(PerItemTTLCache. (dissoc cache key)
(dissoc expiry-heap key)
get-ttl))
Object
(toString [_]
(str cache \, \space expiry-heap)))

(str cache \, \space expiry-heap)))

(defn per-item-ttl-cache-factory
"Returns a TTL cache with the cache and expiration-table initialied to `base` --
each with the same time-to-live.
each item in `base` with its own time-to-live.
This function also allows an optional `:ttl` argument that defines the default
time in milliseconds that entries are allowed to reside in the cache."
This function also takes a `:get-ttl` keyword argument that defines
a function which is applied to the key and value of any added entry,
and is expected to return the TTL -in milli-seconds- for the entry."
[base & {get-ttl :ttl-getter}]
{:pre [(map? base)
(fn? get-ttl)]}
(clojure.core.cache/seed (PerItemTTLCache. {} (priority-map) get-ttl) base))

(defn ttl-cache-factory
"Returns a TTL cache with the cache and expiration-table initialied to `base` --
each with the same time-to-live.
each with the same time-to-live.
This function also allows an optional `:ttl` argument that defines the default
time in milliseconds that entries are allowed to reside in the cache."
This function also allows an optional `:ttl` argument that defines the default
time in milliseconds that entries are allowed to reside in the cache. If not
specified, a default of 2000 milliseconds is used."
[base & {ttl :ttl :or {ttl 2000}}]
{:pre [(number? ttl) (<= 0 ttl)
{:pre [(and (number? ttl) (<= 0 ttl))
(map? base)]}
(per-item-ttl-cache-factory base :ttl-getter (constantly ttl)))

0 comments on commit f8c78d9

Please sign in to comment.