Module: Valkey::Commands::ScriptingCommands

Included in:
Valkey::Commands
Defined in:
lib/valkey/commands/scripting_commands.rb

Overview

this module contains commands related to list data type.

Instance Method Summary collapse

Instance Method Details

#eval(script, args: [], keys: []) ⇒ Object

TODO: not implemented in Glide



99
# File 'lib/valkey/commands/scripting_commands.rb', line 99

def eval(script, args: [], keys: []); end

#evalsha(script, args: [], keys: []) ⇒ Object

TODO: not implemented in Glide



102
# File 'lib/valkey/commands/scripting_commands.rb', line 102

def evalsha(script, args: [], keys: []); end

#invoke_script(script, args: [], keys: []) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/valkey/commands/scripting_commands.rb', line 104

def invoke_script(script, args: [], keys: [])
  arg_ptrs, arg_lens = build_command_args(args)
  keys_ptrs, keys_lens = build_command_args(keys)

  route = ""
  route_buf = FFI::MemoryPointer.from_string(route)

  sha = FFI::MemoryPointer.new(:char, script.bytesize + 1)
  sha.put_bytes(0, script)

  res = Bindings.invoke_script(
    @connection,
    0,
    sha,
    keys.size,
    keys_ptrs,
    keys_lens,
    args.size,
    arg_ptrs,
    arg_lens,
    route_buf,
    route.bytesize
  )

  convert_response(res)
end

#script(subcommand, args = nil, options: {}) ⇒ String, ...

Control remote script registry.

Examples:

Load a script

sha = valkey.script(:load, "return 1")
  # => <sha of this script>

Check if a script exists

valkey.script(:exists, sha)
  # => true

Check if multiple scripts exist

valkey.script(:exists, [sha, other_sha])
  # => [true, false]

Flush the script registry

valkey.script(:flush)
  # => "OK"

Kill a running script

valkey.script(:kill)
  # => "OK"

Parameters:

  • subcommand (String)

    e.g. ‘exists`, `flush`, `load`, `kill`

  • args (Array<String>) (defaults to: nil)

    depends on subcommand

Returns:

  • (String, Boolean, Array<Boolean>, ...)

    depends on subcommand

See Also:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/valkey/commands/scripting_commands.rb', line 34

def script(subcommand, args = nil, options: {})
  subcommand = subcommand.to_s.downcase

  if args.nil?
    send("script_#{subcommand}", **options)
  else
    send("script_#{subcommand}", args)
  end

  # if subcommand == "exists"
  #   arg = args.first
  #
  #   send_command([:script, :exists, arg]) do |reply|
  #     reply = reply.map { |r| Boolify.call(r) }
  #
  #     if arg.is_a?(Array)
  #       reply
  #     else
  #       reply.first
  #     end
  #   end
  # else
  #   send_command([:script, subcommand] + args)
  # end
end

#script_exists(args) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/valkey/commands/scripting_commands.rb', line 72

def script_exists(args)
  send_command(RequestType::SCRIPT_EXISTS, Array(args)) do |reply|
    if args.is_a?(Array)
      reply
    else
      reply.first
    end
  end
end

#script_flush(sync: false, async: false) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/valkey/commands/scripting_commands.rb', line 60

def script_flush(sync: false, async: false)
  args = []

  if async
    args << "async"
  elsif sync
    args << "sync"
  end

  send_command(RequestType::SCRIPT_FLUSH, args)
end

#script_killObject



82
83
84
# File 'lib/valkey/commands/scripting_commands.rb', line 82

def script_kill
  send_command(RequestType::SCRIPT_KILL)
end

#script_load(script) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/valkey/commands/scripting_commands.rb', line 86

def script_load(script)
  script = script.is_a?(Array) ? script.first : script

  buf = FFI::MemoryPointer.new(:char, script.bytesize)
  buf.put_bytes(0, script)

  result = Bindings.store_script(buf, script.bytesize)

  hash_buffer = Bindings::ScriptHashBuffer.new(result)
  hash_buffer[:ptr].read_string(hash_buffer[:len])
end