Module: SafeMemoize::RactorSharedMethods
- Defined in:
- lib/safe_memoize/ractor_shared_methods.rb
Overview
Class-level methods for Ractor-safe shared caching.
Mixed into a class (via ClassMethods) when any method is memoized with +shared: true, ractor_safe: true+. The class owns a supervisor +Ractor+ that holds the mutable cache hash. All cache reads and writes are serialized through the supervisor's message loop, removing the need for a +Mutex+ (which is not Ractor-shareable).
Constraints for +ractor_safe: true+ memoization:
- Cached return values are made Ractor-shareable via +Ractor.make_shareable+ (deep-frozen in place). Ensure return values can be frozen.
- +if:+, +unless:+, +max_size:+, +ttl_refresh:+, +key:+, and +store:+ are incompatible and raise +ArgumentError+ at +memoize+ time.
- When calling a ractor-safe memoized method from the main Ractor with multiple threads, responses are matched by thread identity so concurrent callers do not consume each other's replies.
Instance Method Summary collapse
-
#ractor_memo_count(method_name = nil) ⇒ Integer
Returns the number of live entries in the Ractor-safe shared cache.
-
#ractor_memoized?(method_name, *args, **kwargs) ⇒ Boolean
Returns +true+ if a live entry exists in the Ractor-safe shared cache.
-
#reset_all_ractor_memos ⇒ void
Clears the entire Ractor-safe shared cache for this class.
-
#reset_ractor_memo(method_name, *args, **kwargs) ⇒ void
Clears one or all entries from the Ractor-safe shared cache.
Instance Method Details
#ractor_memo_count(method_name = nil) ⇒ Integer
Returns the number of live entries in the Ractor-safe shared cache.
69 70 71 72 73 74 |
# File 'lib/safe_memoize/ractor_shared_methods.rb', line 69 def ractor_memo_count(method_name = nil) sup = @__safe_memo_ractor_supervisor__ return 0 unless sup __ractor_cache_send__(sup, :count, method_name&.to_sym) end |
#ractor_memoized?(method_name, *args, **kwargs) ⇒ Boolean
Returns +true+ if a live entry exists in the Ractor-safe shared cache.
55 56 57 58 59 60 61 62 |
# File 'lib/safe_memoize/ractor_shared_methods.rb', line 55 def ractor_memoized?(method_name, *args, **kwargs) method_name = method_name.to_sym sup = @__safe_memo_ractor_supervisor__ return false unless sup key = Ractor.make_shareable([method_name, args.freeze, kwargs.freeze]) __ractor_cache_send__(sup, :memoized, key) end |
#reset_all_ractor_memos ⇒ void
This method returns an undefined value.
Clears the entire Ractor-safe shared cache for this class.
42 43 44 45 46 47 |
# File 'lib/safe_memoize/ractor_shared_methods.rb', line 42 def reset_all_ractor_memos sup = @__safe_memo_ractor_supervisor__ return unless sup __ractor_cache_send__(sup, :clear) end |
#reset_ractor_memo(method_name, *args, **kwargs) ⇒ void
This method returns an undefined value.
Clears one or all entries from the Ractor-safe shared cache.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/safe_memoize/ractor_shared_methods.rb', line 27 def reset_ractor_memo(method_name, *args, **kwargs) method_name = method_name.to_sym sup = @__safe_memo_ractor_supervisor__ return unless sup if args.empty? && kwargs.empty? __ractor_cache_send__(sup, :delete_all, method_name) else key = Ractor.make_shareable([method_name, args.freeze, kwargs.freeze]) __ractor_cache_send__(sup, :delete_one, key) end end |