Class: Redis::Namespace
- Inherits:
-
Object
- Object
- Redis::Namespace
- Defined in:
- lib/redis/namespace.rb
Constant Summary collapse
- COMMANDS =
The following table defines how input parameters and result values should be modified for the namespace.
COMMANDS is a hash. Each key is the name of a command and each value is a two element array.
The first element in the value array describes how to modify the arguments passed. It can be one of:
nil Do nothing. :first Add the namespace to the first argument passed, e.g. GET key => GET namespace:key :all Add the namespace to all arguments passed, e.g. MGET key1 key2 => MGET namespace:key1 namespace:key2 :exclude_first Add the namespace to all arguments but the first, e.g. :exclude_last Add the namespace to all arguments but the last, e.g. BLPOP key1 key2 timeout => BLPOP namespace:key1 namespace:key2 timeout :exclude_options Add the namespace to all arguments, except the last argument, if the last argument is a hash of options. ZUNIONSTORE key1 2 key2 key3 WEIGHTS 2 1 => ZUNIONSTORE namespace:key1 2 namespace:key2 namespace:key3 WEIGHTS 2 1 :alternate Add the namespace to every other argument, e.g. MSET key1 value1 key2 value2 => MSET namespace:key1 value1 namespace:key2 value2
The second element in the value array describes how to modify the return value of the Redis call. It can be one of:
nil Do nothing. :all Add the namespace to all elements returned, e.g. key1 key2 => namespace:key1 namespace:key2
{ "append" => [:first], "auth" => [], "bgrewriteaof" => [], "bgsave" => [], "bitcount" => [ :first ], "bitop" => [ :exclude_first ], "blpop" => [ :exclude_last, :first ], "brpop" => [ :exclude_last ], "brpoplpush" => [ :exclude_last ], "config" => [], "dbsize" => [], "debug" => [ :exclude_first ], "decr" => [ :first ], "decrby" => [ :first ], "del" => [ :all ], "discard" => [], "dump" => [ :first ], "exists" => [ :first ], "expire" => [ :first ], "expireat" => [ :first ], "flushall" => [], "flushdb" => [], "get" => [ :first ], "getbit" => [ :first ], "getrange" => [ :first ], "getset" => [ :first ], "hset" => [ :first ], "hsetnx" => [ :first ], "hget" => [ :first ], "hincrby" => [ :first ], "hincrbyfloat" => [ :first ], "hmget" => [ :first ], "hmset" => [ :first ], "hdel" => [ :first ], "hexists" => [ :first ], "hlen" => [ :first ], "hkeys" => [ :first ], "hvals" => [ :first ], "hgetall" => [ :first ], "incr" => [ :first ], "incrby" => [ :first ], "incrbyfloat" => [ :first ], "info" => [], "keys" => [ :first, :all ], "lastsave" => [], "lindex" => [ :first ], "linsert" => [ :first ], "llen" => [ :first ], "lpop" => [ :first ], "lpush" => [ :first ], "lpushx" => [ :first ], "lrange" => [ :first ], "lrem" => [ :first ], "lset" => [ :first ], "ltrim" => [ :first ], "mapped_hmset" => [ :first ], "mapped_hmget" => [ :first ], "mapped_mget" => [ :all, :all ], "mget" => [ :all ], "monitor" => [ :monitor ], "move" => [ :first ], "mset" => [ :alternate ], "msetnx" => [ :alternate ], "object" => [ :exclude_first ], "persist" => [ :first ], "pexpire" => [ :first ], "pexpireat" => [ :first ], "ping" => [], "psetex" => [ :first ], "psubscribe" => [ :all ], "pttl" => [ :first ], "publish" => [ :first ], "punsubscribe" => [ :all ], "quit" => [], "randomkey" => [], "rename" => [ :all ], "renamenx" => [ :all ], "restore" => [ :first ], "rpop" => [ :first ], "rpoplpush" => [ :all ], "rpush" => [ :first ], "rpushx" => [ :first ], "sadd" => [ :first ], "save" => [], "scard" => [ :first ], "sdiff" => [ :all ], "sdiffstore" => [ :all ], "select" => [], "set" => [ :first ], "setbit" => [ :first ], "setex" => [ :first ], "setnx" => [ :first ], "setrange" => [ :first ], "shutdown" => [], "sinter" => [ :all ], "sinterstore" => [ :all ], "sismember" => [ :first ], "slaveof" => [], "smembers" => [ :first ], "smove" => [ :exclude_last ], "sort" => [ :sort ], "spop" => [ :first ], "srandmember" => [ :first ], "srem" => [ :first ], "subscribe" => [ :all ], "sunion" => [ :all ], "sunionstore" => [ :all ], "ttl" => [ :first ], "type" => [ :first ], "unsubscribe" => [ :all ], "watch" => [ :all ], "zadd" => [ :first ], "zcard" => [ :first ], "zcount" => [ :first ], "zincrby" => [ :first ], "zinterstore" => [ :exclude_options ], "zrange" => [ :first ], "zrangebyscore" => [ :first ], "zrank" => [ :first ], "zrem" => [ :first ], "zremrangebyrank" => [ :first ], "zremrangebyscore" => [ :first ], "zrevrange" => [ :first ], "zrevrangebyscore" => [ :first ], "zrevrank" => [ :first ], "zscore" => [ :first ], "zunionstore" => [ :exclude_options ], "[]" => [ :first ], "[]=" => [ :first ] }
- ALIASES =
support previous versions of redis gem
case when defined? Redis::Client::ALIASES then Redis::Client::ALIASES when defined? Redis::ALIASES then Redis::ALIASES else {} end
Instance Attribute Summary collapse
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#redis ⇒ Object
readonly
Returns the value of attribute redis.
Instance Method Summary collapse
-
#initialize(namespace, options = {}) ⇒ Namespace
constructor
A new instance of Namespace.
- #keys(query = nil) ⇒ Object
- #method_missing(command, *args, &block) ⇒ Object
- #multi(&block) ⇒ Object
- #pipelined(&block) ⇒ Object
- #respond_to?(command, include_private = false) ⇒ Boolean
- #self_respond_to? ⇒ Object
-
#type(key) ⇒ Object
Ruby defines a now deprecated type method so we need to override it here since it will never hit method_missing.
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(command, *args, &block) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/redis/namespace.rb', line 221 def method_missing(command, *args, &block) handling = COMMANDS[command.to_s] || COMMANDS[ALIASES[command.to_s]] # redis-namespace does not know how to handle this command. # Passing it to @redis as is. if handling.nil? return @redis.send(command, *args, &block) end (before, after) = handling # Add the namespace to any parameters that are keys. case before when :first args[0] = add_namespace(args[0]) if args[0] when :all args = add_namespace(args) when :exclude_first first = args.shift args = add_namespace(args) args.unshift(first) if first when :exclude_last last = args.pop args = add_namespace(args) args.push(last) if last when :exclude_options if args.last.is_a?(Hash) last = args.pop args = add_namespace(args) args.push(last) else args = add_namespace(args) end when :alternate args.each_with_index { |a, i| args[i] = add_namespace(a) if i.even? } when :sort args[0] = add_namespace(args[0]) if args[0] [:by, :get, :store].each do |key| args[1][key] = add_namespace(args[1][key]) if args[1][key] end if args[1].is_a?(Hash) end # Dispatch the command to Redis and store the result. result = @redis.send(command, *args, &block) # Remove the namespace from results that are keys. case after when :all result = rem_namespace(result) when :first result[0] = rem_namespace(result[0]) if result end result end |
Instance Attribute Details
#namespace ⇒ Object
Returns the value of attribute namespace.
185 186 187 |
# File 'lib/redis/namespace.rb', line 185 def namespace @namespace end |
#redis ⇒ Object (readonly)
Returns the value of attribute redis.
186 187 188 |
# File 'lib/redis/namespace.rb', line 186 def redis @redis end |
Instance Method Details
#keys(query = nil) ⇒ Object
209 210 211 |
# File 'lib/redis/namespace.rb', line 209 def keys(query = nil) query.nil? ? super("*") : super end |
#multi(&block) ⇒ Object
213 214 215 |
# File 'lib/redis/namespace.rb', line 213 def multi(&block) namespaced_block(:multi, &block) end |
#pipelined(&block) ⇒ Object
217 218 219 |
# File 'lib/redis/namespace.rb', line 217 def pipelined(&block) namespaced_block(:pipelined, &block) end |
#respond_to?(command, include_private = false) ⇒ Boolean
201 202 203 204 205 206 207 |
# File 'lib/redis/namespace.rb', line 201 def respond_to?(command, include_private=false) if self_respond_to?(command, include_private) true else @redis.respond_to?(command, include_private) end end |
#self_respond_to? ⇒ Object
199 |
# File 'lib/redis/namespace.rb', line 199 alias_method :self_respond_to?, :respond_to? |
#type(key) ⇒ Object
Ruby defines a now deprecated type method so we need to override it here since it will never hit method_missing
195 196 197 |
# File 'lib/redis/namespace.rb', line 195 def type(key) method_missing(:type, key) end |