I have a - very simple - memoizer that uses Weak/Soft/Hard Hash tables from the JDK or from google collections. (Google collections has different strategies for expiration of cache. Soft is the last time since used strategy. Weak is a good strategy for object for which identity is equality.)
It is clearly less nice than your solution from a programming point of view and it is a less good excercise in concurrency and style but it has an advantage : it put a lot of work in the hands of someone else (google or oracle), which is always good. And using a mutable atom to a map allows less concurrent access than a concurrent hash map, as you don't need to synchronize access to different cells. If some people are interested, I could think of how to make it nice and usable (it is currently a few lines of ugly code somewhere in a project) and upload it to clojars. Just tell me if someone is interested and I will be happy to make it nice, Best, Nicolas. On Sat, Jul 3, 2010 at 10:21 AM, Saul Hazledine <shaz...@gmail.com> wrote: > cache-dot-clj Clojure library that caches the results of impure > functions. > > It is almost entirely based on the memoize functions described here: > http://kotka.de/blog/2010/03/memoize_done_right.html > > I have found this useful for caching the results of database calls and > for holding HTML snippets. > > This library is available at clojars.org for use with Leiningen/Maven. > > Github page: > http://github.com/alienscience/cache-dot-clj > > ==== Example Usage ===== > > (ns an-example > (:use cache-dot-clj.cache)) > > (defn get-user-from-db > "Gets a user details from a database." > [username] > ;; Slow database read goes here > ) > > ;; Cache the last 1000 users read in > ;; i.e support serving a 1000 concurrent users from memory > (def get-user-from-db > (cached get-user-from-db (lru-cache-strategy 1000))) > > ;; First read of the user is slow > (get-user-from-db "fred") > > ;; Second is fast > (get-user-from-db "fred") > > ;; When fred's details are changed invalidate the cache > (invalidate-cache get-user-from-db "fred") > > ;; The next call will read from the db and cache the result again > (get-user-from-db "fred") > > -- > 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<clojure%2bunsubscr...@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 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