Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor namespace support. Use Redis::Namespace instead.

(Why? Because now you can use multiple namespaces within the same app.)
  • Loading branch information...
commit 0fb3a344a158574db5cb6010919a3b8b372f9081 1 parent 76b2e15
@defunkt defunkt authored
Showing with 108 additions and 15 deletions.
  1. +3 −13 lib/redis.rb
  2. +102 −0 lib/redis/namespace.rb
  3. +3 −2 spec/redis_spec.rb
View
16 lib/redis.rb
@@ -107,7 +107,6 @@ def initialize(options = {})
@timeout = (options[:timeout] || 5).to_i
@password = options[:password]
@logger = options[:logger]
- @namespace = options[:namespace]
@thread_safe = options[:thread_safe]
@logger.info { self.to_s } if @logger
@@ -160,7 +159,7 @@ def method_missing(*argv)
call_command(argv)
end
- def call_command(argv, use_namespace = true)
+ def call_command(argv)
@logger.debug { argv.inspect } if @logger
# this wrapper to raw_call_command handle reconnection on socket
@@ -169,7 +168,7 @@ def call_command(argv, use_namespace = true)
connect_to_server if !@sock
begin
- raw_call_command(argv.dup, use_namespace)
+ raw_call_command(argv.dup)
rescue Errno::ECONNRESET, Errno::EPIPE
@sock.close
@sock = nil
@@ -178,7 +177,7 @@ def call_command(argv, use_namespace = true)
end
end
- def raw_call_command(argvp, use_namespace = true)
+ def raw_call_command(argvp)
pipeline = argvp[0].is_a?(Array)
unless pipeline
@@ -199,10 +198,6 @@ def raw_call_command(argvp, use_namespace = true)
argv[-1] = bulk.respond_to?(:bytesize) ? bulk.bytesize : bulk.size
end
- if @namespace && argv[1] && use_namespace
- argv[1] = "#{@namespace}:#{argv[1]}"
- end
-
command << "#{argv.join(' ')}\r\n"
command << "#{bulk}\r\n" if bulk
end
@@ -276,11 +271,6 @@ def mapped_mget(*keys)
result
end
- def mget(*keys)
- keys = keys.map { |key| "#{@namespace}:#{key}"} if @namespace
- call_command([:mget] + keys, false)
- end
-
# Ruby defines a now deprecated type method so we need to override it here
# since it will never hit method_missing
def type(key)
View
102 lib/redis/namespace.rb
@@ -0,0 +1,102 @@
+require 'redis'
+
+class Redis
+ class Namespace
+ # Generated from http://code.google.com/p/redis/wiki/CommandReference
+ # using the following jQuery:
+ #
+ # $('.vt li a').map(function(){ return $(this).text().toLowerCase() }).sort()
+ COMMANDS = [
+ "auth",
+ "bgsave",
+ "dbsize",
+ "decr",
+ "decrby",
+ "del",
+ "exists",
+ "expire",
+ "flushall",
+ "flushdb",
+ "get",
+ "getset",
+ "incr",
+ "incrby",
+ "info",
+ "keys",
+ "lastsave",
+ "lindex",
+ "llen",
+ "lpop",
+ "lpush",
+ "lrange",
+ "lrem",
+ "lset",
+ "ltrim",
+ "mget",
+ "monitor",
+ "move",
+ "quit",
+ "randomkey",
+ "rename",
+ "renamenx",
+ "rpop",
+ "rpush",
+ "sadd",
+ "save",
+ "scard",
+ "sdiff",
+ "sdiffstore",
+ "select",
+ "set",
+ "setnx",
+ "shutdown",
+ "sinter",
+ "sinterstore",
+ "sismember",
+ "slaveof",
+ "smembers",
+ "smove",
+ "sort",
+ "srem",
+ "sunion",
+ "sunionstore",
+ "ttl",
+ "type",
+ "[]",
+ "[]="
+ ]
+
+ def initialize(namespace, options = {})
+ @namespace = namespace
+ @redis = options[:redis]
+ end
+
+ # Ruby defines a now deprecated type method so we need to override it here
+ # since it will never hit method_missing
+ def type(key)
+ call_command(['type', key])
+ end
+
+ def mapped_mget(*keys)
+ result = {}
+ mget(*keys).each do |value|
+ key = keys.shift
+ result.merge!(key => value) unless value.nil?
+ end
+ result
+ end
+
+ def mget(*keys)
+ keys = keys.map { |key| "#{@namespace}:#{key}"} if @namespace
+ call_command([:mget] + keys)
+ end
+
+ def method_missing(command, *args, &block)
+ if COMMANDS.include?(command.to_s) && args[0]
+ args[0] = "#{@namespace}:#{args[0]}"
+ end
+
+ @redis.send(command, *args, &block)
+ end
+ end
+end
View
5 spec/redis_spec.rb
@@ -1,5 +1,6 @@
require File.dirname(__FILE__) + '/spec_helper'
require 'redis/raketasks'
+require 'redis/namespace'
require 'logger'
class Foo
@@ -489,7 +490,7 @@ def ==(other)
end
it "should be able to use a namespace" do
- r = Redis.new(:namespace => :ns, :db => 15)
+ r = Redis::Namespace.new(:ns, :redis => @r)
r.flushdb
r['foo'].should == nil
@@ -504,7 +505,7 @@ def ==(other)
end
it "should be able to use a namespace with mget" do
- r = Redis.new(:namespace => :ns, :db => 15)
+ r = Redis::Namespace.new(:ns, :redis => @r)
r['foo'] = 1000
r['bar'] = 2000
Please sign in to comment.
Something went wrong with that request. Please try again.