Class: RailsQuery::Client
- Inherits:
-
Object
- Object
- RailsQuery::Client
- Defined in:
- lib/rails_query/client.rb
Overview
Internal client class responsible for cache interactions
Instance Method Summary collapse
- #fetch(key, ttl: @default_ttl, provider: nil, &block) ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
- #invalidate(key) ⇒ Object
- #invalidate_provider(provider) ⇒ Object
- #store_index(provider, key) ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
6 7 8 9 10 11 |
# File 'lib/rails_query/client.rb', line 6 def initialize(config) @cache = config.cache_store @default_ttl = config.default_ttl @namespace = config.namespace @logger = config.logger end |
Instance Method Details
#fetch(key, ttl: @default_ttl, provider: nil, &block) ⇒ Object
13 14 15 16 17 |
# File 'lib/rails_query/client.rb', line 13 def fetch(key, ttl: @default_ttl, provider: nil, &block) store_index(provider, key) if provider namespaced = namespaced_key(key) @cache.fetch(namespaced, expires_in: ttl, &block) end |
#invalidate(key) ⇒ Object
42 43 44 |
# File 'lib/rails_query/client.rb', line 42 def invalidate(key) @cache.delete(namespaced_key(key)) end |
#invalidate_provider(provider) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rails_query/client.rb', line 30 def invalidate_provider(provider) index_key = index_key(provider) keys = @cache.read(index_key) || [] keys.each do |digest| @cache.delete(namespaced_key(digest)) end @cache.delete(index_key) end |
#store_index(provider, key) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rails_query/client.rb', line 19 def store_index(provider, key) return unless provider idx_key = index_key(provider) keys = @cache.read(idx_key) || [] keys << key @cache.write(idx_key, keys.uniq) end |