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.



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

def initialize(cache_backend)
  @cache_backend = cache_backend
end

Instance Attribute Details

#cache_backendObject

Returns the value of attribute cache_backend.



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

def cache_backend
  @cache_backend
end

Instance Method Details

#clearObject



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

def clear
  @cache_backend.clear if IdentityCache.should_update_cache?
end

#delete(key) ⇒ Object



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

def delete(key)
  @cache_backend.delete(key) if IdentityCache.should_update_cache?
end

#fetch(key) ⇒ Object



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

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

#fetch_multi(keys, &block) ⇒ Object



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

def fetch_multi(keys, &block)
  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_update_cache?
      results[key] = replacement_result
    end
  end
  results
end

#write(key, value) ⇒ Object



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

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