Skip to content

Commit 20913ff

Browse files
Merge #661
661: added REPLACE modifier for restore command r=badboy Added support for REPLACE modifier to the RESTORE command. If :replace option is true, passes along the REPLACE modifier. If the modifier is missing, a Redis::CommandError is raised iff the key already exists. If the modifier is present, the key is overwritten.
2 parents 5fd004d + 72edad4 commit 20913ff

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

lib/redis.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,16 @@ def dump(key)
458458
# @param [String] key
459459
# @param [String] ttl
460460
# @param [String] serialized_value
461+
# @param [Hash] options
462+
# - `:replace => Boolean`: if false, raises an error if key already exists
463+
# @raise [Redis::CommandError]
461464
# @return [String] `"OK"`
462-
def restore(key, ttl, serialized_value)
465+
def restore(key, ttl, serialized_value, options = {})
466+
args = [:restore, key, ttl, serialized_value]
467+
args << 'REPLACE' if options[:replace]
468+
463469
synchronize do |client|
464-
client.call([:restore, key, ttl, serialized_value])
470+
client.call(args)
465471
end
466472
end
467473

lib/redis/distributed.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ def dump(key)
144144
end
145145

146146
# Create a key using the serialized value, previously obtained using DUMP.
147-
def restore(key, ttl, serialized_value)
148-
node_for(key).restore(key, ttl, serialized_value)
147+
def restore(key, ttl, serialized_value, options = {})
148+
node_for(key).restore(key, ttl, serialized_value, options)
149149
end
150150

151151
# Transfer a key from the connected instance to another instance.

test/lint/value_types.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ def test_dump_and_restore
9393
assert r.restore("bar", 1000, w)
9494
assert_equal ["b", "c", "d"], r.lrange("bar", 0, -1)
9595
assert [0, 1].include? r.ttl("bar")
96+
97+
r.set("bar", "somethingelse")
98+
assert_raises(Redis::CommandError) { r.restore("bar", 1000, w) } # ensure by default replace is false
99+
assert_raises(Redis::CommandError) { r.restore("bar", 1000, w, :replace => false) }
100+
assert_equal "somethingelse", r.get("bar")
101+
assert r.restore("bar", 1000, w, :replace => true)
102+
assert_equal ["b", "c", "d"], r.lrange("bar", 0, -1)
103+
assert [0, 1].include? r.ttl("bar")
96104
end
97105
end
98106

0 commit comments

Comments
 (0)