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.



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

def initialize(cache_backend)
  @cache_backend = cache_backend
end

Instance Attribute Details

#cache_backendObject

Returns the value of attribute cache_backend.



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

def cache_backend
  @cache_backend
end

Instance Method Details

#clearObject



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

def clear
  @cache_backend.clear
end

#delete(key) ⇒ Object



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

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

#fetch(key) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/identity_cache/fallback_fetcher.rb', line 35

def fetch(key)
  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



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/identity_cache/fallback_fetcher.rb', line 22

def fetch_multi(keys)
  results = @cache_backend.read_multi(*keys)
  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



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

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