Skip to content

Commit 947c3ab

Browse files
committed
Add redis cache storage support
1 parent 81e767b commit 947c3ab

File tree

8 files changed

+86
-48
lines changed

8 files changed

+86
-48
lines changed

build.cljs.edn

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{:main cache.core
1+
{:main cache.run
22
:target :nodejs
33
:infer-externs true
44
:npm-deps {:redis "2.8.0"}

dev.cljs.edn

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{:main cache.cache
1+
{:main cache.core
22
:target :nodejs
33
:npm-deps {:redis "2.8.0"}
44
:install-deps true}

src/cache/cache.cljs

+3-5
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@
3636
(defn- clean
3737
([cache] (storage/clean-all cache))
3838
([cache type]
39-
(let [tag (cachetype->tag type)]
39+
(let [tag (cachetype->tag type)
40+
prefixed-tag (str (storage/magento-instance-cache-id-prefix) tag)]
4041
(log/debug "Cleaning tag" tag)
41-
(storage/clean-tag cache tag)))
42+
(storage/clean-tag cache prefixed-tag)))
4243
([cache type & types]
4344
(run! #(clean cache %) (into [type] types))))
4445

@@ -52,9 +53,6 @@
5253
(redis/create config)
5354
(file/create config))))
5455

55-
(defn tag->ids [cache tag]
56-
(storage/tag->ids cache tag))
57-
5856
(defn clean-cache-types [cache-types]
5957
(if (seq cache-types)
6058
(apply log/notice "Cleaning cache type(s)" cache-types)

src/cache/core.cljs

-2
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,3 @@ Clean the given cache types. If none are given, clean all cache types.
114114
(binding [*print-fn* *print-err-fn*]
115115
(println "[ERROR]" (or (.-message e) e)))
116116
(exit-with-code 1)))))
117-
118-
(set! *main-cli-fn* -main)

src/cache/run.cljs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(ns cache.run
2+
(:require [cache.core]))
3+
4+
(defn -main [& args]
5+
(apply cache.core/-main args))
6+
7+
(set! *main-cli-fn* -main)

src/cache/storage.cljs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
(ns cache.storage)
1+
(ns cache.storage
2+
(:require [magento.app :as mage]
3+
[file.system :as fs]))
4+
5+
(defn md5 [^String data]
6+
(let [crypto (js/require "crypto")]
7+
(-> crypto (.createHash "md5") (.update data) (.digest "hex"))))
8+
9+
(defn magento-instance-cache-id-prefix []
10+
(str (subs (md5 (str (fs/realpath (mage/base-dir)) "/app/etc/")) 0 3) "_"))
211

312
(defprotocol CacheStorage
4-
(tag->ids [storage tag] "Takes a cache tag and returns a seq of ids associated with the tag.")
5-
(delete [storage id] "Deletes the cache record with the given ID")
613
(clean-tag [storage tag] "Deletes all cache records associated with the given tag.")
714
(clean-all [storage] "Delete all cache records and tags."))

src/cache/storage/file.cljs

+14-24
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,51 @@
99
{:file-name-prefix "mage"
1010
:hashed-directory-level 1})
1111

12-
(defn- md5 [^String data]
13-
(let [crypto (js/require "crypto")]
14-
(-> crypto (.createHash "md5") (.update data) (.digest "hex"))))
15-
1612
(defn- file-name-prefix []
1713
(:file-name-prefix options))
1814

19-
(defn- cache-id-prefix []
20-
(str (subs (md5 (str (fs/realpath (mage/base-dir)) "/app/etc/")) 0 3) "_"))
21-
2215
(defn- chars-from-end [^String s length]
2316
(subs s (- (count s) length)))
2417

2518
(defn- path [^String cache-dir ^String id]
2619
(let [length (:hashed-directory-level options)]
2720
(if (< 0 length)
28-
(let [suffix (chars-from-end (md5 (str (cache-id-prefix) id)) length)]
21+
(let [suffix (chars-from-end (storage/md5 id) length)]
2922
(str cache-dir (file-name-prefix) "--" suffix "/"))
3023
cache-dir)))
3124

3225
(defn- tag-path [cache-dir]
3326
(str cache-dir (file-name-prefix) "-tags/"))
3427

3528
(defn- id->filename [^String id]
36-
(str (file-name-prefix) "---" (cache-id-prefix) id))
29+
(str (file-name-prefix) "---" id))
3730

3831
(defn- id->filepath [^String cache-dir ^String id]
3932
(str (path cache-dir id) (id->filename id)))
4033

4134
(defn- tag->filepath [^String cache-dir ^String tag]
4235
(str (tag-path cache-dir) (id->filename tag)))
4336

44-
(defn- remove-cache-id-prefix [id-with-prefix]
45-
(subs id-with-prefix 4))
37+
(defn- tag->ids [cache-dir tag]
38+
(let [file (tag->filepath cache-dir tag)]
39+
(if (fs/exists? file)
40+
(string/split-lines (fs/slurp file))
41+
[])))
42+
43+
(defn- delete [cache-dir id]
44+
(log/debug "Cleaning id" id)
45+
(let [file (id->filepath cache-dir id)]
46+
(when (fs/exists? file)
47+
(fs/rm file))))
4648

4749
(defrecord File [cache-dir]
4850
storage/CacheStorage
4951

50-
(tag->ids [this tag]
51-
(let [file (tag->filepath cache-dir tag)]
52-
(if (fs/exists? file)
53-
(doall (map remove-cache-id-prefix (string/split-lines (fs/slurp file))))
54-
[])))
55-
56-
(delete [this id]
57-
(log/debug "Cleaning id" id)
58-
(let [file (id->filepath cache-dir id)]
59-
(when (fs/exists? file)
60-
(fs/rm file))))
61-
6252
(clean-tag [this tag]
6353
(let [tag-file (tag->filepath cache-dir tag)]
6454
(log/debug "Tag-file" tag-file)
6555
(when (fs/exists? tag-file)
66-
(run! #(storage/delete this %) (storage/tag->ids this tag))
56+
(run! #(delete cache-dir %) (tag->ids cache-dir tag))
6757
(fs/rm tag-file))))
6858

6959
(clean-all [this]

src/cache/storage/redis.cljs

+50-12
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,60 @@
11
(ns cache.storage.redis
2-
(:require [cache.storage :as storage]
3-
[log.log :as log]))
2+
(:require [file.system :as fs]
3+
[magento.app :as mage]
4+
[cache.storage :as storage]
5+
[log.log :as log]
6+
[clojure.string :as string]))
47

5-
(defrecord Redis [config]
6-
storage/CacheStorage
8+
(defonce redis (js/require "redis"))
9+
10+
(def ^:const set-ids "zc:ids")
11+
(def ^:const set-tags "zc:tags")
12+
(def ^:const prefix-key "zc:k:")
13+
(def ^:const prefix-tag-id "zc:ti:")
14+
15+
(defn- localhost? [host]
16+
(#{"localhost" "127.0.0.1"} host))
17+
18+
(defn- default-port? [port]
19+
(= "6379" port))
720

8-
(tag->ids [this tag]
9-
[])
21+
(defn- connect-db [config]
22+
(or (-> config :backend_options :database) 0))
1023

11-
(delete [this id]
12-
)
24+
(defn- connect-options [config]
25+
(let [{:keys [server port password]} (:backend_options config)
26+
database (connect-db config)]
27+
(cond-> {}
28+
(and server (not (localhost? server))) (assoc :host server)
29+
(and port (not (default-port? port))) (assoc :port port)
30+
password (assoc :auth_pass password)
31+
database (assoc :db database))))
32+
33+
(defn- tag->ids [client tag callback]
34+
(.smembers client (str prefix-tag-id tag)
35+
(fn [err ids]
36+
(callback ids))))
37+
38+
(defn- delete-tag-and-ids [client tag ids]
39+
;; TODO: make multi command
40+
(run! #(log/debug "Cleaning id" %) ids)
41+
(.del client (doall (map #(str prefix-key %) ids)))
42+
(.srem client set-ids ids)
43+
(.del client (str prefix-tag-id tag))
44+
(.srem client set-tags tag))
45+
46+
(defrecord Redis [client database]
47+
storage/CacheStorage
1348

1449
(clean-tag [this tag]
15-
)
50+
(let [callback (partial delete-tag-and-ids client tag)]
51+
(tag->ids client tag callback)))
1652

1753
(clean-all [this]
18-
))
54+
(log/debug "Flushing redis db" database)
55+
(.flushdb client)))
1956

2057
(defn create [config]
21-
(log/error "[Error] REDIS cache storage not yet supported.")
22-
(->Redis config))
58+
(let [options (connect-options config)
59+
client (.createClient redis (clj->js options))]
60+
(->Redis client (connect-db config))))

0 commit comments

Comments
 (0)