Sha256: 732d94f2116b3e2701a1df6c3305dbe2a65d3f20ee02e110f2cb6232d3d9bd6e

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

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)
      method_missing(: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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis-namespace-0.1.1 lib/redis/namespace.rb