Class: Valkey
- Inherits:
-
Object
- Object
- Valkey
- Extended by:
- Bindings
- Includes:
- Commands
- Defined in:
- lib/valkey.rb,
lib/valkey/errors.rb,
lib/valkey/version.rb,
lib/valkey/bindings.rb,
lib/valkey/commands.rb,
lib/valkey/request_type.rb,
lib/valkey/response_type.rb,
lib/valkey/commands/server.rb,
lib/valkey/commands/strings.rb,
lib/valkey/commands/connection.rb
Defined Under Namespace
Modules: Bindings, Commands, RequestType, ResponseType Classes: BaseConnectionError, BaseError, CannotConnectError, CommandError, ConnectionError, InheritedError, InvalidClientOptionError, NoScriptError, OutOfMemoryError, PermissionError, ProtocolError, ReadOnlyError, SubscriptionError, TimeoutError, WrongTypeError
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
- #close ⇒ Object (also: #disconnect!)
-
#initialize(_options = {}) ⇒ Valkey
constructor
TODO: use options.
- #send_command(command_type, command_args = []) ⇒ Object
- #your_pubsub_callback(_client_ptr, kind, msg_ptr, msg_len, chan_ptr, chan_len, pat_ptr, pat_len) ⇒ Object
Methods included from Commands::Server
#bgrewriteaof, #bgsave, #client, #config, #dbsize, #debug, #flushall, #flushdb, #info, #lastsave, #monitor, #save, #shutdown, #slaveof, #slowlog, #sync, #time
Methods included from Commands::Connection
#auth, #echo, #ping, #quit, #select
Methods included from Commands::Strings
Constructor Details
#initialize(_options = {}) ⇒ Valkey
TODO: use options
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/valkey.rb', line 63 def initialize( = {}) request = ConnectionRequest::ConnectionRequest.new( addresses: [ConnectionRequest::NodeAddress.new(host: "127.0.0.1", port: 6379)] ) client_type = Bindings::ClientType.new client_type[:tag] = 1 # AsyncClient request_str = ConnectionRequest::ConnectionRequest.encode(request) request_buf = FFI::MemoryPointer.new(:char, request_str.bytesize) request_buf.put_bytes(0, request_str) request_len = request_str.bytesize response_ptr = Bindings.create_client( request_buf, request_len, client_type, method(:your_pubsub_callback) # Pass the pubsub callback ) res = Bindings::ConnectionResponse.new(response_ptr) @connection = res[:conn_ptr] end |
Instance Method Details
#close ⇒ Object Also known as: disconnect!
89 90 91 |
# File 'lib/valkey.rb', line 89 def close # TODO: handle closing the connection properly end |
#send_command(command_type, command_args = []) ⇒ Object
24 25 26 27 28 29 30 31 32 33 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 59 60 |
# File 'lib/valkey.rb', line 24 def send_command(command_type, command_args = []) channel = 0 route = "" # empty or some serialized route bytes arg_ptrs = FFI::MemoryPointer.new(:pointer, command_args.size) arg_lens = FFI::MemoryPointer.new(:ulong, command_args.size) buffers = [] command_args.each_with_index do |arg, i| buf = FFI::MemoryPointer.from_string(arg) buffers << buf # prevent garbage collection arg_ptrs.put_pointer(i * FFI::Pointer.size, buf) arg_lens.put_ulong(i * 8, arg.bytesize) end route_buf = FFI::MemoryPointer.from_string(route) res = Bindings.command( @connection, # Assuming @connection is set after create channel, command_type, command_args.size, arg_ptrs, arg_lens, route_buf, route.bytesize ) result = Bindings::CommandResult.new(res)[:response] case result[:response_type] when ResponseType::STRING result[:string_value].read_string(result[:string_value_len]) when ResponseType::OK "OK" end end |
#your_pubsub_callback(_client_ptr, kind, msg_ptr, msg_len, chan_ptr, chan_len, pat_ptr, pat_len) ⇒ Object
20 21 22 |
# File 'lib/valkey.rb', line 20 def your_pubsub_callback(_client_ptr, kind, msg_ptr, msg_len, chan_ptr, chan_len, pat_ptr, pat_len) puts "PubSub received kind=#{kind}, message=#{msg_ptr.read_string(msg_len)}, channel=#{chan_ptr.read_string(chan_len)}, pattern=#{pat_ptr.read_string(pat_len)}" end |