Class: Redis::Cluster

Inherits:
Redis
  • Object
show all
Defined in:
lib/redis/cluster.rb,
lib/redis/cluster/client.rb,
lib/redis/cluster/version.rb,
lib/redis/cluster/transaction_adapter.rb

Defined Under Namespace

Classes: AmbiguousNodeError, Client, CommandErrorCollection, InitialSetupError, NodeMightBeDown, OrchestrationCommandNotSupported, TransactionAdapter, TransactionConsistencyError

Constant Summary collapse

VERSION =
Redis::VERSION

Instance Method Summary collapse

Constructor Details

#initializeRedis::Cluster

Create a new client instance

Parameters:

  • options (Hash)


68
69
70
# File 'lib/redis/cluster.rb', line 68

def initialize(*) # rubocop:disable Lint/UselessMethodDefinition
  super
end

Instance Method Details

#cluster(subcommand, *args) ⇒ Object

Sends ‘CLUSTER *` command to random node and returns its reply.

Parameters:

  • subcommand (String, Symbol)

    the subcommand of cluster command e.g. ‘:slots`, `:nodes`, `:slaves`, `:info`

Returns:

  • (Object)

    depends on the subcommand

See Also:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/redis/cluster.rb', line 81

def cluster(subcommand, *args)
  subcommand = subcommand.to_s.downcase
  block = case subcommand
  when 'slots'
    HashifyClusterSlots
  when 'nodes'
    HashifyClusterNodes
  when 'slaves'
    HashifyClusterSlaves
  when 'info'
    HashifyInfo
  else
    Noop
  end

  send_command([:cluster, subcommand] + args, &block)
end

#connectionObject

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/redis/cluster.rb', line 47

def connection
  raise NotImplementedError, "Redis::Cluster doesn't implement #connection"
end

#watch(*keys, &block) ⇒ Object

Watch the given keys to determine execution of the MULTI/EXEC block.

Using a block is required for a cluster client. It’s different from a standalone client. And you should use the block argument as a receiver if you call commands.

An ‘#unwatch` is automatically issued if an exception is raised within the block that is a subclass of StandardError and is not a ConnectionError.

Examples:

A typical use case.

# The client is an instance of the internal adapter for the optimistic locking
redis.watch("{my}key") do |client|
  if client.get("{my}key") == "some value"
    # The tx is an instance of the internal adapter for the transaction
    client.multi do |tx|
      tx.set("{my}key", "other value")
      tx.incr("{my}counter")
    end
  else
    client.unwatch
  end
end
#=> ["OK", 6]

Parameters:

  • keys (String, Array<String>)

    one or more keys to watch

Returns:

  • (Object)

    returns the return value of the block



124
125
126
# File 'lib/redis/cluster.rb', line 124

def watch(*keys, &block)
  synchronize { |c| c.watch(*keys, &block) }
end