valkey-rb 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rubocop.yml +43 -0
- data/.rubocop_todo.yml +22 -0
- data/README.md +34 -0
- data/Rakefile +23 -0
- data/lib/valkey/bindings.rb +173 -0
- data/lib/valkey/commands/bitmap_commands.rb +86 -0
- data/lib/valkey/commands/cluster_commands.rb +259 -0
- data/lib/valkey/commands/connection_commands.rb +318 -0
- data/lib/valkey/commands/function_commands.rb +255 -0
- data/lib/valkey/commands/generic_commands.rb +454 -0
- data/lib/valkey/commands/geo_commands.rb +87 -0
- data/lib/valkey/commands/hash_commands.rb +586 -0
- data/lib/valkey/commands/hyper_log_log_commands.rb +51 -0
- data/lib/valkey/commands/json_commands.rb +389 -0
- data/lib/valkey/commands/list_commands.rb +348 -0
- data/lib/valkey/commands/module_commands.rb +125 -0
- data/lib/valkey/commands/pubsub_commands.rb +237 -0
- data/lib/valkey/commands/scripting_commands.rb +217 -0
- data/lib/valkey/commands/server_commands.rb +961 -0
- data/lib/valkey/commands/set_commands.rb +220 -0
- data/lib/valkey/commands/sorted_set_commands.rb +756 -0
- data/lib/valkey/commands/stream_commands.rb +636 -0
- data/lib/valkey/commands/string_commands.rb +359 -0
- data/lib/valkey/commands/transaction_commands.rb +175 -0
- data/lib/valkey/commands/vector_search_commands.rb +271 -0
- data/lib/valkey/commands.rb +69 -0
- data/lib/valkey/errors.rb +41 -0
- data/lib/valkey/libglide_ffi.dylib +0 -0
- data/lib/valkey/libglide_ffi.so +0 -0
- data/lib/valkey/pipeline.rb +20 -0
- data/lib/valkey/protobuf/command_request_pb.rb +30 -0
- data/lib/valkey/protobuf/connection_request_pb.rb +28 -0
- data/lib/valkey/protobuf/response_pb.rb +18 -0
- data/lib/valkey/pubsub_callback.rb +10 -0
- data/lib/valkey/request_error_type.rb +10 -0
- data/lib/valkey/request_type.rb +436 -0
- data/lib/valkey/response_type.rb +20 -0
- data/lib/valkey/utils.rb +253 -0
- data/lib/valkey/version.rb +5 -0
- data/lib/valkey.rb +477 -0
- metadata +119 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Valkey
|
|
4
|
+
module Commands
|
|
5
|
+
# This module contains commands related to connection management.
|
|
6
|
+
#
|
|
7
|
+
# @see https://valkey.io/commands/#connection
|
|
8
|
+
#
|
|
9
|
+
module ConnectionCommands
|
|
10
|
+
# Authenticate to the server.
|
|
11
|
+
#
|
|
12
|
+
# @param [Array<String>] args includes both username and password
|
|
13
|
+
# or only password
|
|
14
|
+
# @return [String] `OK`
|
|
15
|
+
def auth(*args)
|
|
16
|
+
send_command(RequestType::AUTH, args)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Ping the server.
|
|
20
|
+
#
|
|
21
|
+
# @param [optional, String] message
|
|
22
|
+
# @return [String] `PONG`
|
|
23
|
+
def ping(message = nil)
|
|
24
|
+
send_command(RequestType::PING, [message].compact)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Echo the given string.
|
|
28
|
+
#
|
|
29
|
+
# @param [String] value
|
|
30
|
+
# @return [String]
|
|
31
|
+
def echo(value)
|
|
32
|
+
send_command(RequestType::ECHO, [value])
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Change the selected database for the current connection.
|
|
36
|
+
#
|
|
37
|
+
# @param [Integer] db zero-based index of the DB to use (0 to 15)
|
|
38
|
+
# @return [String] `OK`
|
|
39
|
+
def select(db)
|
|
40
|
+
send_command(RequestType::SELECT, [db])
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Close the connection.
|
|
44
|
+
#
|
|
45
|
+
# @deprecated The QUIT command is deprecated since Redis 7.2.0 / Valkey 7.2+.
|
|
46
|
+
# Clients should use the `close` method directly instead.
|
|
47
|
+
# This avoids lingering TIME_WAIT sockets on the server side.
|
|
48
|
+
#
|
|
49
|
+
# @return [String] `OK` or nil if connection already closed
|
|
50
|
+
# @see https://redis.io/docs/latest/commands/quit/
|
|
51
|
+
def quit
|
|
52
|
+
# For compatibility, we still support QUIT but recommend using close() instead
|
|
53
|
+
send_command(RequestType::QUIT)
|
|
54
|
+
rescue ConnectionError
|
|
55
|
+
# Server closes connection immediately after QUIT
|
|
56
|
+
nil
|
|
57
|
+
ensure
|
|
58
|
+
# Clean up our side of the connection
|
|
59
|
+
close if respond_to?(:close)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Switch to a different protocol version and handshake with the server.
|
|
63
|
+
#
|
|
64
|
+
# @param [Integer] protover Protocol version (2 or 3)
|
|
65
|
+
# @param [Hash] options Optional parameters like AUTH, SETNAME
|
|
66
|
+
# @return [Array] Server information as flat array (TODO: should be Hash for RESP3)
|
|
67
|
+
def hello(protover = 3, **options)
|
|
68
|
+
args = [protover]
|
|
69
|
+
|
|
70
|
+
if options[:auth]
|
|
71
|
+
args << "AUTH"
|
|
72
|
+
args.concat(Array(options[:auth]))
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
args << "SETNAME" << options[:setname] if options[:setname]
|
|
76
|
+
|
|
77
|
+
send_command(RequestType::HELLO, args)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Reset the connection state.
|
|
81
|
+
#
|
|
82
|
+
# @return [String] `RESET`
|
|
83
|
+
def reset
|
|
84
|
+
send_command(RequestType::RESET)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Send a generic CLIENT subcommand.
|
|
88
|
+
#
|
|
89
|
+
# @param [Symbol, String] subcommand The CLIENT subcommand to run, e.g. :list, :id, :kill, etc.
|
|
90
|
+
# @param [Array] args Arguments for the subcommand
|
|
91
|
+
# @return [Object] Depends on subcommand
|
|
92
|
+
# @example
|
|
93
|
+
# client(:id) # => 12345
|
|
94
|
+
# client(:set_name, "my_app") # => "OK"
|
|
95
|
+
# client(:list) # => [{"id" => "1", ...}, ...]
|
|
96
|
+
def client(subcommand, *args)
|
|
97
|
+
send("client_#{subcommand.to_s.downcase}", *args)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Get the current client's ID.
|
|
101
|
+
#
|
|
102
|
+
# @return [Integer] Unique client ID
|
|
103
|
+
def client_id
|
|
104
|
+
send_command(RequestType::CLIENT_ID)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Get the current client's name.
|
|
108
|
+
#
|
|
109
|
+
# @return [String, nil] Client name or nil if not set
|
|
110
|
+
def client_get_name
|
|
111
|
+
send_command(RequestType::CLIENT_GET_NAME)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Set the current client's name.
|
|
115
|
+
#
|
|
116
|
+
# @param [String] name New name for the client connection
|
|
117
|
+
# @return [String] `OK`
|
|
118
|
+
def client_set_name(name)
|
|
119
|
+
send_command(RequestType::CLIENT_SET_NAME, [name])
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Get a list of client connections.
|
|
123
|
+
#
|
|
124
|
+
# @param [String] type Optional filter by client type (normal, master, slave, pubsub)
|
|
125
|
+
# @param [Array<String>] ids Optional filter by client IDs
|
|
126
|
+
# @return [Array<Hash>] List of clients, each represented as a Hash of attributes
|
|
127
|
+
def client_list(type: nil, ids: nil)
|
|
128
|
+
args = []
|
|
129
|
+
|
|
130
|
+
args << "TYPE" << type if type
|
|
131
|
+
|
|
132
|
+
if ids
|
|
133
|
+
args << "ID"
|
|
134
|
+
args.concat(Array(ids))
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
send_command(RequestType::CLIENT_LIST, args) do |reply|
|
|
138
|
+
reply.lines.map do |line|
|
|
139
|
+
entries = line.chomp.split(/[ =]/)
|
|
140
|
+
Hash[entries.each_slice(2).to_a]
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Get information about the current client connection.
|
|
146
|
+
#
|
|
147
|
+
# @return [String] Client connection information
|
|
148
|
+
def client_info
|
|
149
|
+
send_command(RequestType::CLIENT_INFO)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Kill client connections.
|
|
153
|
+
#
|
|
154
|
+
# @param [String] addr Client address (ip:port)
|
|
155
|
+
# @param [Hash] options Optional filters (id, type, user, addr, laddr, skipme)
|
|
156
|
+
# @return [Integer] Number of clients killed
|
|
157
|
+
def client_kill(addr = nil, **options)
|
|
158
|
+
if addr && options.empty?
|
|
159
|
+
send_command(RequestType::CLIENT_KILL_SIMPLE, [addr])
|
|
160
|
+
else
|
|
161
|
+
send_command(RequestType::CLIENT_KILL, build_client_kill_args(addr, options))
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Kill a client connection by address (simple form).
|
|
166
|
+
#
|
|
167
|
+
# @param [String] addr Client address (ip:port)
|
|
168
|
+
# @return [String] `OK`
|
|
169
|
+
def client_kill_simple(addr)
|
|
170
|
+
send_command(RequestType::CLIENT_KILL_SIMPLE, [addr])
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
private
|
|
174
|
+
|
|
175
|
+
def build_client_kill_args(addr, options)
|
|
176
|
+
args = []
|
|
177
|
+
args << "ADDR" << addr if addr
|
|
178
|
+
options.each do |key, value|
|
|
179
|
+
case key
|
|
180
|
+
when :id then args << "ID" << value.to_s
|
|
181
|
+
when :type then args << "TYPE" << value.to_s
|
|
182
|
+
when :user then args << "USER" << value.to_s
|
|
183
|
+
when :addr then args << "ADDR" << value.to_s
|
|
184
|
+
when :laddr then args << "LADDR" << value.to_s
|
|
185
|
+
when :skipme then args << "SKIPME" << (value ? "yes" : "no")
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
args
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
public
|
|
192
|
+
|
|
193
|
+
# Pause client processing.
|
|
194
|
+
#
|
|
195
|
+
# @param [Integer] timeout Pause duration in milliseconds
|
|
196
|
+
# @param [String] mode Optional mode (WRITE, ALL)
|
|
197
|
+
# @return [String] `OK`
|
|
198
|
+
def client_pause(timeout, mode = nil)
|
|
199
|
+
args = [timeout]
|
|
200
|
+
args << mode if mode
|
|
201
|
+
send_command(RequestType::CLIENT_PAUSE, args)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# Unpause client processing.
|
|
205
|
+
#
|
|
206
|
+
# @return [String] `OK`
|
|
207
|
+
def client_unpause
|
|
208
|
+
send_command(RequestType::CLIENT_UNPAUSE)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# Configure client reply mode.
|
|
212
|
+
#
|
|
213
|
+
# @param [String] mode Reply mode (ON, OFF, SKIP)
|
|
214
|
+
# @return [String] `OK`
|
|
215
|
+
def client_reply(mode)
|
|
216
|
+
send_command(RequestType::CLIENT_REPLY, [mode])
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# Unblock a client blocked in a blocking operation.
|
|
220
|
+
#
|
|
221
|
+
# @param [Integer] client_id ID of the client to unblock
|
|
222
|
+
# @param [String] unblock_type Optional unblock type (TIMEOUT, ERROR)
|
|
223
|
+
# @return [Integer] 1 if client was unblocked, 0 otherwise
|
|
224
|
+
def client_unblock(client_id, unblock_type = nil)
|
|
225
|
+
args = [client_id]
|
|
226
|
+
args << unblock_type if unblock_type
|
|
227
|
+
send_command(RequestType::CLIENT_UNBLOCK, args)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# Set client connection information.
|
|
231
|
+
#
|
|
232
|
+
# @param [String] attr Attribute to set (lib-name, lib-ver)
|
|
233
|
+
# @param [String] value Value to set for the attribute
|
|
234
|
+
# @return [String] `OK`
|
|
235
|
+
def client_set_info(attr, value)
|
|
236
|
+
send_command(RequestType::CLIENT_SET_INFO, [attr, value])
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# Enable/disable client caching.
|
|
240
|
+
#
|
|
241
|
+
# @param [String] mode Caching mode (YES, NO)
|
|
242
|
+
# @return [String] `OK`
|
|
243
|
+
def client_caching(mode)
|
|
244
|
+
send_command(RequestType::CLIENT_CACHING, [mode])
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# Configure client tracking.
|
|
248
|
+
#
|
|
249
|
+
# @param [String] status Tracking status (ON, OFF)
|
|
250
|
+
# @param [Array] args Additional positional arguments (REDIRECT, PREFIX, BCAST, OPTIN, OPTOUT, NOLOOP)
|
|
251
|
+
# @param [Hash] options Optional parameters (for keyword argument style)
|
|
252
|
+
# @return [String] `OK`
|
|
253
|
+
# @example Positional style
|
|
254
|
+
# client_tracking("ON", "OPTIN")
|
|
255
|
+
# @example Keyword style
|
|
256
|
+
# client_tracking("ON", optin: true, redirect: 123)
|
|
257
|
+
def client_tracking(status, *args, **options)
|
|
258
|
+
cmd_args = [status]
|
|
259
|
+
cmd_args.concat(args.any? ? args : build_client_tracking_args(options))
|
|
260
|
+
send_command(RequestType::CLIENT_TRACKING, cmd_args)
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
# Get client tracking information.
|
|
264
|
+
#
|
|
265
|
+
# @return [Array] Tracking information
|
|
266
|
+
def client_tracking_info
|
|
267
|
+
send_command(RequestType::CLIENT_TRACKING_INFO)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
# Get the client ID used for tracking redirection.
|
|
271
|
+
#
|
|
272
|
+
# @return [Integer] Client ID for tracking redirection
|
|
273
|
+
def client_getredir
|
|
274
|
+
send_command(RequestType::CLIENT_GET_REDIR)
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
# Enable/disable client no-evict mode.
|
|
278
|
+
#
|
|
279
|
+
# @param [String] mode Mode (ON, OFF)
|
|
280
|
+
# @return [String] `OK`
|
|
281
|
+
def client_no_evict(mode)
|
|
282
|
+
send_command(RequestType::CLIENT_NO_EVICT, [mode.to_s.upcase])
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
# Enable/disable client no-touch mode.
|
|
286
|
+
#
|
|
287
|
+
# @param [String] mode Mode (ON, OFF)
|
|
288
|
+
# @return [String] `OK`
|
|
289
|
+
def client_no_touch(mode)
|
|
290
|
+
send_command(RequestType::CLIENT_NO_TOUCH, [mode.to_s.upcase])
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
private
|
|
294
|
+
|
|
295
|
+
def build_client_tracking_args(options)
|
|
296
|
+
args = []
|
|
297
|
+
options.each do |key, value|
|
|
298
|
+
case key
|
|
299
|
+
when :redirect
|
|
300
|
+
args << "REDIRECT" << value.to_s
|
|
301
|
+
when :prefix
|
|
302
|
+
args << "PREFIX"
|
|
303
|
+
Array(value).each { |prefix| args << prefix }
|
|
304
|
+
when :bcast
|
|
305
|
+
args << "BCAST" if value
|
|
306
|
+
when :optin
|
|
307
|
+
args << "OPTIN" if value
|
|
308
|
+
when :optout
|
|
309
|
+
args << "OPTOUT" if value
|
|
310
|
+
when :noloop
|
|
311
|
+
args << "NOLOOP" if value
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
args
|
|
315
|
+
end
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
end
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Valkey
|
|
4
|
+
module Commands
|
|
5
|
+
# This module contains commands related to Valkey Functions.
|
|
6
|
+
#
|
|
7
|
+
# @see https://valkey.io/commands/#scripting
|
|
8
|
+
#
|
|
9
|
+
module FunctionCommands
|
|
10
|
+
# Delete a library and all its functions.
|
|
11
|
+
#
|
|
12
|
+
# @example Delete a library
|
|
13
|
+
# valkey.function_delete("mylib")
|
|
14
|
+
# # => "OK"
|
|
15
|
+
#
|
|
16
|
+
# @param [String] library_name the library name to delete
|
|
17
|
+
# @return [String] "OK"
|
|
18
|
+
#
|
|
19
|
+
# @see https://valkey.io/commands/function-delete/
|
|
20
|
+
def function_delete(library_name)
|
|
21
|
+
send_command(RequestType::FUNCTION_DELETE, [library_name])
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Return the serialized payload of loaded libraries.
|
|
25
|
+
#
|
|
26
|
+
# @example Dump all libraries
|
|
27
|
+
# valkey.function_dump
|
|
28
|
+
# # => <binary string>
|
|
29
|
+
#
|
|
30
|
+
# @return [String] the serialized payload
|
|
31
|
+
#
|
|
32
|
+
# @see https://valkey.io/commands/function-dump/
|
|
33
|
+
def function_dump
|
|
34
|
+
send_command(RequestType::FUNCTION_DUMP)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Delete all libraries.
|
|
38
|
+
#
|
|
39
|
+
# @example Flush all libraries
|
|
40
|
+
# valkey.function_flush
|
|
41
|
+
# # => "OK"
|
|
42
|
+
# @example Flush all libraries asynchronously
|
|
43
|
+
# valkey.function_flush(async: true)
|
|
44
|
+
# # => "OK"
|
|
45
|
+
# @example Flush all libraries synchronously
|
|
46
|
+
# valkey.function_flush(sync: true)
|
|
47
|
+
# # => "OK"
|
|
48
|
+
#
|
|
49
|
+
# @param [Boolean] async flush asynchronously
|
|
50
|
+
# @param [Boolean] sync flush synchronously
|
|
51
|
+
# @return [String] "OK"
|
|
52
|
+
#
|
|
53
|
+
# @see https://valkey.io/commands/function-flush/
|
|
54
|
+
def function_flush(async: false, sync: false)
|
|
55
|
+
args = []
|
|
56
|
+
|
|
57
|
+
if async
|
|
58
|
+
args << "ASYNC"
|
|
59
|
+
elsif sync
|
|
60
|
+
args << "SYNC"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
send_command(RequestType::FUNCTION_FLUSH, args)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Kill a function that is currently executing.
|
|
67
|
+
#
|
|
68
|
+
# @example Kill a running function
|
|
69
|
+
# valkey.function_kill
|
|
70
|
+
# # => "OK"
|
|
71
|
+
#
|
|
72
|
+
# @return [String] "OK"
|
|
73
|
+
#
|
|
74
|
+
# @see https://valkey.io/commands/function-kill/
|
|
75
|
+
def function_kill
|
|
76
|
+
send_command(RequestType::FUNCTION_KILL)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Return information about the functions and libraries.
|
|
80
|
+
#
|
|
81
|
+
# @example List all libraries
|
|
82
|
+
# valkey.function_list
|
|
83
|
+
# # => [{"library_name" => "mylib", "engine" => "LUA", ...}]
|
|
84
|
+
# @example List libraries matching a pattern
|
|
85
|
+
# valkey.function_list(library_name: "mylib*")
|
|
86
|
+
# # => [{"library_name" => "mylib", ...}]
|
|
87
|
+
# @example List libraries with code
|
|
88
|
+
# valkey.function_list(with_code: true)
|
|
89
|
+
# # => [{"library_name" => "mylib", "library_code" => "...", ...}]
|
|
90
|
+
#
|
|
91
|
+
# @param [String] library_name filter by library name pattern
|
|
92
|
+
# @param [Boolean] with_code include the library code in the response
|
|
93
|
+
# @return [Array<Hash>] array of library information
|
|
94
|
+
#
|
|
95
|
+
# @see https://valkey.io/commands/function-list/
|
|
96
|
+
def function_list(library_name: nil, with_code: false)
|
|
97
|
+
args = []
|
|
98
|
+
|
|
99
|
+
if library_name
|
|
100
|
+
args << "LIBRARYNAME"
|
|
101
|
+
args << library_name
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
args << "WITHCODE" if with_code
|
|
105
|
+
|
|
106
|
+
send_command(RequestType::FUNCTION_LIST, args)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Load a library to Valkey.
|
|
110
|
+
#
|
|
111
|
+
# @example Load a library
|
|
112
|
+
# code = "#!lua name=mylib\nvalkey.register_function('myfunc', function(keys, args) return args[1] end)"
|
|
113
|
+
# valkey.function_load(code)
|
|
114
|
+
# # => "mylib"
|
|
115
|
+
# @example Load a library, replacing if exists
|
|
116
|
+
# valkey.function_load(code, replace: true)
|
|
117
|
+
# # => "mylib"
|
|
118
|
+
#
|
|
119
|
+
# @param [String] function_code the source code
|
|
120
|
+
# @param [Boolean] replace replace the library if it exists
|
|
121
|
+
# @return [String] the library name that was loaded
|
|
122
|
+
#
|
|
123
|
+
# @see https://valkey.io/commands/function-load/
|
|
124
|
+
def function_load(function_code, replace: false)
|
|
125
|
+
args = []
|
|
126
|
+
args << "REPLACE" if replace
|
|
127
|
+
args << function_code
|
|
128
|
+
|
|
129
|
+
send_command(RequestType::FUNCTION_LOAD, args)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Restore libraries from a payload.
|
|
133
|
+
#
|
|
134
|
+
# @example Restore libraries
|
|
135
|
+
# payload = valkey.function_dump
|
|
136
|
+
# valkey.function_restore(payload)
|
|
137
|
+
# # => "OK"
|
|
138
|
+
# @example Restore libraries with FLUSH policy
|
|
139
|
+
# valkey.function_restore(payload, policy: "FLUSH")
|
|
140
|
+
# # => "OK"
|
|
141
|
+
# @example Restore libraries with APPEND policy
|
|
142
|
+
# valkey.function_restore(payload, policy: "APPEND")
|
|
143
|
+
# # => "OK"
|
|
144
|
+
# @example Restore libraries with REPLACE policy
|
|
145
|
+
# valkey.function_restore(payload, policy: "REPLACE")
|
|
146
|
+
# # => "OK"
|
|
147
|
+
#
|
|
148
|
+
# @param [String] serialized_value the serialized payload from FUNCTION DUMP
|
|
149
|
+
# @param [String] policy the restore policy: "FLUSH", "APPEND", or "REPLACE"
|
|
150
|
+
# @return [String] "OK"
|
|
151
|
+
#
|
|
152
|
+
# @see https://valkey.io/commands/function-restore/
|
|
153
|
+
def function_restore(serialized_value, policy: nil)
|
|
154
|
+
args = [serialized_value]
|
|
155
|
+
|
|
156
|
+
args << policy.to_s.upcase if policy
|
|
157
|
+
|
|
158
|
+
send_command(RequestType::FUNCTION_RESTORE, args)
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Return information about the function that's currently running.
|
|
162
|
+
#
|
|
163
|
+
# @example Get function stats
|
|
164
|
+
# valkey.function_stats
|
|
165
|
+
# # => {"running_script" => {...}, "engines" => {...}}
|
|
166
|
+
#
|
|
167
|
+
# @return [Hash] function execution statistics
|
|
168
|
+
#
|
|
169
|
+
# @see https://valkey.io/commands/function-stats/
|
|
170
|
+
def function_stats
|
|
171
|
+
send_command(RequestType::FUNCTION_STATS)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# Invoke a function.
|
|
175
|
+
#
|
|
176
|
+
# @example Call a function
|
|
177
|
+
# valkey.fcall("myfunc", keys: ["key1"], args: ["arg1"])
|
|
178
|
+
# # => <function result>
|
|
179
|
+
# @example Call a function without keys
|
|
180
|
+
# valkey.fcall("myfunc", args: ["arg1", "arg2"])
|
|
181
|
+
# # => <function result>
|
|
182
|
+
#
|
|
183
|
+
# @param [String] function the function name
|
|
184
|
+
# @param [Array<String>] keys the keys to pass to the function
|
|
185
|
+
# @param [Array<String>] args the arguments to pass to the function
|
|
186
|
+
# @return [Object] the function result
|
|
187
|
+
#
|
|
188
|
+
# @see https://valkey.io/commands/fcall/
|
|
189
|
+
def fcall(function, keys: [], args: [])
|
|
190
|
+
command_args = [function, keys.size] + keys + args
|
|
191
|
+
send_command(RequestType::FCALL, command_args)
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# Invoke a read-only function.
|
|
195
|
+
#
|
|
196
|
+
# @example Call a read-only function
|
|
197
|
+
# valkey.fcall_ro("myfunc", keys: ["key1"], args: ["arg1"])
|
|
198
|
+
# # => <function result>
|
|
199
|
+
#
|
|
200
|
+
# @param [String] function the function name
|
|
201
|
+
# @param [Array<String>] keys the keys to pass to the function
|
|
202
|
+
# @param [Array<String>] args the arguments to pass to the function
|
|
203
|
+
# @return [Object] the function result
|
|
204
|
+
#
|
|
205
|
+
# @see https://valkey.io/commands/fcall_ro/
|
|
206
|
+
def fcall_ro(function, keys: [], args: [])
|
|
207
|
+
command_args = [function, keys.size] + keys + args
|
|
208
|
+
send_command(RequestType::FCALL_READ_ONLY, command_args)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# Control function registry (convenience method).
|
|
212
|
+
#
|
|
213
|
+
# @example Delete a library
|
|
214
|
+
# valkey.function(:delete, "mylib")
|
|
215
|
+
# # => "OK"
|
|
216
|
+
# @example Dump all libraries
|
|
217
|
+
# valkey.function(:dump)
|
|
218
|
+
# # => <binary string>
|
|
219
|
+
# @example Flush all libraries
|
|
220
|
+
# valkey.function(:flush)
|
|
221
|
+
# # => "OK"
|
|
222
|
+
# @example Kill a running function
|
|
223
|
+
# valkey.function(:kill)
|
|
224
|
+
# # => "OK"
|
|
225
|
+
# @example List all libraries
|
|
226
|
+
# valkey.function(:list)
|
|
227
|
+
# # => [...]
|
|
228
|
+
# @example Load a library
|
|
229
|
+
# valkey.function(:load, code)
|
|
230
|
+
# # => "mylib"
|
|
231
|
+
# @example Restore libraries
|
|
232
|
+
# valkey.function(:restore, payload)
|
|
233
|
+
# # => "OK"
|
|
234
|
+
# @example Get function stats
|
|
235
|
+
# valkey.function(:stats)
|
|
236
|
+
# # => {...}
|
|
237
|
+
#
|
|
238
|
+
# @param [String, Symbol] subcommand the subcommand (delete, dump, flush, kill, list, load, restore, stats)
|
|
239
|
+
# @param [Array] args arguments for the subcommand
|
|
240
|
+
# @param [Hash] options options for the subcommand
|
|
241
|
+
# @return [Object] depends on subcommand
|
|
242
|
+
def function(subcommand, *args, **options)
|
|
243
|
+
subcommand = subcommand.to_s.downcase
|
|
244
|
+
|
|
245
|
+
if args.empty? && options.empty?
|
|
246
|
+
send("function_#{subcommand}")
|
|
247
|
+
elsif options.empty?
|
|
248
|
+
send("function_#{subcommand}", *args)
|
|
249
|
+
else
|
|
250
|
+
send("function_#{subcommand}", *args, **options)
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
end
|