discord test stars

About

A Redis client in the Mys programming language.

Project: https://github.com/mys-lang/package-redis

Examples

More examples can be found in the repository’s examples directory.

String

from redis import Client

func main():
    client = Client()
    client.connect()
    client.set("my_key", b"my_value")
    print(client.get("my_key"))
    client.disconnect()

Build and run:

 mys run
 Reading package configuration (0 seconds)
 Building (0.01 seconds)
b"my_value"

List

from redis import Client

func main():
    client = Client()
    client.connect()
    client.lpush("bar", b"1")
    client.rpush("bar", b"2")
    print(client.lpop("bar"))
    print(client.rpop("bar"))
    client.disconnect()

Build and run:

 mys run
 Reading package configuration (0 seconds)
 Building (0.01 seconds)
b"1"
b"2"

Hash

from redis import Client

func main():
    client = Client()
    client.connect()
    client.hset("fie", "a", b"x")
    client.hset("fie", "b", b"y")
    print(client.hget("fie", "a"))
    print(client.hgetall("fie"))
    client.hdel("fie", "a")
    print(client.hgetall("fie"))
    client.disconnect()

Build and run:

 mys run
 Reading package configuration (0 seconds)
 Building (0.01 seconds)
b"x"
{"b": b"y", "a": b"x"}
{"b": b"y"}

Publish

from redis import Client

func main():
    client = Client()
    client.connect()
    client.publish("my_channel", b"my_payload")
    client.disconnect()

Build and run:

 mys run
 Reading package configuration (0 seconds)
 Building (0.01 seconds)

Subscribe

from redis import Client
from redis import PublishMessage
from redis import SubscribeMessage
from redis import UnsubscribeMessage

func main():
    client = Client()
    client.connect()
    client.subscribe("my_channel")

    while True:
        match client.get_message():
            case PublishMessage() as publish_message:
                print(publish_message)
            case SubscribeMessage() as subscribe_message:
                print(subscribe_message)
            case UnsubscribeMessage() as unsubscribe_message:
                print(unsubscribe_message)

Build and run, and publish hi on my_channel in another terminal:

 mys run
 Reading package configuration (0 seconds)
 Building (0.01 seconds)
SubscribeMessage(channel="my_channel", number_of_subscriptions=1)
PublishMessage(channel="my_channel", payload=b"hi")

Pipeline

Multiple commands in flight simultaniously.

from redis import Client

func main():
    client = Client()
    client.connect()
    client.set_write("foo", b"bar")
    client.get_write("foo")
    client.set_read()
    print(client.get_read())
    client.disconnect()

Build and run:

 mys run
 Reading package configuration (0 seconds)
 Building (0.01 seconds)
b"bar"

API

trait Message:
    A received Pub/Sub message.
trait Reply:
    A command reply.
class RedisError(Error):
    message: string
class SubscribeMessage(Message):
    A Pub/Sub subscribe reply message.

    channel: string
    number_of_subscriptions: i64
class UnsubscribeMessage(Message):
    An Pub/Sub unsubscribe reply message.

    channel: string
    number_of_subscriptions: i64
class PublishMessage(Message):
    A Pub/Sub published message.

    channel: string
    payload: bytes
class SimpleStringReply(Reply):
    A simple string command reply.

    value: string
class ErrorReply(Reply):
    An error command reply.

    error: string
    message: string
class IntegerReply(Reply):
    An integer command reply.

    value: i64
class BulkStringReply(Reply):
    A bulk string command reply.

    value: bytes?
class ArrayReply(Reply):
    An array command reply.

    items: [Reply]?
class Client:
    A Redis client.

    func __init__(self):

    func connect(self, host: string = "127.0.0.1", port: i64 = 6379):
        Connect to given server.

    func disconnect(self):
        Disconnect from the server.

    func auth(self, password: string):
        Authenticate.

    func del(self, key: string) -> i64:
        Delete given key.

    func set(self, key: string, value: bytes):
        Set given value for given key.

    func get(self, key: string) -> bytes?:
        Get the value for given key.

    func getdel(self, key: string) -> bytes?:
        Get the value for given key and then delete it.

    func append(self, key: string, value: bytes) -> i64:
        Append given value for given key.

    func incr(self, key: string) -> i64:
        Increment the value for given key. Returns the value after the
        increment.

    func decr(self, key: string) -> i64:
        Decrement the value for given key. Returns the value after the
        decrement.

    func strlen(self, key: string) -> i64:
        Get the value length for given key.

    func scan(self, cursor: i64, pattern: string) -> (i64, [string]):
        Scan for matching keys.

    func lpush(self, key: string, value: bytes) -> i64:
        Prepend given value for given list key.

    func lpop(self, key: string) -> bytes?:
        Pop the first value for given list key.

    func rpush(self, key: string, value: bytes) -> i64:
        Append given value for given list key.

    func rpop(self, key: string) -> bytes?:
        Pop the last value for given list key.

    func hset(self, key: string, field: string, value: bytes) -> i64:
        Set given field to given value for given hash key.

    func hget(self, key: string, field: string) -> bytes?:
        Get the value for given field for given hash key.

    func hgetall(self, key: string) -> {string: bytes}:
        Get all fields and valus for given hash key.

    func hdel(self, key: string, field: string) -> i64:
        Delete given field for given hash key.

    func publish(self, channel: string, message: bytes) -> i64:
        Publish given message on given channel. Returns the number of
        clients that received the message.

    func subscribe(self, channel: string):
        Subscribe to given channel. May be called from any fiber, even
        if another fiber is waiting for a message.

        Call get_message() to get the next message.

        Only a limited set of commands are allowed once in Pub/Sub mode.

    func unsubscribe(self, channel: string):
        Unsubscribe from given channel. May be called from any fiber, even
        if another fiber is waiting for a message.

    func psubscribe(self, pattern: string):
        Subscribe to given pattern. May be called from any fiber, even
        if another fiber is waiting for a message.

        Call get_message() to get the next message.

        Only a limited set of commands are allowed once in Pub/Sub mode.

    func punsubscribe(self, pattern: string):
        Unsubscribe from given pattern. May be called from any fiber, even
        if another fiber is waiting for a message.

    func get_message(self) -> Message:
        Get the next Pub/Sub message. Blocks until a message is received.

    func call(self, command: [bytes]) -> Reply:
        Call given command.

    func call_integer_reply(self, command: [bytes]) -> i64:
        Call given command and expect an integer (:) reply.

    func call_bulk_string_reply(self, command: [bytes]) -> bytes?:
        Call given command and expect a bulk string ($) reply.

    func call_simple_string_reply(self, command: [bytes]) -> string:
        Call given command and expect a simple string (+) reply.

    func del_write(self, key: string):
        Write pipelined delete given key.

    func del_read(self) -> i64:
        Read pipelined delete given key.

    func set_write(self, key: string, value: bytes):
        Write pipelined set given value for given key.

    func set_read(self):
        Read pipelined set given value for given key.

    func get_write(self, key: string):
        Write pipelined get the value for given key.

    func get_read(self) -> bytes?:
        Read pipelined get the value for given key.

    func call_write(self, command: [bytes]):
        Write pipelined call given command.

    func call_read(self) -> Reply:
        Read pipelined call given command.