valkey

Native async Valkey/Redis client for MoonBit

moon add Metalymph/valkey@0.5.0
Download zip
Author
Version
0.5.0
License
MIT
Last updated
last month
Downloads
139
README

#Valkey

Native async Valkey/Redis client for MoonBit.

Valkey provides a lightweight RESP implementation, TCP networking support, typed Redis helpers, Redis Streams support, and Consumer Group operations.

Built on top of the moonbitlang/async runtime.


#Features

  • Native RESP protocol implementation
  • Async TCP client
  • Typed Redis/Valkey commands
  • Redis Streams support
  • Consumer Groups
  • Pending message inspection
  • Message recovery helpers
  • MoonBit-native APIs


#Installation

{ "deps": { "Metalymph/valkey": "0.5.0" } }

example moon.pkg:
{ "import": [ "Metalymph/valkey" ] }

let client =
@valkey.Client::connect(
"127.0.0.1",
6379,
)

let pong = client.ping()
println(pong)


#Relay Adapter

This package also provides a backend adapter for Metalymph/relay.

Add the dependency to your moon.pkg:
{ "import": [ "Metalymph/relay/core", "Metalymph/valkey/relay" ] }

Usage:
let backend = @relay.ValkeyStreamBackend::new(client, "my_stream", "my_group", "consumer_1")
let queue = backend.to_relay_queue()

Supported Features

Core Commands

  • GET
  • SET
  • DEL
  • EXISTS
  • INCR
  • DECR
  • EXPIRE
  • TTL
  • PING

Streams

  • XADD
  • XGROUP CREATE
  • XREADGROUP
  • XACK
  • XPENDING
  • XAUTOCLAIM

#
RespError

pub suberror RespError {
InvalidPrefix(Int)
UnexpectedEndOfStream
MalformedNumber
MalformedCrlf
} derive(ToJson,
Debug
)

RespError represents errors occurring during RESP parsing.

#
ValkeyError

pub suberror ValkeyError {
ConnectionError(String)
ProtocolError(String)
} derive(ToJson,
Debug
)

ValkeyError represents the error types handled by the Valkey driver.

#
Client

pub struct Client {
conn :
Tcp

}

Client is a native Valkey/Redis client supporting asynchronous operations.

#
Client::connect

async fn Client::connect(host : String, port : Int) -> Client

connect opens an asynchronous connection to the specified host and port.

#
Client::del

async fn Client::del(self : Client, key : String) -> Int64

del removes a key and returns the number of deleted keys.

#
Client::exists

async fn Client::exists(self : Client, key : String) -> Bool

exists returns true when the key exists.

#
Client::get

async fn Client::get(self : Client, key : String) -> String?

get returns a string value by key, or None when the key does not exist.

#
Client::ping

async fn Client::ping(self : Client) -> Unit

ping sends a PING command to the server and expects a PONG response.

#
Client::read_response

async fn Client::read_response(self : Client) -> RespValue

read_response reads a RESP response from the server.

#
Client::request

async fn Client::request(self : Client, args : Array[String]) -> RespValue

request sends a command and returns the response.

#
Client::send_command

async fn Client::send_command(self : Client, args : Array[String]) -> Unit

send_command encodes and sends a RESP command to the server.

#
Client::set

async fn Client::set(self : Client, key : String, value : String) -> Unit

set stores a string value by key.

#
Client::xack

async fn Client::xack(self : Client, stream : String, group : String, id : String) -> Int64

xack acknowledges a stream entry and returns the number of acknowledged ids.

#
Client::xadd

async fn Client::xadd(self : Client, stream : String, payload : String) -> String

xadd appends a payload field to a stream and returns the generated entry id.

#
Client::xautoclaim

async fn Client::xautoclaim(self : Client, stream : String, group : String, consumer : String, min_idle_ms : Int) -> (String, String)?

xautoclaim claims one pending stream entry and returns its (id, payload).

#
Client::xgroup_create

async fn Client::xgroup_create(self : Client, stream : String, group : String) -> Unit

xgroup_create creates a consumer group for a stream, creating the stream if needed.

#
Client::xpending_count

async fn Client::xpending_count(self : Client, stream : String, group : String) -> Int64

xpending_count returns the number of pending entries for a consumer group.

#
Client::xreadgroup

async fn Client::xreadgroup(self : Client, stream : String, group : String, consumer : String, count : Int) -> (String, String)?

xreadgroup reads one payload entry from a stream consumer group.

#
Client::xreadgroup_block

async fn Client::xreadgroup_block(self : Client, stream : String, group : String, consumer : String, count : Int, block_ms : Int) -> (String, String)?

xreadgroup_block reads one payload entry from a stream consumer group, blocking for up to block_ms milliseconds.

#
RespParser

pub struct RespParser[R] {
reader : R
}

RespParser handles decoding RESP messages from a stream.

#
RespParser::new

fn[R] RespParser::new(reader : R) -> RespParser[R]

new creates a new RespParser from an object implementing @io.Reader.

#
RespParser::parse_value

async fn[R :
Reader
] RespParser::parse_value(self : RespParser[R]) -> RespValue

parse_value decodes a single RespValue from the reader.

#
RespValue

pub enum RespValue {
SimpleString(String)
Error(String)
Integer(Int64)
BulkString(String?)
Array(Array[RespValue]?)
} derive(ToJson,
Debug
)

RespValue represents the different types in the RESP (Redis Serialization Protocol).

Source Files

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io