Files
RW becbbc3dee
Some checks failed
Benchmark / changes (push) Has been cancelled
Benchmark / compare (push) Has been cancelled
Golangci-Lint Check / changes (push) Has been cancelled
Golangci-Lint Check / lint (push) Has been cancelled
Release Drafter (All) / changes (push) Has been cancelled
Release Drafter (All) / release-drafter (push) Has been cancelled
Tests Couchbase / Tests (1.24.x) (push) Has been cancelled
Tests Couchbase / Tests (1.25.x) (push) Has been cancelled
Tests Aerospike / Tests (1.24.x) (push) Has been cancelled
Tests Aerospike / Tests (1.25.x) (push) Has been cancelled
Tests ArangoDB / Tests (1.24.x) (push) Has been cancelled
Tests ArangoDB / Tests (1.25.x) (push) Has been cancelled
Tests Azure Blob / Tests (1.24.x) (push) Has been cancelled
Tests Azure Blob / Tests (1.25.x) (push) Has been cancelled
Tests Cassandra / Tests (1.24.x) (push) Has been cancelled
Tests Cassandra / Tests (1.25.x) (push) Has been cancelled
Tests Clickhouse / Tests (1.24.x) (push) Has been cancelled
Tests Clickhouse / Tests (1.25.x) (push) Has been cancelled
Tests Coherence / Tests (1.24.x) (push) Has been cancelled
Tests Coherence / Tests (1.25.x) (push) Has been cancelled
Tests DynamoDB / Tests (1.24.x) (push) Has been cancelled
Tests DynamoDB / Tests (1.25.x) (push) Has been cancelled
Tests Etcd / Tests (1.24.x) (push) Has been cancelled
Tests Etcd / Tests (1.25.x) (push) Has been cancelled
Tests Firestore / Tests (1.24.x) (push) Has been cancelled
Tests Firestore / Tests (1.25.x) (push) Has been cancelled
Tests Memcache / Tests (1.24.x) (push) Has been cancelled
Tests Memcache / Tests (1.25.x) (push) Has been cancelled
Tests Minio / Tests (1.24.x) (push) Has been cancelled
Tests Minio / Tests (1.25.x) (push) Has been cancelled
Tests Mongodb / Tests (1.24.x) (push) Has been cancelled
Tests Mongodb / Tests (1.25.x) (push) Has been cancelled
Tests MySQL / Tests (1.24.x) (push) Has been cancelled
Tests MySQL / Tests (1.25.x) (push) Has been cancelled
Tests Nats Driver / Tests (1.24.x) (push) Has been cancelled
Tests Nats Driver / Tests (1.25.x) (push) Has been cancelled
Tests Neo4j / Tests (1.24.x) (push) Has been cancelled
Tests Neo4j / Tests (1.25.x) (push) Has been cancelled
Tests Postgres / Tests (1.24.x) (push) Has been cancelled
Tests Postgres / Tests (1.25.x) (push) Has been cancelled
Tests Redis / Tests (1.24.x, 7) (push) Has been cancelled
Tests Redis / Tests (1.24.x, 8) (push) Has been cancelled
Tests Redis / Tests (1.25.x, 7) (push) Has been cancelled
Tests Redis / Tests (1.25.x, 8) (push) Has been cancelled
Tests Rueidis / Tests (1.24.x, 7) (push) Has been cancelled
Tests Rueidis / Tests (1.24.x, 8) (push) Has been cancelled
Tests Rueidis / Tests (1.25.x, 7) (push) Has been cancelled
Tests Rueidis / Tests (1.25.x, 8) (push) Has been cancelled
Tests S3 / Tests (1.24.x) (push) Has been cancelled
Tests S3 / Tests (1.25.x) (push) Has been cancelled
Tests ScyllaDb / Tests (1.24.x) (push) Has been cancelled
Tests ScyllaDb / Tests (1.25.x) (push) Has been cancelled
Tests SurrealDB / Tests (1.24.x) (push) Has been cancelled
Tests SurrealDB / Tests (1.25.x) (push) Has been cancelled
Tests TestHelper Redis / Tests (1.24.x, 7) (push) Has been cancelled
Tests TestHelper Redis / Tests (1.24.x, 8) (push) Has been cancelled
Tests TestHelper Redis / Tests (1.25.x, 7) (push) Has been cancelled
Tests TestHelper Redis / Tests (1.25.x, 8) (push) Has been cancelled
Tests TestHelper TCK / Tests (1.24.x) (push) Has been cancelled
Tests TestHelper TCK / Tests (1.25.x) (push) Has been cancelled
Tests Valkey / Tests (1.24.x, 7) (push) Has been cancelled
Tests Valkey / Tests (1.24.x, 8) (push) Has been cancelled
Tests Valkey / Tests (1.25.x, 7) (push) Has been cancelled
Tests Valkey / Tests (1.25.x, 8) (push) Has been cancelled
Merge pull request #2293 from gofiber/dependabot/go_modules/aerospike/shirou-modules-2205efe4f5
2025-12-18 08:40:37 +01:00
..
2025-07-04 14:42:34 +03:00
2025-07-06 16:07:06 +03:00

id, title
id title
nats Nats

Release Discord Test

A NATS Key/Value storage driver.

Table of Contents

Signatures

func New(config ...Config) Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) GetWithContext(ctx context.Context, key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) DeleteWithContext(ctx context.Context, key string) error
func (s *Storage) Reset() error
func (s *Storage) ResetWithContext(ctx context.Context) error
func (s *Storage) Close() error
func (s *Storage) Conn() (*nats.Conn, jetstream.KeyValue)
func (s *Storage) Keys() ([]string, error)

Installation

NATS Key/Value Store driver is tested on the 2 last Go versions with support for modules. So make sure to initialize one first if you didn't do that yet:

go mod init github.com/<user>/<repo>

And then install the nats implementation:

go get github.com/gofiber/storage/nats

Examples

Import the storage package.

import "github.com/gofiber/storage/nats"

You can use the following options to create a storage driver:

// Initialize default config
store := nats.New()

// Initialize custom config
store := nats.New(Config{
    URLs: "nats://127.0.0.1:4443",
    NatsOptions: []nats.Option{
        nats.MaxReconnects(2),
        // Enable TLS by specifying RootCAs
        nats.RootCAs("./testdata/certs/ca.pem"),
    },
    KeyValueConfig: jetstream.KeyValueConfig{
        Bucket:  "test",
        Storage: jetstream.MemoryStorage,
    },
})

Config

type Config struct {
    // Nats URLs, default "nats://127.0.0.1:4222". Can be comma separated list for multiple servers
    URLs string
    // Nats connection options. See nats_test.go for an example of how to use this.
    NatsOptions []nats.Option
    // Nats connection name
    ClientName string
    // Nats context
    Context context.Context
    // Nats key value config
    KeyValueConfig jetstream.KeyValueConfig
    // Wait for connection to be established, default: 100ms
    WaitForConnection time.Duration
}

Default Config

var ConfigDefault = Config{
    URLs:       nats.DefaultURL,
    Context:    context.Background(),
    ClientName: "fiber_storage",
    KeyValueConfig: jetstream.KeyValueConfig{
    Bucket: "fiber_storage",
    },
    WaitForConnection: 100 * time.Millisecond,
}