Skip to content

Commit

Permalink
Rename cas_or_add to upsert and refer to its return value as upserted
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanahsmith committed Jul 10, 2019
1 parent 43242a5 commit 3df9bc4
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/identity_cache/cache_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def fetch(key, fill_lock_duration: nil, lock_wait_limit: 2)

def fetch_without_fill_lock(key)
data = nil
cas_or_add(key) do |value|
upsert(key) do |value|
value = nil if value == IdentityCache::DELETED || FillLock.cache_value?(value)
unless value.nil?
return value
Expand Down Expand Up @@ -162,22 +162,22 @@ def fetch_with_fill_lock(key, fill_lock_duration, lock_wait_limit)
raise "unexpected number of loop iterations"
end

def cas_or_add(key, expiration_options = EMPTY_HASH)
def upsert(key, expiration_options = EMPTY_HASH)
yielded = false
swapped = @cache_backend.cas(key, expiration_options) do |value|
upserted = @cache_backend.cas(key, expiration_options) do |value|
yielded = true
yield value
end
unless yielded
data = yield nil
swapped = add(key, data, expiration_options)
upserted = add(key, data, expiration_options)
end
swapped
upserted
end

def fetch_or_take_lock(key, old_lock:, **expiration_options)
new_lock = nil
swapped = cas_or_add(key, expiration_options) do |value|
upserted = upsert(key, expiration_options) do |value|
if value.nil? || value == IdentityCache::DELETED
if old_lock # cache invalidated
return value
Expand All @@ -198,7 +198,7 @@ def fetch_or_take_lock(key, old_lock:, **expiration_options)
new_lock.cache_value # take lock
end

return new_lock if swapped
return new_lock if upserted

value = @cache_backend.read(key)
if FillLock.cache_value?(value)
Expand All @@ -209,7 +209,7 @@ def fetch_or_take_lock(key, old_lock:, **expiration_options)
end

def fill_with_lock(key, data, my_lock, expiration_options)
swapped = cas_or_add(key, expiration_options) do |value|
upserted = upsert(key, expiration_options) do |value|
return false if value.nil? || value == IdentityCache::DELETED
return true unless FillLock.cache_value?(value) # already filled
current_lock = FillLock.from_cache(*value)
Expand All @@ -219,7 +219,7 @@ def fill_with_lock(key, data, my_lock, expiration_options)
data
end

swapped
upserted
end

def lock_fill_fallback_key(key, lock)
Expand Down

0 comments on commit 3df9bc4

Please sign in to comment.