Show HN: I built a Ruby gem that handles memoization with a ttl
Published on: 2025-08-15 16:51:24
MemoTTL
MemoTTL is a thread-safe memoization utility for Ruby that supports TTL (Time-To-Live) and LRU (Least Recently Used) eviction. It's designed for scenarios where memoized values should expire after a period and memory usage must be constrained.
Features
Memoize method results with expiration (TTL)
Built-in LRU eviction to limit memory usage
Thread-safe with Monitor
Easy integration via include MemoTTL
Installation
Add this line to your application's Gemfile:
gem "memo_ttl"
Afterwards:
bundle install
Usage
require "memo_ttl" class Calculator include MemoTTL def a_method_that_does_something ( x ) sleep ( 2 ) # simulate slow process x * 2 end # use at the bottom due to Ruby's top-down evalation of methods memoize :a_method_that_does_something , ttl : 60 , max_size : 100 end calc = Calculator . new calc . a_method_that_does_something ( 5 ) # takes 2 seconds calc . a_method_that_does_something ( 5 ) # returns instantly from cache
To clear the cache:
calc . clear_memoiz
... Read full article.