Module: SafeMemoize

Includes:
InstanceMethods
Defined in:
lib/safe_memoize.rb,
lib/safe_memoize/rails.rb,
lib/safe_memoize/version.rb,
lib/safe_memoize/lru_methods.rb,
lib/safe_memoize/class_methods.rb,
lib/safe_memoize/configuration.rb,
lib/safe_memoize/hooks_methods.rb,
lib/safe_memoize/public_methods.rb,
lib/safe_memoize/adapters/statsd.rb,
lib/safe_memoize/release_tooling.rb,
lib/safe_memoize/instance_methods.rb,
lib/safe_memoize/rails/middleware.rb,
lib/safe_memoize/custom_key_methods.rb,
lib/safe_memoize/inspection_methods.rb,
lib/safe_memoize/cache_store_methods.rb,
lib/safe_memoize/cache_record_methods.rb,
lib/safe_memoize/rails/request_scoped.rb,
lib/safe_memoize/cache_metrics_methods.rb,
lib/safe_memoize/adapters/opentelemetry.rb,
lib/safe_memoize/public_metrics_methods.rb,
lib/safe_memoize/public_custom_key_methods.rb

Overview

Thread-safe memoization for Ruby that correctly handles +nil+ and +false+ values.

Prepend this module into any class, then call ClassMethods#memoize to wrap instance methods with a per-instance cache backed by a +Mutex+.

Examples:

Basic usage

class UserService
  prepend SafeMemoize

  def current_user
    User.find_by(session_id: session_id)
  end
  memoize :current_user
end

With TTL and LRU cap

class ApiClient
  prepend SafeMemoize

  def fetch(id)
    http_get("/items/#{id}")
  end
  memoize :fetch, ttl: 60, max_size: 500
end

See Also:

Defined Under Namespace

Modules: Adapters, CacheMetricsMethods, CacheRecordMethods, CacheStoreMethods, ClassMethods, CustomKeyMethods, HooksMethods, InspectionMethods, InstanceMethods, LruMethods, PublicCustomKeyMethods, PublicMethods, PublicMetricsMethods, Rails, ReleaseTooling Classes: Configuration, Error

Constant Summary collapse

VERSION =

The current gem version string.

"1.0.0"

Constants included from HooksMethods

HooksMethods::NOTIFICATION_EVENT_NAMES

Class Method Summary collapse

Methods included from PublicCustomKeyMethods

#clear_custom_keys, #memoize_with_custom_key

Methods included from PublicMetricsMethods

#cache_hit_rate, #cache_metrics_reset, #cache_miss_rate, #cache_stats, #cache_stats_for

Methods included from PublicMethods

#clear_memo_hooks, #dump_memo, #load_memo, #memo_age, #memo_count, #memo_inspect, #memo_keys, #memo_preload, #memo_refresh, #memo_stale?, #memo_touch, #memo_ttl_remaining, #memo_values, #memoized?, #on_memo_evict, #on_memo_expire, #on_memo_hit, #on_memo_miss, #on_memo_store, #reset_all_memos, #reset_memo, #warm_memo

Class Method Details

.configurationConfiguration

Returns the global Configuration instance, creating it on first access.

Returns:



76
77
78
# File 'lib/safe_memoize.rb', line 76

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ void

This method returns an undefined value.

Yields the global Configuration object for mutation.

Examples:

SafeMemoize.configure do |c|
  c.default_ttl = 300
end

Yields:

Yield Parameters:



69
70
71
# File 'lib/safe_memoize.rb', line 69

def self.configure
  yield configuration
end

.deprecate(subject, message:, horizon:) ⇒ void

This method returns an undefined value.

Emits a structured deprecation warning through the configured handler.

Parameters:

  • subject (String)

    short identifier of the deprecated symbol

  • message (String)

    migration instructions

  • horizon (String)

    version when the symbol will be removed (e.g. +"v2.0.0"+)



95
96
97
98
99
# File 'lib/safe_memoize.rb', line 95

def self.deprecate(subject, message:, horizon:)
  text = "[SafeMemoize] #{subject} is deprecated and will be removed in #{horizon}. #{message}"
  handler = configuration.on_deprecation
  handler ? handler.call(text) : warn(text)
end

.prepended(base) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



55
56
57
# File 'lib/safe_memoize.rb', line 55

def self.prepended(base)
  base.extend(ClassMethods)
end

.reset_configuration!Configuration

Resets the global configuration to all defaults.

Useful in test suites to prevent configuration leaking between examples.

Returns:



85
86
87
# File 'lib/safe_memoize.rb', line 85

def self.reset_configuration!
  @configuration = Configuration.new
end