Skip to content

Commit 5715dea

Browse files
committed
Title: refactor: extract cache logic to eca.cache namespace
Moves cache directory resolution and workspace hashing from `eca.db` to a new `eca.cache` namespace. Centralizes file path management and allows other components (hooks) to acess cache paths. Avoids circular dependencies on the database namespace.
1 parent 90d71cc commit 5715dea

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

src/eca/cache.clj

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(ns eca.cache
2+
"Cache directory and file management utilities."
3+
(:require
4+
[babashka.fs :as fs]
5+
[clojure.java.io :as io]
6+
[clojure.string :as string]))
7+
8+
(set! *warn-on-reflection* true)
9+
10+
(defn global-dir
11+
"Returns the File object for ECA's global cache directory."
12+
[]
13+
(let [cache-home (or (System/getenv "XDG_CACHE_HOME")
14+
(io/file (System/getProperty "user.home") ".cache"))]
15+
(io/file cache-home "eca")))
16+
17+
(defn workspaces-hash
18+
"Returns an 8-char base64 (URL-safe, no padding) hash key for the given workspace set."
19+
[workspaces uri->filename-fn]
20+
(let [paths (->> workspaces
21+
(map #(str (fs/absolutize (fs/file (uri->filename-fn (:uri %))))))
22+
(distinct)
23+
(sort))
24+
joined (string/join ":" paths)
25+
md (java.security.MessageDigest/getInstance "SHA-256")
26+
digest (.digest (doto md (.update (.getBytes joined "UTF-8"))))
27+
encoder (-> (java.util.Base64/getUrlEncoder)
28+
(.withoutPadding))
29+
key (.encodeToString encoder digest)]
30+
(subs key 0 (min 8 (count key)))))
31+
32+
(defn workspace-cache-file
33+
"Returns a File object for a workspace-specific cache file."
34+
[workspaces filename uri->filename-fn]
35+
(io/file (global-dir)
36+
(workspaces-hash workspaces uri->filename-fn)
37+
filename))

src/eca/db.clj

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
(:require
33
[babashka.fs :as fs]
44
[clojure.java.io :as io]
5-
[clojure.string :as string]
65
[cognitect.transit :as transit]
7-
[eca.config :as config :refer [get-env get-property]]
6+
[eca.cache :as cache]
87
[eca.logger :as logger]
98
[eca.metrics :as metrics]
109
[eca.shared :as shared])
@@ -123,32 +122,11 @@
123122
(proxy-super flush)
124123
(proxy-super close)))))
125124

126-
(defn ^:private global-cache-dir []
127-
(let [cache-home (or (get-env "XDG_CACHE_HOME")
128-
(io/file (get-property "user.home") ".cache"))]
129-
(io/file cache-home "eca")))
130-
131-
(defn ^:private workspaces-hash
132-
"Return an 8-char base64 (URL-safe, no padding) key for the given
133-
workspace set."
134-
[workspaces]
135-
(let [paths (->> workspaces
136-
(map #(str (fs/absolutize (fs/file (shared/uri->filename (:uri %))))))
137-
(distinct)
138-
(sort))
139-
joined (string/join ":" paths)
140-
md (java.security.MessageDigest/getInstance "SHA-256")
141-
digest (.digest (doto md (.update (.getBytes joined "UTF-8"))))
142-
encoder (-> (java.util.Base64/getUrlEncoder)
143-
(.withoutPadding))
144-
key (.encodeToString encoder digest)]
145-
(subs key 0 (min 8 (count key)))))
125+
(defn ^:private transit-global-db-file []
126+
(io/file (cache/global-dir) "db.transit.json"))
146127

147128
(defn ^:private transit-global-by-workspaces-db-file [workspaces]
148-
(io/file (global-cache-dir) (workspaces-hash workspaces) "db.transit.json"))
149-
150-
(defn ^:private transit-global-db-file []
151-
(io/file (global-cache-dir) "db.transit.json"))
129+
(cache/workspace-cache-file workspaces "db.transit.json" shared/uri->filename))
152130

153131
(defn ^:private read-cache [cache-file metrics]
154132
(try

0 commit comments

Comments
 (0)