kv

package
v2.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Example (Memory)
package main

import (
	"context"
	"fmt"

	"github.com/brexhq/substation/v2/config"
	"github.com/brexhq/substation/v2/internal/kv"
)

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

	// create KV config
	cfg := config.Config{
		Type: "memory",
		Settings: map[string]interface{}{
			"capacity": 3,
		},
	}

	// get KV store using factory method
	kvStore, err := kv.Get(cfg)
	if err != nil {
		panic(err)
	}

	// setup and defer closing KV store
	if err := kvStore.Setup(ctx); err != nil {
		panic(err)
	}
	defer kvStore.Close()

	// set a series of values in the store
	if err := kvStore.Set(ctx, "foo", "bar"); err != nil {
		panic(err)
	}

	if err := kvStore.Set(ctx, "baz", "qux"); err != nil {
		panic(err)
	}

	if err := kvStore.Set(ctx, "quux", "corge"); err != nil {
		panic(err)
	}

	// retrieve a value from the store
	item, err := kvStore.Get(ctx, "foo")
	if err != nil {
		panic(err)
	}

	fmt.Println(item)
}
Output:

bar
Example (MemoryWithTTL)
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/brexhq/substation/v2/config"
	"github.com/brexhq/substation/v2/internal/kv"
)

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

	// create KV config
	cfg := config.Config{
		Type: "memory",
		Settings: map[string]interface{}{
			"capacity": 1,
		},
	}

	// get KV store using factory method
	kvStore, err := kv.Get(cfg)
	if err != nil {
		panic(err)
	}

	// setup and defer closing KV store
	if err := kvStore.Setup(ctx); err != nil {
		panic(err)
	}
	defer kvStore.Close()

	// set a value with time-to-live enabled in the KV store
	ttl := time.Now().Add(1 * time.Microsecond).Unix()
	if err := kvStore.SetWithTTL(ctx, "foo", "bar", ttl); err != nil {
		panic(err)
	}

	time.Sleep(15 * time.Microsecond)

	// retrieve a value from the store
	// if the time-to-live has passed, then the item has expired and is
	// no longer available
	item, err := kvStore.Get(ctx, "foo")
	if err != nil {
		panic(err)
	}

	fmt.Println(item)
}
Output:

<nil>

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// ErrNoLock is returned when a lock cannot be acquired.
	ErrNoLock = fmt.Errorf("unable to acquire lock")
)

Functions

This section is empty.

Types

type Locker

type Locker interface {
	Lock(context.Context, string, int64) error
	Unlock(context.Context, string) error
	Setup(context.Context) error
	IsEnabled() bool
}

func GetLocker

func GetLocker(cfg config.Config) (Locker, error)

Get returns a pointer to a Locker that is stored as a package level global variable. This function and each Locker are safe for concurrent access.

func NewLocker

func NewLocker(cfg config.Config) (Locker, error)

type Storer

type Storer interface {
	Get(context.Context, string) (interface{}, error)
	Set(context.Context, string, interface{}) error
	SetWithTTL(context.Context, string, interface{}, int64) error
	SetAddWithTTL(context.Context, string, interface{}, int64) error
	Setup(context.Context) error
	Close() error
	IsEnabled() bool
}

Storer provides tools for getting values from and putting values into key-value stores.

func Get

func Get(cfg config.Config) (Storer, error)

Get returns a pointer to a Storer that is stored as a package level global variable. This function and each Storer are safe for concurrent access.

func New

func New(cfg config.Config) (Storer, error)

New returns a Storer.

Jump to

Keyboard shortcuts

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