Module: SafeMemoize::ClassMethods

Defined in:
lib/safe_memoize/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#memoize(method_name, ttl: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/safe_memoize/class_methods.rb', line 5

def memoize(method_name, ttl: nil)
  method_name = method_name.to_sym
  visibility = memoized_method_visibility(method_name)

  ttl = if ttl.nil?
    nil
  else
    ttl = Float(ttl)
    raise ArgumentError, "ttl must be non-negative" if ttl < 0

    ttl
  end

  expires_at = ttl && Process.clock_gettime(Process::CLOCK_MONOTONIC) + ttl

  mod = Module.new do
    define_method(method_name) do |*args, **kwargs, &block|
      # Blocks bypass cache entirely — they aren't comparable
      return super(*args, **kwargs, &block) if block

      cache_key = compute_cache_key(method_name, args, kwargs)

      # Fast path: check without lock
      if (record = memo_cache_record(cache_key))
        record_cache_hit(method_name, args)
        return memo_record_value(record)
      end

      # Cache miss - compute and store
      start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      result = memo_fetch_or_store(cache_key, expires_at: expires_at) { super(*args, **kwargs) }
      elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time

      with_memo_lock do
        record_cache_miss(method_name, args, elapsed_time)
      end

      result
    end

    send(visibility, method_name)
  end

  prepend mod
end