hiredis

Hiredis minimalistic Redis client bindings for Kit

Files

FileDescription
.editorconfigEditor formatting configuration
.gitignoreGit ignore rules for build artifacts and dependencies
.tool-versionsasdf tool versions (Zig, Kit)
LICENSEMIT license file
README.mdThis file
c/kit_hiredis.cC FFI wrapper
c/kit_hiredis.hC header for FFI wrapper
examples/basic.kitBasic usage example
kit.tomlPackage manifest with metadata and dependencies
src/hiredis.kitKit HiRedis - Minimalistic Redis client bindings
tests/hiredis.test.kitTests for hiredis

Dependencies

No Kit package dependencies.

Installation

kit add gitlab.com/kit-lang/packages/kit-hiredis.git

System Requirements

PlatformCommand
macOSbrew install hiredis
Ubuntusudo apt install libhiredis-dev
Fedorasudo dnf install hiredis-devel

Usage

import Kit.HiRedis as Redis

main = fn =>
  match Redis.connect "127.0.0.1" 6379
  | Ok ctx ->
    # Ping
    pong = Redis.ping ctx
    println ("PING: " ++ pong)

    # String commands
    match Redis.set ctx "key" "value"
    | Ok _ -> println "SET ok"
    | Err e -> println ("SET error: " ++ e)

    match Redis.get ctx "key"
    | Some val -> println ("GET: " ++ val)
    | None -> println "Key not found"

    # Counter
    val = Redis.incr ctx "counter"
    println ("INCR: " ++ (show val))

    # Lists
    -len = Redis.lpush ctx "mylist" "item"
    match Redis.lpop ctx "mylist"
    | Some item -> println ("LPOP: " ++ item)
    | None -> println "List empty"

    # Hashes
    match Redis.hset ctx "user" "name" "Alice"
    | Ok _ -> println "HSET ok"
    | Err e -> println ("HSET error: " ++ e)

    match Redis.hget ctx "user" "name"
    | Some val -> println ("HGET: " ++ val)
    | None -> println "Field not found"

    # Clean up
    -d = Redis.del ctx "key"
    -d2 = Redis.del ctx "counter"
    -d3 = Redis.del ctx "mylist"
    -d4 = Redis.del ctx "user"

    Redis.free ctx
  | Err e -> println ("Connection error: " ++ e)

main

Reply Handling

kit-hiredis is intentionally a minimalistic binding over libhiredis, and that

extends to reply handling: every helper returns a flat Kit value (Int,

String, Option String, or Result). The general command helper returns

the reply as a single string, which means:

  • simple strings, statuses, integers, and bulk strings come back as their text

representation,

  • nil replies come back as an empty string, and
  • array and other nested replies are collapsed to the placeholder

"(complex reply)".

This is a deliberate scope decision rather than a gap to fill. Structured

replies need a real Kit value bridge for nested arrays, and the sibling

kit-redis package already

provides exactly that: its command escape hatch returns a typed RedisReply

union (RedisSimpleString, RedisInteger, RedisBulkString, RedisArray,

…) with no system library dependency. Rebuilding that bridge on top of the

static C string buffer used here would duplicate kit-redis while losing its

type safety.

Use kit-hiredis when you want thin libhiredis bindings and flat replies are

enough. Use kit-redis when you need structured replies, transactions, or

the broader typed command surface.

Development

Running Examples

Run examples with the interpreter:

kit run examples/basic.kit

Compile examples to a native binary:

kit build examples/basic.kit && ./basic

Running Tests

Run the test suite:

kit test

Run the test suite with coverage:

kit test --coverage

Running kit dev

Run the standard development workflow (format, check, test):

kit dev

This will:

  1. Format and check source files in src/
  2. Run tests in tests/ with coverage

Generating Documentation

Generate API documentation from doc comments:

kit doc

Note: Kit sources with doc comments (##) will generate HTML documents in docs/*.html

Cleaning Build Artifacts

Remove generated files, caches, and build artifacts:

kit task clean

Note: Defined in kit.toml.

Local Installation

To install this package locally for development:

kit install

This installs the package to ~/.kit/packages/@kit/hiredis/, making it available for import as Kit.HiRedis in other projects.

License

This package is released under the MIT License - see LICENSE for details.

Hiredis is released under the BSD 3-Clause License.

Exported Functions & Types

connect

Connect to a Redis server.

Parameters:

Returns:

NonEmptyString -> PositiveInt -> Result Ptr String

connect-timeout

Connect to a Redis server with a timeout.

Parameters:

Returns:

NonEmptyString -> PositiveInt -> NonNegativeInt -> Result Ptr String

connect-unix

Connect to a Redis server via Unix socket.

Parameters:

Returns:

NonEmptyString -> Result Ptr String

free

Free a Redis connection and release its resources.

Parameters:

    Ptr -> Unit

is-connected?

Check if the connection is active.

Parameters:

Returns:

Ptr -> Bool

set

Set a key to a string value.

Parameters:

Returns:

Ptr -> String -> String -> Result Unit String

get

Get the value of a key.

Parameters:

Returns:

Ptr -> String -> Option String

setex

Set a key with an expiration time in seconds.

Parameters:

Returns:

Ptr -> String -> PositiveInt -> String -> Result Unit String

del

Delete a key.

Parameters:

Returns:

Ptr -> String -> Int

exists?

Check if a key exists.

Parameters:

Returns:

Ptr -> String -> Bool

expire

Set an expiration on a key.

Parameters:

Returns:

Ptr -> String -> PositiveInt -> Result Unit String

ttl

Get the remaining time to live of a key.

Parameters:

Returns:

Ptr -> String -> Int

incr

Increment the integer value of a key by 1.

Parameters:

Returns:

Ptr -> String -> Int

decr

Decrement the integer value of a key by 1.

Parameters:

Returns:

Ptr -> String -> Int

incrby

Increment the integer value of a key by a specific amount.

Parameters:

Returns:

Ptr -> String -> Int -> Int

lpush

Push a value to the left (head) of a list.

Parameters:

Returns:

Ptr -> String -> String -> Int

rpush

Push a value to the right (tail) of a list.

Parameters:

Returns:

Ptr -> String -> String -> Int

lpop

Pop a value from the left (head) of a list.

Parameters:

Returns:

Ptr -> String -> Option String

rpop

Pop a value from the right (tail) of a list.

Parameters:

Returns:

Ptr -> String -> Option String

llen

Get the length of a list.

Parameters:

Returns:

Ptr -> String -> Int

hset

Set a field in a hash.

Parameters:

Returns:

Ptr -> String -> String -> String -> Result Unit String

hget

Get a field value from a hash.

Parameters:

Returns:

Ptr -> String -> String -> Option String

hdel

Delete a field from a hash.

Parameters:

Returns:

Ptr -> String -> String -> Int

hexists?

Check if a field exists in a hash.

Parameters:

Returns:

Ptr -> String -> String -> Bool

command

Execute an arbitrary Redis command string.

Parameters:

Returns:

Ptr -> String -> String

ping

Ping the Redis server.

Parameters:

Returns:

Ptr -> String

select-db

Select a Redis database by index.

Parameters:

Returns:

Ptr -> Int -> Result Unit String

flushdb

Flush (delete all keys in) the current database.

Parameters:

Returns:

Ptr -> Result Unit String