v0.7.0

Quick Start: Socket Daemon

Last updated

This page opens a ToriiDB data directory through daemon.New, the entry point to use when several processes share one directory.

Use the Socket Daemon

daemon.New is the recommended entry point when more than one process may touch the same data directory. The first process opens the store and serves a unix socket; every later New on the same directory becomes a client of that socket. Your code calls the same methods either way.

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/pardnchiu/toriidb/core/daemon"
)

func main() {
    ctx := context.Background()

    d, err := daemon.New("./data")
    if err != nil {
        log.Fatal(err)
    }
    defer d.Close()

    if err := d.Start(); err != nil {
        log.Fatal(err)
    }

    if err := d.Set(ctx, 0, "user:1", map[string]any{"name": "Torii", "level": 1}, nil); err != nil {
        log.Fatal(err)
    }

    record, err := d.Get(ctx, 0, "user:1")
    if err != nil {
        log.Fatal(err)
    }
    value, _ := record.Value()
    fmt.Println(record.Type, value)
}

Start is non-blocking and does nothing in client mode. Close stops the listener and closes the store in server mode, or drops pooled connections in client mode.

中文