Module: SafeMemoize::Extension
- Defined in:
- lib/safe_memoize/extension.rb
Overview
Mixin for defining SafeMemoize extensions.
Extend this module in any Ruby module or class that you want to register as a SafeMemoize extension. It provides a DSL for declaring custom +memoize+ options and global cache lifecycle event handlers.
Instance Method Summary collapse
- #dispatch_cache_event(event_type, klass, method_name, cache_key, record) ⇒ Object private
- #handled_options ⇒ Object private
-
#handles_option(option_name) {|value, method_name, all_options| ... } ⇒ void
Declares a custom +memoize+ option handled by this extension.
-
#on_cache_event(*event_types) {|klass, method_name, cache_key, record| ... } ⇒ void
Registers a global cache lifecycle event handler.
- #process_memoize_option(option_name, value, method_name, all_options) ⇒ Object private
Instance Method Details
#dispatch_cache_event(event_type, klass, method_name, cache_key, record) ⇒ 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.
80 81 82 83 84 85 86 |
# File 'lib/safe_memoize/extension.rb', line 80 def dispatch_cache_event(event_type, klass, method_name, cache_key, record) return unless @__event_handlers__ (@__event_handlers__[event_type] || []).each do |handler| handler.call(klass, method_name, cache_key, record) end end |
#handled_options ⇒ 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.
68 69 70 |
# File 'lib/safe_memoize/extension.rb', line 68 def @__handled_options__&.keys || [] end |
#handles_option(option_name) {|value, method_name, all_options| ... } ⇒ void
This method returns an undefined value.
Declares a custom +memoize+ option handled by this extension.
The block is called at +memoize+ definition time whenever +option_name+ appears in the +memoize+ keyword arguments. It receives the option value, the method name being memoized, and the full hash of other extension options passed to that +memoize+ call. It must return a +Hash+ of standard ClassMethods#memoize options to inject (e.g. +{ cache_bust: ... }+), or +nil+/empty hash for no injection.
40 41 42 43 |
# File 'lib/safe_memoize/extension.rb', line 40 def handles_option(option_name, &processor) @__handled_options__ ||= {} @__handled_options__[option_name.to_sym] = processor end |
#on_cache_event(*event_types) {|klass, method_name, cache_key, record| ... } ⇒ void
This method returns an undefined value.
Registers a global cache lifecycle event handler.
The block fires after every matching cache event across all memoized methods on all classes. Multiple event types can be listed in a single call. Valid types are +:on_hit+, +:on_miss+, +:on_store+, +:on_expire+, and +:on_evict+.
Handlers execute on the main Ractor only; they are silently skipped from worker Ractors.
62 63 64 65 |
# File 'lib/safe_memoize/extension.rb', line 62 def on_cache_event(*event_types, &handler) @__event_handlers__ ||= {} event_types.each { |type| (@__event_handlers__[type.to_sym] ||= []) << handler } end |
#process_memoize_option(option_name, value, method_name, all_options) ⇒ 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.
73 74 75 76 77 |
# File 'lib/safe_memoize/extension.rb', line 73 def process_memoize_option(option_name, value, method_name, ) processor = @__handled_options__&.[](option_name.to_sym) result = processor&.call(value, method_name, ) result.is_a?(Hash) ? result : {} end |