Module: NexusMods::CacheableApi::CacheableHelpers
- Defined in:
- lib/nexus_mods/cacheable_api.rb
Overview
Some class helpers to make API calls easily cacheable
Instance Method Summary collapse
-
#cacheable_api(*original_method_names, expiry_from_key:, on_cache_update:, key_format: nil) ⇒ Object
Cache methods used for the NexusMods API with a given expiry time in seconds.
Instance Method Details
#cacheable_api(*original_method_names, expiry_from_key:, on_cache_update:, key_format: nil) ⇒ Object
Cache methods used for the NexusMods API with a given expiry time in seconds
- Parameters
-
original_method_names (Array<Symbol>): List of methods to which this cache apply
-
expiry_from_key (Proc): Code giving the number of seconds of cache expiry from the key
- Parameters
-
key (String): The key for which we want the expiry time in seconds
- Result
-
Integer: Corresponding expiry time
-
on_cache_update (Proc): Proc called when the cache has been updated
-
key_format (Proc or nil): Optional proc giving the key format from the target of cacheable [default: nil]. If nil then a default proc concatenating the target’s class, method name and all arguments will be used.
- Parameters
-
target (Object): Object on which the method is cached
-
method_name (Symbol): Method being cached
-
method_args (Array<Object>): Method’s arguments
-
method_kwargs (Hash<Symbol,Object>): Method’s kwargs
- Result
-
String: The corresponding key to be used for caching
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/nexus_mods/cacheable_api.rb', line 41 def cacheable_api(*original_method_names, expiry_from_key:, on_cache_update:, key_format: nil) cacheable_with_expiry( *original_method_names, key_format: key_format || proc do |target, method_name, method_args, method_kwargs| ( [ target.class, method_name ] + method_args + method_kwargs.map { |key, value| "#{key}:#{value}" } ).join('/') end, expiry_from_key:, cache_options: { on_cache_update: proc do |_adapter, _key, _value, , _context| on_cache_update.call end } ) end |