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, stale: @default_stale, 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
- #write(key, data, ttl, provider: nil) ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
6 7 8 9 10 11 12 13 |
# File 'lib/rails_query/client.rb', line 6 def initialize(config) @cache = config.cache_store @default_ttl = config.default_ttl @default_stale = config.default_stale @executor = config.executor @namespace = config.namespace @logger = config.logger end |
Instance Method Details
#fetch(key, ttl: @default_ttl, stale: @default_stale, provider: nil, &block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rails_query/client.rb', line 15 def fetch(key, ttl: @default_ttl, stale: @default_stale, provider: nil, &block) namespaced = namespaced_key(key) entry = @cache.read(namespaced) if entry age = Time.now - entry[:fetched_at] async_refetch(namespaced, ttl, provider, &block) if stale && stale < age return entry[:data] end data = block.call write(namespaced, data, ttl, provider: provider) data end |
#invalidate(key) ⇒ Object
60 61 62 |
# File 'lib/rails_query/client.rb', line 60 def invalidate(key) @cache.delete(namespaced_key(key)) end |
#invalidate_provider(provider) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rails_query/client.rb', line 48 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
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rails_query/client.rb', line 37 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 |
#write(key, data, ttl, provider: nil) ⇒ Object
31 32 33 34 35 |
# File 'lib/rails_query/client.rb', line 31 def write(key, data, ttl, provider: nil) store_index(provider, key) if provider @cache.write(key, { data: data, fetched_at: Time.now }, expires_in: ttl) end |