Module: Protocol::Redis::Cluster::Methods::Scripting

Defined in:
lib/protocol/redis/cluster/methods/scripting.rb

Overview

Methods for managing Redis scripting in cluster environments.

Scripting operations in Redis clusters require careful consideration of key distribution. EVAL and EVALSHA operations are routed based on the keys they access, while SCRIPT management commands may need to be executed on specific nodes or all nodes.

Instance Method Summary collapse

Instance Method Details

#eval(script, key_count = 0, *keys_and_args, role: :master) ⇒ Object

Execute a Lua script server side in a cluster environment.

The script will be executed on the node determined by the first key’s slot. Redis will return a CROSSSLOT error if keys span multiple slots.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/protocol/redis/cluster/methods/scripting.rb', line 27

def eval(script, key_count = 0, *keys_and_args, role: :master)
  if key_count == 0
    # No keys, can execute on any client
    any_client(role).call("EVAL", script, key_count, *keys_and_args)
  else
    # Extract keys for routing
    keys = keys_and_args[0, key_count]
    args = keys_and_args[key_count..-1] || []
    
    # Route to appropriate cluster node based on first key
    # Redis will handle CROSSSLOT validation
    slot = slot_for(keys.first)
    client_for(slot, role).call("EVAL", script, key_count, *keys, *args)
  end
end

#evalsha(sha1, key_count = 0, *keys_and_args, role: :master) ⇒ Object

Execute a cached Lua script by SHA1 digest in a cluster environment.

The script will be executed on the node determined by the first key’s slot. Redis will return a CROSSSLOT error if keys span multiple slots. The script must already be loaded on the target node via SCRIPT LOAD.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/protocol/redis/cluster/methods/scripting.rb', line 55

def evalsha(sha1, key_count = 0, *keys_and_args, role: :master)
  if key_count == 0
    # No keys, can execute on any client
    any_client(role).call("EVALSHA", sha1, key_count, *keys_and_args)
  else
    # Extract keys for routing
    keys = keys_and_args[0, key_count]
    args = keys_and_args[key_count..-1] || []
    
    # Route to appropriate cluster node based on first key
    # Redis will handle CROSSSLOT validation
    slot = slot_for(keys.first)
    client_for(slot, role).call("EVALSHA", sha1, key_count, *keys, *args)
  end
end

#script(subcommand, *arguments, role: :master) ⇒ Object

Execute script management commands in a cluster environment.

Supported script subcommands:

  • DEBUG: Set the debug mode for executed scripts on the target node.

  • EXISTS: Check if scripts exist in the script cache on the target node.

  • FLUSH: Remove all scripts from the script cache (propagates cluster-wide when executed on master).

  • KILL: Kill the currently executing script on the target node.

  • LOAD: Load a script into the script cache (propagates cluster-wide when executed on master).

It is unlikely that DEBUG, EXISTS and KILL are useful when run on a cluster node at random.



86
87
88
# File 'lib/protocol/redis/cluster/methods/scripting.rb', line 86

def script(subcommand, *arguments, role: :master)
  any_client(role).call("SCRIPT", subcommand.to_s, *arguments)
end