Skip to content

Commit

Permalink
Cache interface
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf committed Sep 12, 2019
1 parent b6d0420 commit 431b103
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/methodical/impl/cache/simple.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
'(simple-cache))

Cache
(cached-method [_ dispatch-value]
(cachedMethod [_ dispatch-value]
(get @atomm dispatch-value))

(cache-method! [_ dispatch-value method]
(cacheMethodBang [_ dispatch-value method]
(swap! atomm assoc dispatch-value method))

(clear-cache! [this]
(clearCacheBang [this]
(reset! atomm {})
this)

(empty-copy [this]
(emptyCopy [this]
(SimpleCache. (atom {}))))
14 changes: 7 additions & 7 deletions src/methodical/impl/cache/watching.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@
(remove-watches this))

Cache
(cached-method [_ dispatch-value]
(.cached-method cache dispatch-value))
(cachedMethod [_ dispatch-value]
(.cachedMethod cache dispatch-value))

(cache-method! [this dispatch-value method]
(.cache-method! cache dispatch-value method)
(cacheMethodBang [this dispatch-value method]
(.cacheMethodBang cache dispatch-value method)
this)

(clear-cache! [this]
(.clear-cache! cache)
(clearCacheBang [this]
(.clearCacheBang cache)
this)

(empty-copy [this]
(emptyCopy [this]
(add-watches (i/empty-copy cache) refs)))

(defn- cache-watch-fn [cache]
Expand Down
2 changes: 1 addition & 1 deletion src/methodical/impl/multifn/cached.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

(effectiveMethod [_ dispatch-value]
(or
(.cached-method cache dispatch-value)
(.cachedMethod cache dispatch-value)
(let [method (i/effective-method impl dispatch-value)]
(i/cache-method! cache dispatch-value method)
method))))
37 changes: 25 additions & 12 deletions src/methodical/interface.clj
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,28 @@
[^MultiFnImpl multifn dispatch-value]
(.effectiveMethod multifn dispatch-value))

(p.types/definterface+ Cache
(cached-method [cache dispatch-value]
"Return cached effective method for `dispatch-value`, if it exists in the cache.")

(cache-method! [cache dispatch-value method]
"Cache the effective method for `dispatch-value` in this cache.")

(clear-cache! [cache]
"Empty the contents of the cache in-place.")

(^methodical.interface.Cache empty-copy [cache]
"Return an empty copy of the same type as this cache, e.g. for use when copying a multifn."))
(definterface Cache
(cachedMethod [dispatch-value])
(cacheMethodBang [dispatch-value method])
(clearCacheBang [])
(^methodical.interface.Cache emptyCopy []))

(defn cached-method
"Return cached effective method for `dispatch-value`, if it exists in the cache."
[^Cache cache dispatch-value]
(.cachedMethod cache dispatch-value))

(defn cache-method!
"Cache the effective method for `dispatch-value` in this cache."
[^Cache cache dispatch-value method]
(.cacheMethodBang cache dispatch-value method))

(defn clear-cache!
"Empty the contents of the cache in-place."
[^Cache cache]
(.clearCacheBang cache))

(defn ^methodical.interface.Cache empty-copy
"Return an empty copy of the same type as this cache, e.g. for use when copying a multifn."
[^Cache cache]
(.emptyCopy cache))
2 changes: 1 addition & 1 deletion test/methodical/impl/cache/watching_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[& [num-times-cleared]]
(reify
Cache
(clear-cache! [_]
(clearCacheBang [_]
(some-> num-times-cleared (swap! inc)))

PrettyPrintable
Expand Down

0 comments on commit 431b103

Please sign in to comment.