Created
March 15, 2018 00:19
-
-
Save aterweele/71136a8da4b495883592c9aaf180b555 to your computer and use it in GitHub Desktop.
This file contains 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
(ns file-backed-cache.core | |
(:require [clojure.core.cache :as cache :refer [defcache]] | |
[clojure.edn :as edn] | |
[clojure.java.io :as io] | |
[clojure.pprint :as pp] | |
[clojure.test :refer :all] | |
[hasch.core :as hash]) | |
(:import [clojure.lang IDeref] | |
[java.io PushbackReader] | |
[java.nio.file Files] | |
[java.nio.file.attribute FileAttribute])) | |
(defrecord Persisted [file] | |
IDeref | |
(deref [_] | |
(let [v (-> file io/reader PushbackReader. edn/read)] | |
(io/delete-file file) | |
v))) | |
(defmethod pp/simple-dispatch Persisted [{:keys [file]}] (pr file)) | |
(defn persist | |
[v] | |
(let [f (io/file store-location (str (hash/uuid v)))] | |
(pr spit f v) | |
(->Persisted f))) | |
;; Not only does this test fail (likely due to confounding variables), | |
;; it also made me realize that because the file is never | |
;; dereferenced, it is never deleted -- it is effectively leaked. | |
(deftest mem-usage | |
(let [mem-0 (.totalMemory (Runtime/getRuntime)) | |
large-object-holder (->> rand | |
repeatedly | |
(take (* 1024 1024 4)) | |
vec | |
atom) | |
mem-1 (.totalMemory (Runtime/getRuntime)) | |
_ (swap! large-object-holder file-backed-cache.core/persist) | |
_ (System/gc) | |
mem-2 (.totalMemory (Runtime/getRuntime))] | |
(is (< mem-0 mem-1)) | |
(is (< mem-2 mem-1)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment