Class: IdentityCache::CacheFetcher
- Inherits:
-
Object
- Object
- IdentityCache::CacheFetcher
- Defined in:
- lib/identity_cache/cache_fetcher.rb
Instance Attribute Summary collapse
-
#cache_backend ⇒ Object
Returns the value of attribute cache_backend.
Instance Method Summary collapse
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #fetch(key) ⇒ Object
- #fetch_multi(keys, &block) ⇒ Object
-
#initialize(cache_backend) ⇒ CacheFetcher
constructor
A new instance of CacheFetcher.
- #write(key, value) ⇒ Object
Constructor Details
#initialize(cache_backend) ⇒ CacheFetcher
Returns a new instance of CacheFetcher.
6 7 8 |
# File 'lib/identity_cache/cache_fetcher.rb', line 6 def initialize(cache_backend) @cache_backend = cache_backend end |
Instance Attribute Details
#cache_backend ⇒ Object
Returns the value of attribute cache_backend.
4 5 6 |
# File 'lib/identity_cache/cache_fetcher.rb', line 4 def cache_backend @cache_backend end |
Instance Method Details
#clear ⇒ Object
18 19 20 |
# File 'lib/identity_cache/cache_fetcher.rb', line 18 def clear @cache_backend.clear end |
#delete(key) ⇒ Object
14 15 16 |
# File 'lib/identity_cache/cache_fetcher.rb', line 14 def delete(key) @cache_backend.write(key, IdentityCache::DELETED, expires_in: IdentityCache::DELETED_TTL.seconds) end |
#fetch(key) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/identity_cache/cache_fetcher.rb', line 28 def fetch(key) result = nil yielded = false @cache_backend.cas(key) do |value| yielded = true unless IdentityCache::DELETED == value result = value break end result = yield break unless IdentityCache.should_fill_cache? result end unless yielded result = yield add(key, result) end result end |
#fetch_multi(keys, &block) ⇒ Object
22 23 24 25 26 |
# File 'lib/identity_cache/cache_fetcher.rb', line 22 def fetch_multi(keys, &block) results = cas_multi(keys, &block) results = add_multi(keys, &block) if results.nil? results end |
#write(key, value) ⇒ Object
10 11 12 |
# File 'lib/identity_cache/cache_fetcher.rb', line 10 def write(key, value) @cache_backend.write(key, value) if IdentityCache.should_fill_cache? end |