Skip to content

Commit

Permalink
Redis cache store: fix expanding empty keys with no namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeclaghorn committed Mar 31, 2020
1 parent a1b6f08 commit 8c4d6a0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/cache/redis_cache_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,11 @@ def write_multi_entries(entries, expires_in: nil, **options)

# Truncate keys that exceed 1kB.
def normalize_key(key, options)
truncate_key super.b
truncate_key super&.b
end

def truncate_key(key)
if key.bytesize > max_key_bytesize
if key && key.bytesize > max_key_bytesize
suffix = ":sha2:#{::Digest::SHA2.hexdigest(key)}"
truncate_at = max_key_bytesize - suffix.bytesize
"#{key.byteslice(0, truncate_at)}#{suffix}"
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/cache/behaviors/cache_store_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ def test_read_multi_with_expires
end
end

def test_read_multi_with_empty_keys_and_a_logger_and_no_namespace
@cache.options[:namespace] = nil
@cache.logger = ActiveSupport::Logger.new(nil)
assert_equal({}, @cache.read_multi)
end

def test_fetch_multi
@cache.write("foo", "bar")
@cache.write("fud", "biz")
Expand Down

0 comments on commit 8c4d6a0

Please sign in to comment.