Class: IdentityCache::CacheFetcher

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

Defined Under Namespace

Classes: FillLock

Constant Summary collapse

EMPTY_HASH =
{}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_backend) ⇒ CacheFetcher

Returns a new instance of CacheFetcher.



51
52
53
# File 'lib/identity_cache/cache_fetcher.rb', line 51

def initialize(cache_backend)
  @cache_backend = cache_backend
end

Instance Attribute Details

#cache_backendObject

Returns the value of attribute cache_backend.



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

def cache_backend
  @cache_backend
end

Instance Method Details

#clearObject



68
69
70
# File 'lib/identity_cache/cache_fetcher.rb', line 68

def clear
  @cache_backend.clear
end

#delete(key) ⇒ Object



59
60
61
# File 'lib/identity_cache/cache_fetcher.rb', line 59

def delete(key)
  @cache_backend.write(key, IdentityCache::DELETED, expires_in: IdentityCache::DELETED_TTL.seconds)
end

#delete_multi(keys) ⇒ Object



63
64
65
66
# File 'lib/identity_cache/cache_fetcher.rb', line 63

def delete_multi(keys)
  key_values = keys.map { |key| [key, IdentityCache::DELETED] }.to_h
  @cache_backend.write_multi(key_values, expires_in: IdentityCache::DELETED_TTL.seconds)
end

#fetch(key, fill_lock_duration: nil, lock_wait_tries: 2, &block) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/identity_cache/cache_fetcher.rb', line 82

def fetch(key, fill_lock_duration: nil, lock_wait_tries: 2, &block)
  if fill_lock_duration && IdentityCache.should_fill_cache?
    fetch_with_fill_lock(key, fill_lock_duration, lock_wait_tries, &block)
  else
    fetch_without_fill_lock(key, &block)
  end
end

#fetch_multi(keys, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/identity_cache/cache_fetcher.rb', line 72

def fetch_multi(keys, &block)
  if IdentityCache.should_use_cache?
    results = cas_multi(keys, &block)
    results = add_multi(keys, &block) if results.nil?
    results
  else
    {}
  end
end

#write(key, value) ⇒ Object



55
56
57
# File 'lib/identity_cache/cache_fetcher.rb', line 55

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