Skip to content

blinklang/redis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

redis

A Redis client library for Blink.

Install

Add to your blink.toml:

[dependencies]
redis = { path = "../path/to/redis" }

Quick Start

import redis

fn main() ! Net, IO {
    // Connect with automatic cleanup via Closeable
    with client.connect("localhost", 6379) as c {
        client.set(c, "greeting", "hello")
        let val = client.get(c, "greeting")
        match val {
            Ok(Some(s)) => io.println("got: {s}")
            Ok(None) => io.println("key not found")
            Err(e) => io.println("error")
        }
    }
}

Or manage the connection manually:

import redis

fn main() ! Net, IO {
    let c = client.connect("localhost", 6379).unwrap()
    client.ping(c)
    client.set(c, "key", "value")
    client.close(c)
}

API

Connection

Function Signature Description
client.connect (host: Str, port: Int) -> Result[RedisClient, RedisError] Connect to Redis with 5s timeout
client.close (client: RedisClient) Close the connection

RedisClient implements Closeable for use with with-as blocks.

Core Commands

Function Returns Redis Command
client.ping(c) Result[Str, RedisError] PING
client.get(c, key) Result[Option[Str], RedisError] GET
client.set(c, key, val) Result[Str, RedisError] SET
client.set_ex(c, key, val, secs) Result[Str, RedisError] SET ... EX
client.del(c, keys) Result[Int, RedisError] DEL
client.exists(c, key) Result[Int, RedisError] EXISTS
client.expire(c, key, secs) Result[Int, RedisError] EXPIRE
client.ttl(c, key) Result[Int, RedisError] TTL
client.incr(c, key) Result[Int, RedisError] INCR
client.decr(c, key) Result[Int, RedisError] DECR
client.keys(c, pattern) Result[List[Str], RedisError] KEYS

Hash Commands

Function Returns Redis Command
client.hget(c, key, field) Result[Option[Str], RedisError] HGET
client.hset(c, key, field, val) Result[Int, RedisError] HSET
client.hdel(c, key, field) Result[Int, RedisError] HDEL
client.hgetall(c, key) Result[Map[Str, Str], RedisError] HGETALL

List Commands

Function Returns Redis Command
client.lpush(c, key, val) Result[Int, RedisError] LPUSH
client.rpush(c, key, val) Result[Int, RedisError] RPUSH
client.lpop(c, key) Result[Option[Str], RedisError] LPOP
client.rpop(c, key) Result[Option[Str], RedisError] RPOP
client.lrange(c, key, start, stop) Result[List[Str], RedisError] LRANGE

Set Commands

Function Returns Redis Command
client.sadd(c, key, member) Result[Int, RedisError] SADD
client.srem(c, key, member) Result[Int, RedisError] SREM
client.smembers(c, key) Result[List[Str], RedisError] SMEMBERS

Pub/Sub

Function Returns Redis Command
client.publish(c, channel, msg) Result[Int, RedisError] PUBLISH

Transactions

Function Returns Redis Command
client.multi(c) Result[Str, RedisError] MULTI
client.exec(c) Result[RedisValue, RedisError] EXEC
client.discard(c) Result[Str, RedisError] DISCARD

Error Handling

All commands return Result[T, RedisError]. RedisError has three variants:

  • Network(message: Str) — connection failures, timeouts, closed connections
  • Protocol(message: Str) — malformed RESP responses
  • Server(message: Str) — errors returned by Redis itself (e.g. wrong type operations)
let result = client.get(c, "mykey")
match result {
    Ok(Some(val)) => io.println("value: {val}")
    Ok(None) => io.println("not found")
    Err(RedisError.Network(msg)) => io.println("network error: {msg}")
    Err(RedisError.Server(msg)) => io.println("redis error: {msg}")
    Err(RedisError.Protocol(msg)) => io.println("protocol error: {msg}")
}

RESP Protocol

The resp module is also available for low-level RESP protocol work:

import redis

let cmd = resp.serialize_command(["SET", "key", "value"])
let parsed = resp.parse("+OK\r\n")

Requirements

  • Blink >= 0.29.0
  • Redis server

About

Redis client library for the Blink programming language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors