Module: NexusMods::CacheableWithExpiry::CacheableHelpers
- Defined in:
- lib/nexus_mods/cacheable_with_expiry.rb
Overview
Some class helpers to make cacheable calls easy with expiry proc
Instance Method Summary collapse
-
#cacheable_with_expiry(*original_method_names, **opts) ⇒ Object
Wrap Cacheable’s cacheable method to add the expiry_rules kwarg and use to invalidate the cache of a PersistentJsonAdapter based on the key format.
-
#clear_cacheable_expiry_caches ⇒ Object
Clear expiry times caches.
Instance Method Details
#cacheable_with_expiry(*original_method_names, **opts) ⇒ Object
Wrap Cacheable’s cacheable method to add the expiry_rules kwarg and use to invalidate the cache of a PersistentJsonAdapter based on the key format.
- Parameters
-
original_method_names (Array<Symbol>): List of methods to which this cache apply
-
opts (Hash<Symbol,Object>): kwargs that will be transferred to the cacheable method, with the following ones interpreted:
-
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
-
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/nexus_mods/cacheable_with_expiry.rb', line 31 def cacheable_with_expiry(*original_method_names, **opts) expiry_cache = {} cacheable( *original_method_names, **opts.merge( { cache_options: (opts[:cache_options] || {}).merge( { expiry_from_key: opts[:expiry_from_key], invalidate_if: proc do |key, , context| next true unless context['invalidate_time'] # Find if we know already the expiry for this key expiry_cache[key] = [:expiry_from_key].call(key) unless expiry_cache.key?(key) expiry_cache[key].nil? || (Time.now.utc - Time.parse(context['invalidate_time']).utc > expiry_cache[key]) end, update_context_after_fetch: proc do |_key, _value, , context| context['invalidate_time'] = Time.now.utc.strftime('%FT%T.%9NUTC') end } ) } ) ) @_cacheable_expiry_caches = [] unless defined?(@_cacheable_expiry_caches) @_cacheable_expiry_caches << expiry_cache end |
#clear_cacheable_expiry_caches ⇒ Object
Clear expiry times caches
60 61 62 63 64 65 66 |
# File 'lib/nexus_mods/cacheable_with_expiry.rb', line 60 def clear_cacheable_expiry_caches return unless defined?(@_cacheable_expiry_caches) @_cacheable_expiry_caches.each do |expiry_cache| expiry_cache.replace({}) end end |