Skip to content

added REPLACE modifier for restore command #661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,16 @@ def dump(key)
# @param [String] key
# @param [String] ttl
# @param [String] serialized_value
# @param [Hash] options
# - `:replace => Boolean`: if false, raises an error if key already exists
# @raise [Redis::CommandError]
# @return [String] `"OK"`
def restore(key, ttl, serialized_value)
def restore(key, ttl, serialized_value, options = {})
args = [:restore, key, ttl, serialized_value]
args << 'REPLACE' if options[:replace]

synchronize do |client|
client.call([:restore, key, ttl, serialized_value])
client.call(args)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/redis/distributed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ def dump(key)
end

# Create a key using the serialized value, previously obtained using DUMP.
def restore(key, ttl, serialized_value)
node_for(key).restore(key, ttl, serialized_value)
def restore(key, ttl, serialized_value, options = {})
node_for(key).restore(key, ttl, serialized_value, options)
end

# Transfer a key from the connected instance to another instance.
Expand Down
8 changes: 8 additions & 0 deletions test/lint/value_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ def test_dump_and_restore
assert r.restore("bar", 1000, w)
assert_equal ["b", "c", "d"], r.lrange("bar", 0, -1)
assert [0, 1].include? r.ttl("bar")

r.set("bar", "somethingelse")
assert_raises(Redis::CommandError) { r.restore("bar", 1000, w) } # ensure by default replace is false
assert_raises(Redis::CommandError) { r.restore("bar", 1000, w, :replace => false) }
assert_equal "somethingelse", r.get("bar")
assert r.restore("bar", 1000, w, :replace => true)
assert_equal ["b", "c", "d"], r.lrange("bar", 0, -1)
assert [0, 1].include? r.ttl("bar")
end
end

Expand Down