Module: Rabarber::Core::Cache
- Defined in:
- lib/rabarber/core/cache.rb
Class Method Summary collapse
- .clear ⇒ Object
- .delete(*roleable_ids, context:) ⇒ Object
- .enabled? ⇒ Boolean
- .fetch(roleable_id, context:, &block) ⇒ Object
- .key_for(id, context) ⇒ Object
Class Method Details
.clear ⇒ Object
30 31 32 |
# File 'lib/rabarber/core/cache.rb', line 30 def clear Rails.cache.delete_matched(/^#{CACHE_PREFIX}/o) end |
.delete(*roleable_ids, context:) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/rabarber/core/cache.rb', line 19 def delete(*roleable_ids, context:) return unless enabled? keys = roleable_ids.map { |roleable_id| key_for(roleable_id, context) } Rails.cache.delete_multi(keys) if keys.any? end |
.enabled? ⇒ Boolean
26 27 28 |
# File 'lib/rabarber/core/cache.rb', line 26 def enabled? Rabarber::Configuration.instance.cache_enabled end |
.fetch(roleable_id, context:, &block) ⇒ Object
13 14 15 16 17 |
# File 'lib/rabarber/core/cache.rb', line 13 def fetch(roleable_id, context:, &block) return yield unless enabled? Rails.cache.fetch(key_for(roleable_id, context), expires_in: 1.hour, race_condition_ttl: 5.seconds, &block) end |
.key_for(id, context) ⇒ Object
34 35 36 |
# File 'lib/rabarber/core/cache.rb', line 34 def key_for(id, context) "#{CACHE_PREFIX}:#{Digest::SHA2.hexdigest("#{id}#{context}")}" end |