clojure.core/memoize¶
Returns a memoized version of a referentially transparent function. The memoized version of the function keeps a cache of the mapping from arguments to results and, when calls with the same arguments are repeated often, has higher performance at the expense of higher memory use.
Examples¶
Note by rquinlivan¶
What is the definition of "often" for the purposes of caching? Is there a util like this that allows for different cache eviction strategies?
Note by PetrGlad¶
@rquinlivan The implementation of clojure.core/memoize is plain straightforward. It should be obvious that computation_cost times number_of_calls should determine whether to use it or not. If you need more control over caching have a look at clojure.core.cache
Note by l3nz¶
memoize is nice but it may be tricky to use in production, because it just caches everything. Also, the key under which the value is stored are all the parametres you have as input.
If you need something more flexible, with user-definable keys (eg because you are passing in a database connection) or a flexible eviction policy, see clojure.core.memoize that is a wrapper around clojure.core.cache.
See also¶
Content from the matching ClojureDocs page, with authors credited on each contribution.