ActiveSupport#Cache :race_condition_ttl option handling not optimal #14157

Open
wants to merge 3 commits into from

4 participants

@niclas

nil.to_i ==> 0, e.g. second condition is always evaluated here...

@patricksrobertson patricksrobertson commented on an outdated diff Feb 22, 2014
activesupport/lib/active_support/cache.rb
@@ -563,7 +563,7 @@ def find_cached_entry(key, name, options)
def handle_expired_entry(entry, key, options)
if entry && entry.expired?
- race_ttl = options[:race_condition_ttl].to_i
+ race_ttl = options[:race_condition_ttl].try(:to_i)

consider something like this instead:

time_since_expired = Time.now.to_f - entry.expires_at
race_ttl = options.fetch(:race_condition_ttl, (time_since_expired - 1))
if time_since_expired <= race_ttl
#do work

This eliminates the whole nil check premise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@patricksrobertson

It would be ideal if there was a test case that noted the change in behavior as well

@patricksrobertson

This is also related to the PR #10001 and should be closed at the same time.

@niclas

After considering PR #10001 this is my new proposal. I think the code gets more readable if time calcs is avoided for zero/nil cases.

@niclas niclas closed this Jul 7, 2015
@niclas niclas reopened this Jul 7, 2015
@sgrif sgrif was assigned by rails-bot Oct 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment