This package adds a defmemoized
macro to Elixir. This is used to define a function that should be memoized - this means that after the first time it is called with a particular set of arguments, the result is stored and never recomputed.
The cache is local to the current process. Note that there are no expiration strategies or ways to remove entries from the cache; the cache will exist for the lifetime of the process. That makes this useful for shortlived processes like Phoenix Liveviews.
- Don't use memoization prematurely; there are often better solutions for optimization.
- Used in the wrong place, memoization can decrease performance!
- Careful not to memoize too much stuff or you could run out of memory.
- This uses the
process dictionary
which might make you feel icky.
If available in Hex, the package can be installed
by adding memoized
to your list of dependencies in mix.exs
:
def deps do
[
{:memoized, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/memoized.