Skip to content

Commit

Permalink
wip3
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf committed Oct 14, 2024
1 parent ec393ca commit a57733f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 40 deletions.
33 changes: 14 additions & 19 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -760,38 +760,28 @@
(demolish [_ _])))
(registry key)))))


;; should a cache key be the one?
(defn collect-cache []
(let [cache (atom {})]
(let [cache (volatile! {})]
(fn [registry]
(fn [key]
(if (= ::cache key)
cache
#_
(reify p/Factory
(dependencies [_]
nil)
(build [_ _]
cache)
(demolish [_ obj]
;; invalidate cache if the global/main system has been stopped
(reset! cache {})))
@cache
(let [factory (registry key)]
(reify p/Factory
(dependencies [_]
(p/dependencies factory))
(build [_ deps]
(let [obj (p/build factory deps)]

;; тут идея в том, что если в локальной системе что-то переопределить,
;; то все, что от этого зависит - инвалидируется

(swap! cache assoc [key deps] obj)
(vswap! cache assoc
[:key+deps key deps] obj
#_#_
[:key+obj key obj] true)
obj))
(demolish [_ obj]
(p/demolish factory obj)))))))))

;; может быть сделать 2 арности вместо этих 2х функций?

(defn use-cache [cache]
(fn [registry]
(fn [key]
Expand All @@ -800,7 +790,7 @@
(dependencies [_]
(p/dependencies factory))
(build [_ deps]
(?? (get @cache [key deps])
(?? (get cache [:key+deps key deps])
(p/build factory deps)))
(demolish [_ obj]
;; todo
Expand All @@ -813,4 +803,9 @@
;; (swap! cache update :direct assoc [key deps] obj)
;; (swap! cache update :indirect conj [key obj]) ;; #{set}

;; или можно каждой фабрике сделать локальный (volatile! cached?)
;; и спрашивать у него
;; но это логику di ломает


#_"we must stop only not cached obj"))))))
42 changes: 21 additions & 21 deletions test/darkleaf/di/tutorial/z_memoize_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

(defn connection
{::di/stop #(reset! % :stopped)}
[]
[{url "CONNECTION_URL"}]
(atom :started))

(defn migrations
Expand All @@ -24,34 +24,34 @@

;; todo: check registry placement

(t/deftest ok
(let [[global-system cache :as root]
(di/start [`root ::di/cache]
{"CONNECTION_URL" "1"}
(di/collect-cache))]

;; todo: test a cache reset after the global system stop

(t/deftest ok
(let [[global-system cache] (di/start [`root ::di/cache]
(di/collect-cache))]
;; cache is not printable O_o
(prn cache)

(with-open [local (di/start `root
(di/use-cache cache))]
(t/is (identical? global-system @local)))))
(t/is (identical? global-system @local)))

#_
;; or should we use external atom to also cache in local system?
(t/deftest ok
(let [cache (atom {}) ;; (di/make-cache)
global-system (di/start `root
(di/collect-cache cache))]
(with-open [local (di/start `root
(di/use-cache cache)
{"CONNECTION_URL" "1"})]
(t/is (identical? global-system @local)))

(with-open [local (di/start `root
;; `use-cache` should be the first registry
(di/use-cache cache)
(di/collect-cache cache))]
(t/is (identical? @global-system @local)))))
#_
(t/deftest ok
(let [cache (atom {}) ;; (di/make-cache)
global-system (di/start `root
(di/use-cache cache))]
{"CONNECTION_URL" "2"})]
(t/is (not (identical? global-system @local))))


#_#_
(di/stop root)

(with-open [local (di/start `root
(di/use-cache cache))]
(t/is (identical? @global-system @local)))))
(t/is (identical? global-system @local)))))

0 comments on commit a57733f

Please sign in to comment.