Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Elixir
  3. memoize

memoize

MITElixir

A memoization macro for Elixir that caches function results using CAS on ETS for performance optimization.

Visit WebsiteGitHubGitHub
204 stars14 forks0 contributors

What is memoize?

Memoize is an Elixir library that provides a `defmemo` macro to easily add memoization to functions, caching the results of expensive calls to avoid redundant computations and speed up programs. It leverages Erlang's ETS with CAS operations for thread-safe, efficient caching and offers features like exclusive execution to prevent parallel calls and flexible cache invalidation.

Target Audience

Elixir developers working on applications with computationally expensive or frequently called functions where caching results can significantly improve performance, such as those implementing recursive algorithms, external API calls, or data transformation pipelines.

Value Proposition

Developers choose Memoize for its simplicity—replacing `def` with `defmemo` after `use Memoize`—and its robust features like exclusive execution to ensure correctness, configurable cache strategies including expiration and eviction, and the ability to implement custom caching behaviors via the `Memoize.CacheStrategy` protocol.

Overview

A method caching macro for elixir using CAS on ETS.

Use Cases

Best For

  • Optimizing recursive functions like Fibonacci calculations by caching intermediate results.
  • Caching expensive external API calls to reduce latency and avoid rate limits.
  • Memoizing configuration or data loading functions in long-running Elixir applications to improve startup or response times.
  • Implementing custom cache strategies with specific eviction or expiration logic via the `Memoize.CacheStrategy` behaviour.
  • Ensuring thread-safe caching in concurrent Elixir applications with exclusive execution to prevent duplicate work.
  • Partially caching function arguments for advanced use cases using `Memoize.Cache.get_or_run/2`.

Not Ideal For

  • Applications requiring distributed caching across multiple Erlang/OTP nodes, as Memoize relies on ETS which is local to a single VM.
  • Real-time systems with frequent cache invalidations, since invalidation functions have linear complexity and can become slow with large cache sizes.
  • Projects needing ultra-lightweight caching without macro overhead, where simpler key-value stores or manual caching might be more performant.

Pros & Cons

Pros

Simple Integration

Just replace `def` with `defmemo` after `use Memoize`, as shown in the Fibonacci example, making it effortless to add caching to existing functions.

Exclusive Execution

Ensures memoized functions are never called in parallel, preventing duplicate work and race conditions, demonstrated with the `Calc.calc/0` example in the README.

Flexible Cache Strategies

Offers multiple strategies like Default and Eviction, with options for expiration and permanent caching, plus the ability to implement custom strategies via the `Memoize.CacheStrategy` behaviour.

Partial Argument Caching

Supports caching with partial arguments using `Memoize.Cache.get_or_run/2`, enabling advanced use cases beyond standard function memoization.

Cons

Slow Invalidation

The `invalidate/{0-2}` functions have linear complexity, meaning they can degrade performance when invalidating many cached values, as explicitly warned in the README.

Complex Configuration

Advanced features like the Eviction strategy require careful tuning of thresholds and options, which can be error-prone and add overhead for simple caching needs.

Frequently Asked Questions

Quick Stats

Stars204
Forks14
Contributors0
Open Issues3
Last commit5 months ago
CreatedSince 2017

Tags

#macro#elixir-lang#elixir#caching#performance-optimization#memoization#cas#erlang-otp#ets#cache

Built With

E
ETS
E
Elixir
E
Erlang/OTP

Links & Resources

Website

Included in

Elixir13.1k
Auto-fetched 11 hours ago

Related Projects

cachexcachex

A powerful caching library for Elixir with support for transactions, fallbacks and expirations

Stars1,745
Forks122
Last commit6 months ago
nebulexnebulex

In-memory and distributed caching toolkit for Elixir.

Stars1,398
Forks79
Last commit18 hours ago
con_cachecon_cache

ets based key/value cache with row level isolated writes and ttl support

Stars927
Forks73
Last commit11 months ago
stashstash

A small and user-friendly ETS wrapper for caching in Elixir

Stars52
Forks9
Last commit8 months ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub