diff --git a/src/darkleaf/di/core.clj b/src/darkleaf/di/core.clj index e18ea180..66363616 100644 --- a/src/darkleaf/di/core.clj +++ b/src/darkleaf/di/core.clj @@ -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] @@ -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 @@ -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")))))) diff --git a/test/darkleaf/di/tutorial/z_memoize_test.clj b/test/darkleaf/di/tutorial/z_memoize_test.clj index e71dd9f7..3dc70c1a 100644 --- a/test/darkleaf/di/tutorial/z_memoize_test.clj +++ b/test/darkleaf/di/tutorial/z_memoize_test.clj @@ -5,7 +5,7 @@ (defn connection {::di/stop #(reset! % :stopped)} - [] + [{url "CONNECTION_URL"}] (atom :started)) (defn migrations @@ -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)))))