PitchHut logo
MemoTTL
by neat_scarlet_elle
A thread-safe memoization utility for Ruby with TTL and LRU support.
Pitch

MemoTTL simplifies memory management in Ruby applications by providing a thread-safe memoization utility that allows values to expire based on a time-to-live (TTL) policy while also enforcing a least recently used (LRU) eviction strategy. This ensures optimal performance and memory efficiency in applications that rely heavily on cached computations.

Description

MemoTTL is a robust thread-safe memoization utility for Ruby, designed to enhance performance through efficient caching mechanisms. This utility boasts support for both Time-To-Live (TTL) and Least Recently Used (LRU) eviction strategies, making it ideal for scenarios requiring expired cached values and optimized memory usage.

Key Features

  • TTL Support: Memoize method results with configurable expiration times to ensure that outdated data does not persist beyond its usefulness.
  • LRU Eviction: Automatically limit memory consumption by evicting the least recently used cached items, maintaining efficiency in resource allocation.
  • Thread Safety: Engineered with Monitor for thread-safe operations, ensuring reliability in multi-threaded environments.
  • Seamless Integration: Easily incorporate MemoTTL into existing Ruby classes using include MemoTTL for straightforward application.

Example Usage

Here is a simple example of how to use MemoTTL within a Ruby class:

require "memo_ttl"

class Calculator
  include MemoTTL

  def a_method_that_does_something(x)
    sleep(2) # simulate a slow process
    x * 2
  end

  # Correct placement excusing Ruby's top-down evaluation 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 manage cached results, you can clear the memoized methods as shown below:

calc.clear_memoized_method(:a_method_that_does_something)
calc.clear_all_memoized_methods
calc.cleanup_memoized_methods

Integration with Rails

MemoTTL can be effectively utilized within Ruby on Rails applications as demonstrated in the following example:

require 'memo_ttl'

class TestController < ApplicationController
  include MemoTTL

  def index
    result1 = test_method(1, 2)
    result4 = test_method(1, 2)
    result6 = test_method(3, 4)

    render plain: <<~TEXT
      Result 1: #{result1}
      Result 2: #{result2}
      Result 3: #{result3}
      Result 4: #{result4}
      Result 5: #{result5}
      Result 6: #{result6}
    TEXT
  end

  def test_method(x, y)
    puts "Calling test_method(#{x}, #{y})"
    x + y
  end

  def clean_up
    clear_memoized_method(:test_method)
    clear_all_memoized_methods
    cleanup_memoized_methods
  end

  memoize :test_method, ttl: 10, max_size: 10
end

When the TestController receives a request, results for previously computed values are returned instantly from cache, improving response times significantly.

0 comments

No comments yet.

Sign in to be the first to comment.