Class: IdentityCache::FallbackFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/identity_cache/fallback_fetcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_backend) ⇒ FallbackFetcher

Returns a new instance of FallbackFetcher.



7
8
9
# File 'lib/identity_cache/fallback_fetcher.rb', line 7

def initialize(cache_backend)
  @cache_backend = cache_backend
end

Instance Attribute Details

#cache_backendObject

Returns the value of attribute cache_backend.



5
6
7
# File 'lib/identity_cache/fallback_fetcher.rb', line 5

def cache_backend
  @cache_backend
end

Instance Method Details

#clearObject



19
20
21
# File 'lib/identity_cache/fallback_fetcher.rb', line 19

def clear
  @cache_backend.clear
end

#delete(key) ⇒ Object



15
16
17
# File 'lib/identity_cache/fallback_fetcher.rb', line 15

def delete(key)
  @cache_backend.delete(key)
end

#fetch(key, **cache_fetcher_options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/identity_cache/fallback_fetcher.rb', line 40

def fetch(key, **cache_fetcher_options)
  unless cache_fetcher_options.empty?
    raise ArgumentError, "unsupported cache_fetcher options: #{cache_fetcher_options.keys.join(", ")}"
  end

  result = @cache_backend.read(key)
  if result.nil?
    result = yield
    @cache_backend.write(key, result) if IdentityCache.should_fill_cache?
  end
  result
end

#fetch_multi(keys) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/identity_cache/fallback_fetcher.rb', line 23

def fetch_multi(keys)
  results = if IdentityCache.should_use_cache?
    @cache_backend.read_multi(*keys)
  else
    {}
  end
  missed_keys = keys - results.keys
  unless missed_keys.empty?
    replacement_results = yield missed_keys
    missed_keys.zip(replacement_results) do |key, replacement_result|
      @cache_backend.write(key, replacement_result) if IdentityCache.should_fill_cache?
      results[key] = replacement_result
    end
  end
  results
end

#write(key, value) ⇒ Object



11
12
13
# File 'lib/identity_cache/fallback_fetcher.rb', line 11

def write(key, value)
  @cache_backend.write(key, value) if IdentityCache.should_fill_cache?
end