Skip to content

Instantly share code, notes, and snippets.

@tmountain
Created September 28, 2017 16:13
Show Gist options
  • Save tmountain/b375015a7ced4902fa717ed37d2caa20 to your computer and use it in GitHub Desktop.
Save tmountain/b375015a7ced4902fa717ed37d2caa20 to your computer and use it in GitHub Desktop.
(defn epoch
[]
(quot (System/currentTimeMillis) 1000))
(let [cache (atom {})]
(defn add-key
[k v expire]
(swap! cache assoc k {:v v, :e expire, :c (epoch)}))
(defn del-key
[k]
(swap! cache dissoc k))
(defn get-key
[k]
(get @cache k))
(defn get-keys
[]
(keys @cache)))
(defn remove-expired
[]
(loop []
(do
(doseq [k (get-keys)]
(let [now (epoch)
{e :e, c :c} (get-key k)]
(if (> now (+ e c))
(del-key k))))
(Thread/sleep 1000)
(recur))))
(def expire-handler (future (remove-expired)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment