Created
September 28, 2017 16:13
-
-
Save tmountain/b375015a7ced4902fa717ed37d2caa20 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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