Skip to content

Commit 9f3e46e

Browse files
author
Nicolas Pepin-Perreault
committed
added REPLACE modifier for restore command
1 parent 37ab904 commit 9f3e46e

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

lib/redis.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,17 @@ 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+
# @param [Hash] replace
464+
# @raise [Redis::CommandError]
461465
# @return [String] `"OK"`
462-
def restore(key, ttl, serialized_value)
466+
def restore(key, ttl, serialized_value, options = {})
467+
args = [:restore, key, ttl, serialized_value]
468+
args << 'REPLACE' if options.fetch(:replace, true)
469+
463470
synchronize do |client|
464-
client.call([:restore, key, ttl, serialized_value])
471+
client.call(args)
465472
end
466473
end
467474

lib/redis/distributed.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ 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+
options[:replace] = true unless options.key?(:replace)
149+
node_for(key).restore(key, ttl, serialized_value, options)
149150
end
150151

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

test/lint/value_types.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ 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, replace: false) }
99+
assert_equal "somethingelse", r.get("bar")
96100
end
97101
end
98102

0 commit comments

Comments
 (0)