Files
redka/docs/usage-module.md
Anton 818954d339 refactor: use vfs=memdb instead of mode=memory&cache=shared
Shared cache is discouraged. The supported alternative
(and increasingly recommended by SQLite developers),
is the memdb VFS (which I believe is supported by all drivers).
I'm sure modernc has it, and mattn also does if compiled with
SQLITE_ENABLE_DESERIALIZE, which became the default in 2021.

https://github.com/ncruces/go-sqlite3/issues/94#issuecomment-2157679766
2024-06-10 21:55:49 +05:00

1.9 KiB

Using Redka as a Go module

The primary object in Redka is the DB. To open or create your database, use the redka.Open() function:

package main

import (
    "log"

    _ "github.com/mattn/go-sqlite3"
    "github.com/nalgeon/redka"
)

func main() {
    // Open or create the data.db file.
    db, err := redka.Open("data.db", nil)
    if err != nil {
        log.Fatal(err)
    }
    // Always close the database when you are finished.
    defer db.Close()
    // ...
}

Don't forget to import the driver (here I use github.com/mattn/go-sqlite3). Using modernc.org/sqlite is slightly different, see example/modernc/main.go for details.

To open an in-memory database that doesn't persist to disk, use the following path:

// All data is lost when the database is closed.
redka.Open("file:/data.db?vfs=memdb")

After opening the database, call redka.DB methods to run individual commands:

db.Str().Set("name", "alice")
db.Str().Set("age", 25)

count, err := db.Key().Count("name", "age", "city")
slog.Info("count", "count", count, "err", err)

name, err := db.Str().Get("name")
slog.Info("get", "name", name, "err", err)
count count=2 err=<nil>
get name="alice" err=<nil>

See the full example in example/simple/main.go.

Use transactions to batch commands. There are View (read-only transaction) and Update (writable transaction) methods for this:

updCount := 0
err := db.Update(func(tx *redka.Tx) error {
    err := tx.Str().Set("name", "bob")
    if err != nil {
        return err
    }
    updCount++

    err = tx.Str().Set("age", 50)
    if err != nil {
        return err
    }
    updCount++
    return nil
})
slog.Info("updated", "count", updCount, "err", err)
updated count=2 err=<nil>

See the full example in example/tx/main.go.

See the package documentation for API reference.