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/stores/base.rb,
lib/safe_memoize/stores/redis.rb,
lib/safe_memoize/class_methods.rb,
lib/safe_memoize/configuration.rb,
lib/safe_memoize/hooks_methods.rb,
lib/safe_memoize/stores/memory.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/stores/rails_cache.rb,
lib/safe_memoize/cache_store_methods.rb,
lib/safe_memoize/fiber_local_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/ractor_shared_methods.rb,
lib/safe_memoize/adapters/opentelemetry.rb,
lib/safe_memoize/public_metrics_methods.rb,
lib/safe_memoize/adapters/concurrent_ruby.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+.
Defined Under Namespace
Modules: Adapters, CacheMetricsMethods, CacheRecordMethods, CacheStoreMethods, ClassMethods, CustomKeyMethods, FiberLocalMethods, HooksMethods, InspectionMethods, InstanceMethods, LruMethods, PublicCustomKeyMethods, PublicMethods, PublicMetricsMethods, RactorSharedMethods, Rails, ReleaseTooling, Stores Classes: Configuration, Error
Constant Summary collapse
- VERSION =
The current gem version string.
"1.2.0"
Constants included from FiberLocalMethods
FiberLocalMethods::FIBER_STORE_KEY
Constants included from HooksMethods
HooksMethods::NOTIFICATION_EVENT_NAMES
Class Method Summary collapse
-
.configuration ⇒ Configuration
Returns the global Configuration instance, creating it on first access.
-
.configure {|config| ... } ⇒ void
Yields the global Configuration object for mutation.
-
.deprecate(subject, message:, horizon:) ⇒ void
Emits a structured deprecation warning through the configured handler.
- .prepended(base) ⇒ Object private
-
.reset_configuration! ⇒ Configuration
Resets the global configuration to all defaults.
Methods included from FiberLocalMethods
#fiber_local_memoized?, #reset_all_fiber_memos, #reset_fiber_memo
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
.configuration ⇒ Configuration
Returns the global Configuration instance, creating it on first access.
81 82 83 |
# File 'lib/safe_memoize.rb', line 81 def self.configuration @configuration ||= Configuration.new end |
.configure {|config| ... } ⇒ void
This method returns an undefined value.
Yields the global Configuration object for mutation.
74 75 76 |
# File 'lib/safe_memoize.rb', line 74 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.
100 101 102 103 104 |
# File 'lib/safe_memoize.rb', line 100 def self.deprecate(subject, message:, horizon:) text = "[SafeMemoize] #{subject} is deprecated and will be removed in #{horizon}. #{}" 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.
60 61 62 |
# File 'lib/safe_memoize.rb', line 60 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.
90 91 92 |
# File 'lib/safe_memoize.rb', line 90 def self.reset_configuration! @configuration = Configuration.new end |