kv

package
v1.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2023 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Example (Memory)
package main

import (
	"context"
	"fmt"

	"github.com/brexhq/substation/config"
	"github.com/brexhq/substation/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/config"
	"github.com/brexhq/substation/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

This section is empty.

Functions

This section is empty.

Types

type Storer

type Storer interface {
	Get(context.Context, string) (interface{}, error)
	Set(context.Context, string, interface{}) error
	SetWithTTL(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 added in v0.8.4

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