Skip to content

Commit 949b4d1

Browse files
authored
Merge pull request rails#49873 from Mangara/mangara-memcache-fallback
Make return value of `Cache::Store#write` consistent
2 parents 22a26d7 + 4cfdcfe commit 949b4d1

File tree

8 files changed

+35
-14
lines changed

8 files changed

+35
-14
lines changed

activesupport/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Make return values of `Cache::Store#write` consistent.
2+
3+
The return value was not specified before. Now it returns `true` on a successful write,
4+
`nil` if there was an error talking to the cache backend, and `false` if the write failed
5+
for another reason (e.g. the key already exists and `unless_exist: true` was passed).
6+
7+
*Sander Verdonschot*
8+
19
* Fix logged cache keys not always matching actual key used by cache action.
210

311
*Hartley McGuire*

activesupport/lib/active_support/cache.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def retrieve_store_class(store)
166166
# cache = ActiveSupport::Cache::MemoryStore.new
167167
#
168168
# cache.read('city') # => nil
169-
# cache.write('city', "Duckburgh")
169+
# cache.write('city', "Duckburgh") # => true
170170
# cache.read('city') # => "Duckburgh"
171171
#
172172
# cache.write('not serializable', Proc.new {}) # => TypeError
@@ -630,6 +630,9 @@ def fetch_multi(*names)
630630
# Writes the value to the cache with the key. The value must be supported
631631
# by the +coder+'s +dump+ and +load+ methods.
632632
#
633+
# Returns +true+ if the write succeeded, +nil+ if there was an error talking
634+
# to the cache backend, or +false+ if the write failed for another reason.
635+
#
633636
# By default, cache entries larger than 1kB are compressed. Compression
634637
# allows more data to be stored in the same memory footprint, leading to
635638
# fewer cache evictions and higher hit rates.

activesupport/lib/active_support/cache/mem_cache_store.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,10 @@ def write_serialized_entry(key, payload, **options)
263263
# Set the memcache expire a few minutes in the future to support race condition ttls on read
264264
expires_in += 5.minutes
265265
end
266-
rescue_error_with false do
266+
rescue_error_with nil do
267267
# Don't pass compress option to Dalli since we are already dealing with compression.
268268
options.delete(:compress)
269-
@data.with { |c| c.send(method, key, payload, expires_in, **options) }
269+
@data.with { |c| !!c.send(method, key, payload, expires_in, **options) }
270270
end
271271
end
272272

activesupport/lib/active_support/cache/redis_cache_store.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ def write_serialized_entry(key, payload, raw: false, unless_exist: false, expire
368368
if pipeline
369369
pipeline.set(key, payload, **modifiers)
370370
else
371-
failsafe :write_entry, returning: false do
372-
redis.then { |c| c.set key, payload, **modifiers }
371+
failsafe :write_entry, returning: nil do
372+
redis.then { |c| !!c.set(key, payload, **modifiers) }
373373
end
374374
end
375375
end

activesupport/test/cache/behaviors/cache_store_behavior.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
module CacheStoreBehavior
88
def test_should_read_and_write_strings
99
key = SecureRandom.uuid
10-
assert @cache.write(key, "bar")
10+
assert_equal true, @cache.write(key, "bar")
1111
assert_equal "bar", @cache.read(key)
1212
end
1313

1414
def test_should_overwrite
1515
key = SecureRandom.uuid
16-
@cache.write(key, "bar")
17-
@cache.write(key, "baz")
16+
assert_equal true, @cache.write(key, "bar")
17+
assert_equal true, @cache.write(key, "baz")
1818
assert_equal "baz", @cache.read(key)
1919
end
2020

@@ -116,25 +116,25 @@ def test_fetch_with_forced_cache_miss_without_block
116116

117117
def test_should_read_and_write_hash
118118
key = SecureRandom.uuid
119-
assert @cache.write(key, a: "b")
119+
assert_equal true, @cache.write(key, a: "b")
120120
assert_equal({ a: "b" }, @cache.read(key))
121121
end
122122

123123
def test_should_read_and_write_integer
124124
key = SecureRandom.uuid
125-
assert @cache.write(key, 1)
125+
assert_equal true, @cache.write(key, 1)
126126
assert_equal 1, @cache.read(key)
127127
end
128128

129129
def test_should_read_and_write_nil
130130
key = SecureRandom.uuid
131-
assert @cache.write(key, nil)
131+
assert_equal true, @cache.write(key, nil)
132132
assert_nil @cache.read(key)
133133
end
134134

135135
def test_should_read_and_write_false
136136
key = SecureRandom.uuid
137-
assert @cache.write(key, false)
137+
assert_equal true, @cache.write(key, false)
138138
assert_equal false, @cache.read(key)
139139
end
140140

activesupport/test/cache/behaviors/failure_safety_behavior.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ def test_read_multi_failure_returns_empty_hash
4949
end
5050
end
5151

52-
def test_write_failure_returns_false
52+
def test_write_failure_returns_nil
5353
key = SecureRandom.uuid
5454
emulating_unavailability do |cache|
55-
assert_equal false, cache.write(key, SecureRandom.alphanumeric)
55+
assert_nil cache.write(key, SecureRandom.alphanumeric)
5656
end
5757
end
5858

activesupport/test/cache/stores/mem_cache_store_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ def test_write_expires_at
207207
end
208208
end
209209

210+
def test_write_with_unless_exist
211+
assert_equal true, @cache.write("foo", 1)
212+
assert_equal false, @cache.write("foo", 1, unless_exist: true)
213+
end
214+
210215
def test_increment_expires_in
211216
cache = lookup_store(raw: true, namespace: nil)
212217
assert_called_with client(cache), :incr, [ "foo", 1, 60, 1 ] do

activesupport/test/cache/stores/redis_cache_store_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ def test_write_expires_at
195195
end
196196
end
197197

198+
def test_write_with_unless_exist
199+
assert_equal true, @cache.write("foo", 1)
200+
assert_equal false, @cache.write("foo", 1, unless_exist: true)
201+
end
202+
198203
def test_increment_ttl
199204
# existing key
200205
redis_backend(@cache_no_ttl) { |r| r.set "#{@namespace}:jar", 10 }

0 commit comments

Comments
 (0)