nats

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 12 Imported by: 0

README


id: nats title: Nats

Release Discord Test Security Linter

A NATS Key/Value storage driver.

Note: Requires Go 1.20 and above

Table of Contents

Signatures

func New(config ...Config) Storage
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() 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 possibilities to create a storage:

// 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
 // Logger. Using Fiber AllLogger interface for adapting the various log libraries.
 Logger log.AllLogger
 // Use the Logger for nats events, default: false
 Verbose bool
 // 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,
}

Documentation

Index

Constants

This section is empty.

Variables

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

ConfigDefault is the default config

Functions

This section is empty.

Types

type 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
	// Logger. Using Fiber AllLogger interface for adapting the various log libraries.
	Logger log.AllLogger
	// Use the Logger for nats events, default: false
	Verbose bool
	// Wait for connection to be established, default: 100ms
	WaitForConnection time.Duration
}

Config defines the config for storage.

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

Storage interface that is implemented by storage providers

func New

func New(config ...Config) *Storage

New creates a new nats kv storage

func (*Storage) Close

func (s *Storage) Close() error

Close the nats connection

func (*Storage) Conn

func (s *Storage) Conn() (*nats.Conn, jetstream.KeyValue)

Return database client

func (*Storage) Delete

func (s *Storage) Delete(key string) error

Delete key by key

func (*Storage) Get

func (s *Storage) Get(key string) ([]byte, error)

Get value by key

func (*Storage) Keys

func (s *Storage) Keys() ([]string, error)

Return all the keys

func (*Storage) Reset

func (s *Storage) Reset() error

Reset all keys

func (*Storage) Set

func (s *Storage) Set(key string, val []byte, exp time.Duration) error

Set key with value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL