Permalink
Browse files

Merge branch 'master' into cargo

Conflicts:
	lib/redis.rb
  • Loading branch information...
soveran committed Sep 13, 2010
2 parents 7074740 + d8af909 commit 262aa190956f364e3ece07e25957c0d24dd0e548
Showing with 145 additions and 75 deletions.
  1. +35 −28 benchmarking/pipeline.rb
  2. +10 −6 lib/redis.rb
  3. +9 −7 lib/redis/client.rb
  4. +4 −0 lib/redis/distributed.rb
  5. +17 −9 test/distributed_test.rb
  6. +20 −11 test/redis_mock.rb
  7. +50 −14 test/redis_test.rb
View
@@ -1,44 +1,51 @@
-require 'benchmark'
+require "benchmark"
+
$:.push File.join(File.dirname(__FILE__), 'lib')
+
require 'redis'
-times = 20000
+ITERATIONS = 10000
+
+@r = Redis.new
-@r = Redis.new#(:debug => true)
-@r['foo'] = "The first line we sent to the server is some text"
+Benchmark.bmbm do |benchmark|
+ benchmark.report("set") do
+ @r.flushdb
-Benchmark.bmbm do |x|
- x.report("set") do
- 20000.times do |i|
- @r["set#{i}"] = "The first line we sent to the server is some text"; @r["foo#{i}"]
+ ITERATIONS.times do |i|
+ @r.set("foo#{i}", "Hello world!")
+ @r.get("foo#{i}")
end
end
-
- x.report("set (pipelined)") do
- @r.pipelined do |pipeline|
- 20000.times do |i|
- pipeline["set_pipelined#{i}"] = "The first line we sent to the server is some text"; @r["foo#{i}"]
+
+ benchmark.report("set (pipelined)") do
+ @r.flushdb
+
+ @r.pipelined do
+ ITERATIONS.times do |i|
+ @r.set("foo#{i}", "Hello world!")
+ @r.get("foo#{i}")
end
end
end
-
- x.report("push+trim") do
- 20000.times do |i|
- @r.push_head "push_trim#{i}", i
- @r.list_trim "push_trim#{i}", 0, 30
+
+ benchmark.report("lpush+ltrim") do
+ @r.flushdb
+
+ ITERATIONS.times do |i|
+ @r.lpush "lpush#{i}", i
+ @r.ltrim "ltrim#{i}", 0, 30
end
end
-
- x.report("push+trim (pipelined)") do
- @r.pipelined do |pipeline|
- 20000.times do |i|
- pipeline.push_head "push_trim_pipelined#{i}", i
- pipeline.list_trim "push_trim_pipelined#{i}", 0, 30
+
+ benchmark.report("lpush+ltrim (pipelined)") do
+ @r.flushdb
+
+ @r.pipelined do
+ ITERATIONS.times do |i|
+ @r.lpush "lpush#{i}", i
+ @r.ltrim "ltrim#{i}", 0, 30
end
end
end
end
-
-@r.keys('*').each do |k|
- @r.delete k
-end
View
@@ -2,7 +2,7 @@
require 'cargo'
class Redis
- VERSION = "2.1.0"
+ VERSION = "2.0.7"
class ProtocolError < RuntimeError
def initialize(reply_type)
@@ -23,10 +23,10 @@ def self.connect(options = {})
url = URI(options.delete(:url) || ENV["REDIS_URL"] || "redis://127.0.0.1:6379/0")
- options[:host] = url.host
- options[:port] = url.port
- options[:password] = url.password
- options[:db] = url.path[1..-1].to_i
+ options[:host] ||= url.host
+ options[:port] ||= url.port
+ options[:password] ||= url.password
+ options[:db] ||= url.path[1..-1].to_i
new(options)
end
@@ -342,7 +342,7 @@ def setnx(key, value)
end
def del(*keys)
- _bool @client.call(:del, *keys)
+ @client.call(:del, *keys)
end
def rename(old_name, new_name)
@@ -357,6 +357,10 @@ def expire(key, seconds)
_bool @client.call(:expire, key, seconds)
end
+ def persist(key)
+ _bool @client.call(:persist, key)
+ end
+
def ttl(key)
@client.call(:ttl, key)
end
View
@@ -52,6 +52,8 @@ def call_without_timeout(*args)
without_socket_timeout do
call(*args)
end
+ rescue Errno::ECONNRESET
+ retry
end
def process(*commands)
@@ -104,13 +106,13 @@ def read
end
def without_socket_timeout
- ensure_connected do
- begin
- self.timeout = 0
- yield
- ensure
- self.timeout = @timeout if connected?
- end
+ connect unless connected?
+
+ begin
+ self.timeout = 0
+ yield
+ ensure
+ self.timeout = @timeout if connected?
end
end
View
@@ -94,6 +94,10 @@ def expireat(key, unix_time)
node_for(key).expireat(key, unix_time)
end
+ def persist(key)
+ node_for(key).persist(key)
+ end
+
def ttl(key)
node_for(key).ttl(key)
end
View
@@ -63,11 +63,11 @@ class RedisDistributedTest < Test::Unit::TestCase
assert_equal ["bar", "baz", "foo"], @r.keys("*").sort
- @r.del "foo"
+ assert_equal [1], @r.del("foo")
assert_equal ["bar", "baz"], @r.keys("*").sort
- @r.del "bar", "baz"
+ assert_equal [2], @r.del("bar", "baz")
assert_equal [], @r.keys("*").sort
end
@@ -144,6 +144,14 @@ class RedisDistributedTest < Test::Unit::TestCase
assert_equal nil, @r.get("foo")
end
+ test "PERSIST" do
+ @r.set("foo", "s1")
+ @r.expire("foo", 1)
+ @r.persist("foo")
+
+ assert_equal(-1, @r.ttl("foo"))
+ end
+
test "TTL" do
@r.set("foo", "s1")
@r.expire("foo", 1)
@@ -347,7 +355,7 @@ class RedisDistributedTest < Test::Unit::TestCase
assert_equal str, @r.get("foo")
end
end
- end
+ end if defined?(Encoding)
test "SETEX" do
@r.setex("foo", 1, "s1")
@@ -565,9 +573,9 @@ class RedisDistributedTest < Test::Unit::TestCase
redis.lpush("foo", "s3")
end
- assert_equal @r.blpop("foo", 0.1), ["foo", "s2"]
- assert_equal @r.blpop("foo", 0.1), ["foo", "s1"]
- assert_equal @r.blpop("foo", 0.4), ["foo", "s3"]
+ assert_equal ["foo", "s2"], @r.blpop("foo", 1)
+ assert_equal ["foo", "s1"], @r.blpop("foo", 1)
+ assert_equal ["foo", "s3"], @r.blpop("foo", 1)
thread.join
end
@@ -582,9 +590,9 @@ class RedisDistributedTest < Test::Unit::TestCase
redis.rpush("foo", "s3")
end
- assert_equal @r.brpop("foo", 0.1), ["foo", "s2"]
- assert_equal @r.brpop("foo", 0.1), ["foo", "s1"]
- assert_equal @r.brpop("foo", 0.4), ["foo", "s3"]
+ assert_equal ["foo", "s2"], @r.brpop("foo", 1)
+ assert_equal ["foo", "s1"], @r.brpop("foo", 1)
+ assert_equal ["foo", "s3"], @r.brpop("foo", 1)
t.join
end
View
@@ -1,23 +1,32 @@
require "socket"
module RedisMock
- def self.start(port = 6380, &block)
+ def self.start(port = 6380)
server = TCPServer.new("127.0.0.1", port)
- session = server.accept
+ loop do
+ session = server.accept
+
+ while line = session.gets
+ parts = Array.new(line[1..-3].to_i) do
+ bytes = session.gets[1..-3].to_i
+ argument = session.read(bytes)
+ session.read(2) # Discard \r\n
+ argument
+ end
- while line = session.gets
- parts = Array.new(line[1..-3].to_i) do
- bytes = session.gets[1..-3].to_i
- argument = session.read(bytes)
session.read(2) # Discard \r\n
- argument
- end
- session.read(2) # Discard \r\n
+ response = yield(*parts)
- session.write block.call(*parts)
- session.write "\r\n"
+ if response.nil?
+ session.shutdown
+ break
+ else
+ session.write(response)
+ session.write("\r\n")
+ end
+ end
end
end
Oops, something went wrong.

0 comments on commit 262aa19

Please sign in to comment.