On inspecting the source for memoize, it would actually seem (to my eyes, someone please correct me if I'm mistaken) to be using a similar approach to what has been suggested earlier in the discussion, with the risk of re-computing values at concurrent invocations with the same arguments. So using memoize in this case wouldn't actually provide any benefit - should've double checked before posting, sorry!
Den måndagen den 1:e september 2014 kl. 11:16:00 UTC+2 skrev Colin Fleming: > > Hi Marcus, > > That's an interesting approach. My first reaction was that the update is > still not atomic, since you have: > > (when-not (contains? @cache k) > (swap! cache assoc k (calc-value* k))) > > Which I thought could cause different clients to see different values > depending on the race condition there. But since in theory calc-value* > should always return the same value for the same key, at worst you'll just > end up swapping the same value in unnecessarily. I'd need to think a bit > about the implications of using a memoised function in an atom update to > convince myself, but it's a neat idea - thanks! > > Cheers, > Colin > > > > On 31 August 2014 04:25, Marcus Magnusson <mar...@gmail.com <javascript:>> > wrote: > >> How about something like this? You can still keep your cache as an atom >> that you can pass around, and you avoid unnecessary recomputation through >> memoize: >> >> (def cache (atom {})) >> >> (def lookup >> (let [calc-value* (memoize calc-value)] >> (fn [cache k] >> (when-not (contains? @cache k) >> (swap! cache assoc k (calc-value* k))) >> (@cache k)))) >> >> Den lördagen den 30:e augusti 2014 kl. 12:11:58 UTC+2 skrev Colin Fleming: >>> >>> In my case I can't use memoize because I need to supply the cache map - >>> that in turn is stored on another object so it can be invalidated by events >>> outside my control. >>> >>> >>> On 30 August 2014 20:00, Ray Miller <r...@1729.org.uk> wrote: >>> >>>> On 30 August 2014 06:26, Colin Fleming <colin.ma...@gmail.com> wrote: >>>> > >>>> > I want to use a map to cache values based on a key. I'm planning to >>>> use an >>>> > atom for this. My basic operation is "give me the value for this key" >>>> - if >>>> > the value exists in the map then that value should be returned, >>>> otherwise a >>>> > new value should be calculated, inserted in the map and then >>>> returned. My >>>> > plan is to implement something like the following: >>>> > >>>> > >>>> > (defn ensure [cache key] >>>> > (if (contains? cache key) >>>> > cache >>>> > (assoc cache key (calc-value key)))) >>>> > >>>> > (let [value (get (swap! cache ensure key) key)] >>>> > ... do my thing with value ...) >>>> >>>> Why not just use memoize? >>>> >>>> (def calc-value (memoize (fn [key] ...)) >>>> >>>> If you need more control over the cache, check out core.memoize (which >>>> builds on top of core.cache). >>>> >>>> Ray. >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Clojure" group. >>>> To post to this group, send email to clo...@googlegroups.com >>>> >>>> Note that posts from new members are moderated - please be patient with >>>> your first post. >>>> To unsubscribe from this group, send email to >>>> clojure+u...@googlegroups.com >>>> >>>> For more options, visit this group at >>>> http://groups.google.com/group/clojure?hl=en >>>> --- >>>> You received this message because you are subscribed to the Google >>>> Groups "Clojure" group. >>>> To unsubscribe from this group and stop receiving emails from it, send >>>> an email to clojure+u...@googlegroups.com. >>>> >>>> For more options, visit https://groups.google.com/d/optout. >>>> >>> >>> -- >> You received this message because you are subscribed to the Google >> Groups "Clojure" group. >> To post to this group, send email to clo...@googlegroups.com >> <javascript:> >> Note that posts from new members are moderated - please be patient with >> your first post. >> To unsubscribe from this group, send email to >> clojure+u...@googlegroups.com <javascript:> >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> --- >> You received this message because you are subscribed to the Google Groups >> "Clojure" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to clojure+u...@googlegroups.com <javascript:>. >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.