The project is in a healthy, maintained state
Memoize methods with a simple decorator. Supports TTL expiration, LRU eviction, thread-safe per-instance caches, and proper nil/false handling.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

philiprehberger-memo

Tests Gem Version License

Practical memoization — memo decorator with TTL, LRU eviction, thread-safety, and proper nil/false handling.

Requirements

  • Ruby >= 3.1

Installation

Add to your Gemfile:

gem "philiprehberger-memo"

Then run:

bundle install

Or install directly:

gem install philiprehberger-memo

Usage

require "philiprehberger/memo"

class UserService
  include Philiprehberger::Memo

  def find(id)
    User.find(id)
  end
  memo :find

  def expensive_query(filters)
    # ...
  end
  memo :expensive_query, ttl: 300

  def config
    # ...
  end
  memo :config, max_size: 100
end

service = UserService.new
service.find(1)           # executes query
service.find(1)           # returns cached result
service.clear_memo(:find) # manual invalidation
service.clear_all_memos   # clear everything

Features

  • Per-instance caching (not class-level)
  • Handles nil and false return values correctly
  • Optional TTL (time-based expiration)
  • Optional max_size with LRU eviction
  • Thread-safe with per-instance mutex
  • Works with positional and keyword arguments

API

Method Description
memo :method_name, ttl: nil, max_size: nil Memoize a method (class-level)
#clear_memo(:method_name) Clear cache for one method
#clear_all_memos Clear all memoized caches

Development

bundle install
bundle exec rspec      # Run tests
bundle exec rubocop    # Check code style

License

MIT