Skip to content

Commit 65e890a

Browse files
authored
Merge pull request rails#42566 from ghiculescu/dalli-store-optimised-cache
Optimized Cache::Entry should support old dalli_store values
2 parents 3ab0de8 + 25b47c2 commit 65e890a

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

activesupport/lib/active_support/cache.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -844,14 +844,20 @@ module Loader
844844
extend self
845845

846846
def load(payload)
847-
if payload.start_with?(MARK_70_UNCOMPRESSED)
847+
if !payload.is_a?(String)
848+
ActiveSupport::Cache::Store.logger&.warn %{Payload wasn't a string, was #{payload.class.name} - couldn't unmarshal, so returning nil."}
849+
850+
return nil
851+
elsif payload.start_with?(MARK_70_UNCOMPRESSED)
848852
members = Marshal.load(payload.byteslice(1..-1))
849853
elsif payload.start_with?(MARK_70_COMPRESSED)
850854
members = Marshal.load(Zlib::Inflate.inflate(payload.byteslice(1..-1)))
851855
elsif payload.start_with?(MARK_61)
852856
return Marshal.load(payload)
853857
else
854-
raise ArgumentError, %{Invalid cache prefix: #{payload.byteslice(0).inspect}, expected "\\x00" or "\\x01"}
858+
ActiveSupport::Cache::Store.logger&.warn %{Invalid cache prefix: #{payload.byteslice(0).inspect}, expected "\\x00" or "\\x01"}
859+
860+
return nil
855861
end
856862
Entry.unpack(members)
857863
end

activesupport/test/cache/behaviors/local_cache_behavior.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,11 @@ def test_local_race_condition_protection
228228
end
229229
end
230230
end
231+
232+
def test_local_cache_should_read_and_write_false
233+
@cache.with_local_cache do
234+
assert @cache.write("foo", false)
235+
assert_equal false, @cache.read("foo")
236+
end
237+
end
231238
end

activesupport/test/cache/stores/mem_cache_store_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,42 @@ def test_large_object_with_default_compression_settings
232232
assert_compressed(LARGE_OBJECT)
233233
end
234234

235+
def test_can_load_raw_values_from_dalli_store
236+
key = "test-with-value-the-way-the-dalli-store-did"
237+
238+
@cache.instance_variable_get(:@data).with { |c| c.set(@cache.send(:normalize_key, key, nil), "value", 0, compress: false) }
239+
assert_nil @cache.read(key)
240+
assert_equal "value", @cache.fetch(key) { "value" }
241+
end
242+
243+
def test_can_load_raw_falsey_values_from_dalli_store
244+
key = "test-with-false-value-the-way-the-dalli-store-did"
245+
246+
@cache.instance_variable_get(:@data).with { |c| c.set(@cache.send(:normalize_key, key, nil), false, 0, compress: false) }
247+
assert_nil @cache.read(key)
248+
assert_equal false, @cache.fetch(key) { false }
249+
end
250+
251+
def test_can_load_raw_values_from_dalli_store_with_local_cache
252+
key = "test-with-value-the-way-the-dalli-store-did-with-local-cache"
253+
254+
@cache.instance_variable_get(:@data).with { |c| c.set(@cache.send(:normalize_key, key, nil), "value", 0, compress: false) }
255+
@cache.with_local_cache do
256+
assert_nil @cache.read(key)
257+
assert_equal "value", @cache.fetch(key) { "value" }
258+
end
259+
end
260+
261+
def test_can_load_raw_falsey_values_from_dalli_store_with_local_cache
262+
key = "test-with-false-value-the-way-the-dalli-store-did-with-local-cache"
263+
264+
@cache.instance_variable_get(:@data).with { |c| c.set(@cache.send(:normalize_key, key, nil), false, 0, compress: false) }
265+
@cache.with_local_cache do
266+
assert_nil @cache.read(key)
267+
assert_equal false, @cache.fetch(key) { false }
268+
end
269+
end
270+
235271
private
236272
def random_string(length)
237273
(0...length).map { (65 + rand(26)).chr }.join

0 commit comments

Comments
 (0)