Skip to content

Commit

Permalink
Merge pull request redis#289 from bitterb/add-dump-and-restore
Browse files Browse the repository at this point in the history
Add supports for DUMP and RESTORE
  • Loading branch information
pietern committed Nov 4, 2012
2 parents b2bb8c0 + af89db0 commit ea2aaf6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
# 3.0.3 (unreleased)

* Added support for `DUMP` and `RESTORE` (Redis 2.6).

* Added support for `BITCOUNT` and `BITOP` (Redis 2.6).

# 3.0.2
Expand Down
22 changes: 22 additions & 0 deletions lib/redis.rb
Expand Up @@ -359,6 +359,28 @@ def pttl(key)
end
end

# Return a serialized version of the value stored at a key.
#
# @param [String] key
# @return [String] serialized_value
def dump(key)
synchronize do |client|
client.call([:dump, key])
end
end

# Create a key using the serialized value, previously obtained using DUMP.
#
# @param [String] key
# @param [String] ttl
# @param [String] serialized_value
# @return `"OK"`
def restore(key, ttl, serialized_value)
synchronize do |client|
client.call([:restore, key, ttl, serialized_value])
end
end

# Delete one or more keys.
#
# @param [String, Array<String>] keys
Expand Down
10 changes: 10 additions & 0 deletions lib/redis/distributed.rb
Expand Up @@ -137,6 +137,16 @@ def pttl(key)
node_for(key).pttl(key)
end

# Return a serialized version of the value stored at a key.
def dump(key)
node_for(key).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)
end

# Delete a key.
def del(*args)
keys_per_node = args.group_by { |key| node_for(key) }
Expand Down
20 changes: 20 additions & 0 deletions test/lint/value_types.rb
Expand Up @@ -80,6 +80,26 @@ def test_pttl
assert_in_range 1..2000, r.pttl("foo")
end

def test_dump_and_restore
return if version < "2.5.7"

r.set("foo", "a")
v = r.dump("foo")
r.del("foo")

assert r.restore("foo", 1000, v)
assert_equal "a", r.get("foo")
assert [0, 1].include? r.ttl("foo")

r.rpush("bar", ["b", "c", "d"])
w = r.dump("bar")
r.del("bar")

assert r.restore("bar", 1000, w)
assert_equal ["b", "c", "d"], r.lrange("bar", 0, -1)
assert [0, 1].include? r.ttl("bar")
end

def test_move
r.select 14
r.flushdb
Expand Down

0 comments on commit ea2aaf6

Please sign in to comment.