Class: Determinator::Cache::FetchWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/determinator/cache/fetch_wrapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(*caches, cache_missing: true) ⇒ FetchWrapper

list should will be checked before the tail. If the head is empty but the tail is not then the head will be filled with the value of the tail.

Parameters:

  • *caches (ActiveSupport::Cache)

    If a list then the head of the the



7
8
9
10
11
# File 'lib/determinator/cache/fetch_wrapper.rb', line 7

def initialize(*caches, cache_missing: true)
  @cache_missing = cache_missing
  @caches = caches
  @mutex = Mutex.new
end

Instance Method Details

#call(feature_name) ⇒ Object

Call walks through each cache, returning a value if the item exists in any cache, otherwise popularing each cache with the value of yield.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/determinator/cache/fetch_wrapper.rb', line 15

def call(feature_name)
  value =
    @mutex.synchronize do
      read_and_upfill(feature_name)
    end

  # if the value is missing and we cache it, return the missing response
  return value if value.is_a?(MissingResponse) && @cache_missing

  #otherwise only return the non nil/notice_missing_feature value
  return value if !value.nil? && !(value.is_a?(MissingResponse) && !@cache_missing)

  value_to_write = yield
  return value_to_write if value_to_write.is_a?(ErrorResponse)

  @mutex.synchronize do
    @caches.each do |cache|
      cache.write(key(feature_name), value_to_write)
    end
  end

  return value_to_write
end

#expire(feature_name) ⇒ Object



39
40
41
42
43
# File 'lib/determinator/cache/fetch_wrapper.rb', line 39

def expire(feature_name)
  @mutex.synchronize do
    @caches.each{ |c| c.delete(key(feature_name)) }
  end
end