Skip to content

Commit

Permalink
Merge pull request #45842 from trevorturk/validate-expires-at
Browse files Browse the repository at this point in the history
Log a warning if ActiveSupport::Cache is given an expiration in the past
  • Loading branch information
guilleiguaran committed Aug 18, 2022
2 parents cc13846 + 7eeb4af commit 3d84e12
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Log a warning if `ActiveSupport::Cache` is given an expiration in the past.

*Trevor Turk*

* Add `skip_nil:` support to `ActiveSupport::Cache::Store#fetch_multi`.

*Daniel Alfaro*
Expand Down
4 changes: 4 additions & 0 deletions activesupport/lib/active_support/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ def merged_options(call_options)
expires_at = call_options.delete(:expires_at)
call_options[:expires_in] = (expires_at - Time.now) if expires_at

if call_options[:expires_in]&.negative?
ActiveSupport::Cache::Store.logger&.warn("Cache expiration is in the past")
end

if options.empty?
call_options
else
Expand Down
8 changes: 8 additions & 0 deletions activesupport/test/cache/behaviors/cache_store_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,14 @@ def test_expires_in_and_expires_at
assert_equal "Either :expires_in or :expires_at can be supplied, but not both", error.message
end

def test_expires_at_logs_a_warning_if_in_the_past
buffer = StringIO.new
@cache.logger = ActiveSupport::Logger.new(buffer)
key = SecureRandom.uuid
@cache.write(key, "bar", expires_at: 1.minute.ago)
assert_match %r{Cache expiration is in the past}, buffer.string
end

def test_race_condition_protection_skipped_if_not_defined
key = SecureRandom.alphanumeric
@cache.write(key, "bar")
Expand Down

0 comments on commit 3d84e12

Please sign in to comment.