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



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

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

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



77
78
79
80
81
82
83
# File 'lib/identity_cache/cache_fetcher.rb', line 77

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



67
68
69
70
71
72
73
74
75
# File 'lib/identity_cache/cache_fetcher.rb', line 67

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