Skip to content

Commit

Permalink
Wait 5s before triggering another clean
Browse files Browse the repository at this point in the history
for the same type or id from the watcher.
  • Loading branch information
Vinai committed Dec 21, 2021
1 parent ddfcf39 commit d95b541
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
32 changes: 4 additions & 28 deletions src/cache/cache.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -82,38 +82,14 @@
(let [varnish (varnish/create (mage/varnish-hosts-config))]
(storage/clean-all varnish)))

(defn- now []
(.getTime (js/Date.)))

(def cache-clean-grace-period 500) ;; ms

(let [last-cleaned-map (atom {})]

(defn- last-cleaned [cache-type-or-id]
(get @last-cleaned-map cache-type-or-id))

(defn- update-last-cleaned [cache-type-or-id]
(swap! last-cleaned-map assoc cache-type-or-id (now))))

(defn- may-clean? [cache-type-or-id]
(let [prev (last-cleaned cache-type-or-id)
t (now)]
(if (or (nil? prev) (< 0 (- t prev cache-clean-grace-period)))
(do (update-last-cleaned cache-type-or-id)
#_(log/notice "OK to clean " cache-type-or-id "since")
true)
(do #_(log/notice "WAIT grace period " cache-type-or-id (str (- t prev cache-clean-grace-period) "ms"))
false))))

(defn- clean-cache-types-with-base-dir [base-dir cache-types]
(when (or (empty? cache-types) (not= ["full_page"] cache-types))
(log/debug "Using :default cache_backend")
(let [cache (get-storage (mage/cache-config base-dir :default))
cache-types (filter may-clean? (remove #(= "full_page" %) cache-types))]
(apply clean cache cache-types)
(let [cache (get-storage (mage/cache-config base-dir :default))]
(apply clean cache (remove #(= "full_page" %) cache-types))
(storage/close cache)))

(when (or (empty? cache-types) (some #{"full_page"} (filter may-clean? cache-types)))
(when (or (empty? cache-types) (some #{"full_page"} cache-types))
(clean-full-page-cache base-dir)))

(defn clean-cache-types [cache-types]
Expand All @@ -131,6 +107,6 @@
(run! #(storage/clean-id cache %)))))

(defn clean-cache-ids [ids]
(when (seq (filter may-clean? ids))
(when (seq ids)
(apply log/notice "Cleaning id(s):" ids)
(run! #(clean-cache-ids-with-base-dir % ids) (mage/all-base-dirs))))
40 changes: 38 additions & 2 deletions src/magento/watcher.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,50 @@
(not (string/ends-with? file "~"))
(not (in-process? file))))

(defn- now []
(.getTime (js/Date.)))

(def cache-clean-guard-period 5000) ;; ms

(let [last-cleaned-map (atom {})]

(defn- last-cleaned [cache-type-or-id]
(get @last-cleaned-map cache-type-or-id))

(defn- update-last-cleaned [cache-type-or-id]
(swap! last-cleaned-map assoc cache-type-or-id (now))))

(defn- may-clean? [cache-type-or-id]
(let [prev (last-cleaned cache-type-or-id)
t (now)]
(if (or (nil? prev) (< 0 (- t prev cache-clean-guard-period)))
(do (update-last-cleaned cache-type-or-id)
#_(log/notice "OK to clean " cache-type-or-id "since")
true)
(do #_(log/notice "WAIT grace period " cache-type-or-id (str (- t prev cache-clean-guard-period) "ms"))
false))))

(defn- clean-cache-types [types]
(if (seq types)
(let [types (filter may-clean? types)]
(when (seq types) (cache/clean-cache-types types)))
(when (may-clean? :all)
(cache/clean-cache-types types))))

(defn- clean-cache-ids [ids]
(let [ids (filter may-clean? ids)]
(when (seq ids)
(cache/clean-cache-ids ids))))


(defn file-changed [file]
(when (process-changed-file? file)
(set-in-process! file)
(log/info "Processing" file)
(when-let [types (seq (cache/magefile->cachetypes file))]
(cache/clean-cache-types types))
(clean-cache-types types))
(when-let [ids (cache/magefile->cacheids file)]
(cache/clean-cache-ids ids))
(clean-cache-ids ids))
(clean-cache-for-new-controller file)
(clean-cache-for-service-interface file)
(remove-generated-files-based-on! file)
Expand Down

0 comments on commit d95b541

Please sign in to comment.