core.cache

through method should always have cache as first argument to make it more friendly for swap!

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

Given that "through" returns a new cache (like the other methods), it is quite likely that people will store the cache in an atom. Currently, code using through looks like this:

(defn load-secondary-index-segment [[filename offset length :as segment]]
(swap! segment-cache #(cache/through fetch-secondary-index-segment % segment))
(cache/lookup @segment-cache segment))

If the cache argument were always first, it would work nicely with swap, ->, etc, be more consistent with conj, assoc, etc, and you'd have this instead:

(defn load-secondary-index-segment [[filename offset length :as segment]]
(swap! segment-cache cache/through fetch-secondary-index-segment segment)
(cache/lookup @segment-cache segment))

Activity

Hide
Morgon Kanter added a comment - - edited

I signed up to Jira to report this very issue, only to find it already reported. I've had to use some particularly ugly code to work around this API decision. Example, I have a cache atom "user-cache" that I need to either hit or add to based on an action: (swap! user-cache #(cache/through (constantly value) % key))

I'd like to go further than swapping the order of arguments though. Rather than do that (and break backwards compatibility), how about a new function that takes a value instead of a value-fn? That way the (constantly value) can just become value instead.

Show
Morgon Kanter added a comment - - edited I signed up to Jira to report this very issue, only to find it already reported. I've had to use some particularly ugly code to work around this API decision. Example, I have a cache atom "user-cache" that I need to either hit or add to based on an action: (swap! user-cache #(cache/through (constantly value) % key)) I'd like to go further than swapping the order of arguments though. Rather than do that (and break backwards compatibility), how about a new function that takes a value instead of a value-fn? That way the (constantly value) can just become value instead.
Hide
Sean Corfield added a comment -

I'm adding a new function through-cache for this.

Show
Sean Corfield added a comment - I'm adding a new function through-cache for this.
Hide
Sean Corfield added a comment -

Will be in the next release.

Show
Sean Corfield added a comment - Will be in the next release.
Hide
Sean Corfield added a comment -

Release 0.7.0

Show
Sean Corfield added a comment - Release 0.7.0

People

Vote (1)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: