{"id":28092394,"url":"https://github.com/mishalzaman/memo_ttl","last_synced_at":"2025-05-13T13:10:05.824Z","repository":{"id":289113605,"uuid":"970140458","full_name":"mishalzaman/memo_ttl","owner":"mishalzaman","description":"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.","archived":false,"fork":false,"pushed_at":"2025-04-23T03:02:06.000Z","size":51,"stargazers_count":31,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-10T09:33:00.828Z","etag":null,"topics":["gem","lru","memoize","ruby","ttl"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/memo_ttl","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mishalzaman.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-21T14:40:55.000Z","updated_at":"2025-05-06T18:21:09.000Z","dependencies_parsed_at":"2025-04-21T16:46:56.200Z","dependency_job_id":null,"html_url":"https://github.com/mishalzaman/memo_ttl","commit_stats":null,"previous_names":["mishalzaman/memo_ttl"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mishalzaman%2Fmemo_ttl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mishalzaman%2Fmemo_ttl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mishalzaman%2Fmemo_ttl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mishalzaman%2Fmemo_ttl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mishalzaman","download_url":"https://codeload.github.com/mishalzaman/memo_ttl/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253948439,"owners_count":21988957,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gem","lru","memoize","ruby","ttl"],"created_at":"2025-05-13T13:09:56.728Z","updated_at":"2025-05-13T13:10:05.809Z","avatar_url":"https://github.com/mishalzaman.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Gem Version](https://badge.fury.io/rb/memo_ttl.svg)](https://rubygems.org/gems/memo_ttl)\n\n# MemoTTL\n\n**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.\n\n## Features\n\n- Memoize method results with expiration (TTL)\n- Built-in LRU eviction to limit memory usage\n- Thread-safe with Monitor\n- Easy integration via `include MemoTTL`\n\n## When to Use\n\nUse `memo_ttl` when:\n\n- You're calling a **pure method** multiple times with the same arguments\n- The method is **expensive** (I/O, DB, parsing, computation)\n- You want **in-memory** caching without Redis or external dependencies\n- You need **per-object isolation** — not global cache key management\n- You want a cache that’s **automatically invalidated** via TTL and LRU\n\nAvoid using it when:\n\n- The method is already fast (e.g., simple arithmetic, inline logic)\n- You're calling it with **unique arguments** every time\n- You need cross-request or cross-process persistence (use `Rails.cache`)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem \"memo_ttl\"\n```\n\nAfterwards:\n\n```ruby\nbundle install\n```\n\n## Usage\n\n```ruby\nrequire \"memo_ttl\"\n\nclass Calculator\n  include MemoTTL\n\n  def a_method_that_does_something(x)\n    sleep(2) # simulate slow process\n    x * 2\n  end\n\n  # use at the bottom due to Ruby's top-down evalation of methods\n  memoize :a_method_that_does_something, ttl: 60, max_size: 100\nend\n\ncalc = Calculator.new\ncalc.a_method_that_does_something(5) # takes 2 seconds\ncalc.a_method_that_does_something(5) # returns instantly from cache\n```\n\nTo clear the cache:\n\n```ruby\ncalc.clear_memoized_method(:a_method_that_does_something)\ncalc.clear_all_memoized_methods\ncalc.cleanup_memoized_methods\n```\n\n### Rails Example\n\n```ruby\nrequire 'memo_ttl'\n\nclass TestController \u003c ApplicationController\n  include MemoTTL\n\n  def index\n    result1 = test_method(1, 2)\n    result2 = test_method(1, 2)\n    result3 = test_method(5, 2)\n    result4 = test_method(1, 2)\n    result5 = test_method(1, 2)\n    result6 = test_method(3, 4)\n\n    render plain: \u003c\u003c~TEXT\n      Result 1: #{result1}\n      Result 2: #{result2}\n      Result 3: #{result3}\n      Result 4: #{result4}\n      Result 5: #{result5}\n      Result 6: #{result6}\n    TEXT\n  end\n\n  def test_method(x, y)\n    puts \"Calling test_method(#{x}, #{y})\"\n    x + y\n  end\n\n  def clean_up\n    clear_memoized_method(:test_method)\n    clear_all_memoized_methods\n    cleanup_memoized_methods\n  end\n\n  memoize :test_method, ttl: 10, max_size: 10\nend\n```\n\nOutput in Rails console:\n\n```\nProcessing by TestController#index as HTML\nCalling test_method(1, 2)\nCalling test_method(5, 2)\nCalling test_method(3, 4)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmishalzaman%2Fmemo_ttl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmishalzaman%2Fmemo_ttl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmishalzaman%2Fmemo_ttl/lists"}