Also worth mentioning is that the implementation is many-thread friendly,
so using a cache:
1) won't be a single thread bottleneck (as in the case of using an
unadorned agent)
2) work is done on the calling threads

Granted, there's a race condition where two threads trying to find the same
thing at the exact same time will result in one thread doing the work and
the other thread waiting (and therefore you don't know which thread would
be doing the work) but this should be exceedingly rare and the work-or-wait
is happening where it should be anyway.

A previous version of the function had the work being done within the agent
which would have killed parallelized I/O for me (in a separate project).


On Thu, Jun 11, 2015 at 9:32 PM, Ryan Waters <ryan.or...@gmail.com> wrote:

> https://gist.github.com/rwat/4abcebcb4cfae956f382
>
> I've enjoyed using clojure.core.cache for caching results from time
> expensive operations involving I/O, etc. and feel like this is the sort of
> function that makes c.c.cache as simple to use as possible.  e.g. all it
> becomes is:
>
> (cache-interact c f k xs)
>
> where c is the cache (agent), f is the function that generates the value
> you're looking for if it isn't already cached, k is the key to search for
> and xs is optional extra arguments that are apply'ed to f.
>
> I could have used an agent or atom to store the cache but I don't know how
> much is 'too much' to perform within an atom reset!/swap!.  The operations
> performed within the critical section are all fast but there's a handful of
> them so I decided to err on the side of using an agent.
>
> Also, I apologize for some of the symbol names.  I don't make it a habit
> to name things in that style.
>
> I would appreciate any feedback / recommendations for improvement.
>
> Thank you,
> Ryan
>

-- 
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.

Reply via email to