datastore

package
v0.0.0-...-cbc9d59 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultTtl is the default time-to-live for all datastore implementations
	DefaultTtl = (8 * time.Hour) + (30 * time.Minute)
	// DefaultKeyPrefix is the key prefix used by datastore implementations
	// when one is not manually configured.
	DefaultKeyPrefix = "lavamonster"
)

Variables

This section is empty.

Functions

func CompressS2

func CompressS2(data []byte) []byte

CompressS2 compresses a byte slice using S2, an "improved" Snappy algorithm.

func CompressSnappy

func CompressSnappy(data []byte) []byte

CompressSnappy compresses a byte slice using standard Snappy compression.

func DecodeData

func DecodeData[T any](data []byte) (*T, error)

DecodeData decodes gob-encoded bytes, returning the original data.

func DecompressS2

func DecompressS2(data []byte) ([]byte, error)

DecompressS2 decompresses bytes which were compressed using the S2 Snappy algorithm.

func DecompressSnappy

func DecompressSnappy(data []byte) ([]byte, error)

DecompressSnappy decompresses bytes which were compressed using standard Snappy compression.

func EncodeData

func EncodeData[T any](data T) ([]byte, error)

EncodeData encodes arbitrary data using encoding/gob, returning the byte-encoded result of gob.Encode.

Types

type Config

type Config struct {
	KeyPrefix  *string               `json:"key_prefix,omitempty" yaml:"key_prefix,omitempty" toml:"KeyPrefix,omitempty"`
	DefaultTtl *time.Duration        `json:"default_ttl,omitempty" yaml:"default_ttl,omitempty" toml:"DefaultTtl,omitempty"`
	Redis      *RedisDatastoreConfig `json:"redis,omitempty" yaml:"redis,omitempty" toml:"Redis,omitempty"`
}

type Datastore

type Datastore[T any] interface {
	// Exists checks if a key currently exists in the datastore.
	// Should return true if the key exists, false otherwise.
	// If the underlying datastore implementation returns an error
	// when checking key existence, Exists should return `false` and the
	// aforementioned error.
	Exists(ctx context.Context, key string) (bool, error)
	// CheckTtl returns the remaining time-to-live of data in the datastore
	// for the passed key.
	// If no data is set to the passed key, the zero-value of time.Duration is
	// returned, as well as false.
	// If the underlying datastore implementation returns an error,
	// that error will be returned.
	CheckTtl(ctx context.Context, key string) (time.Duration, bool, error)
	// Get returns the data which is stored with the passed key,
	// as well as a boolean representing whether the key exists
	// in the datastore at all.
	Get(ctx context.Context, key string) (*T, bool, error)
	// Insert inserts data using a given key into the datastore,
	// returning any error returned by the underlying datastore implementation.
	Insert(ctx context.Context, key string, data T) error
	// Delete removes a given key from the datastore, returning any error
	// returned by the underlying datastore implementation.
	// Note that this is a "blind" delete: if the passed key does not exist,
	// no error is returned, and no data is otherwise modified.
	Delete(ctx context.Context, key string) error
	// UpdateTtl sets the time-to-live for an object in the datastore which
	// is stored at the passed key.
	// If the passed key is unset, false is returned; otherwise, true is returned.
	UpdateTtl(ctx context.Context, key string, newTtl time.Duration) (bool, error)
	// DefaultTtl returns the time-to-live value used by default by the datastore
	// when storing new data.
	DefaultTtl() time.Duration
	// SetDefaultTtl sets the default time-to-live used by the datastore
	// when storing new data.
	SetDefaultTtl(newTtl time.Duration)
	// KeyPrefix returns the configured key prefixed used by the datastore.
	// Key prefixing should be used by all Datastore implementations
	// to ensure data consistency.
	KeyPrefix() string
	// SetKeyPrefix sets the key prefix used by the datastore
	// when storing new data.
	// Note that setting a new key prefix will *NOT* update previously stored
	// keys, essentially making data using the old key prefix inaccessible
	// to the datastore.
	SetKeyPrefix(newPrefix string)
}

func NewDatastore

func NewDatastore[T any](conf *Config) (Datastore[T], error)

func NewInMemoryDatastore

func NewInMemoryDatastore[T any](conf *Config) (Datastore[T], error)

func NewRedisDatastore

func NewRedisDatastore[T any](conf *Config) (Datastore[T], error)

NewRedisDatastore wraps NewRedisDatastoreContext, passing a new context.Background() context.

func NewRedisDatastoreContext

func NewRedisDatastoreContext[T any](ctx context.Context, conf *Config) (Datastore[T], error)

NewRedisDatastoreContext returns a Datastore implementation which uses Redis as the underlying datastore.

type RedisDatastoreConfig

type RedisDatastoreConfig struct {
	Host       *string        `json:"host,omitempty" yaml:"host,omitempty" toml:"Host,omitempty"`
	Port       *int           `json:"port,omitempty" yaml:"port,omitempty" toml:"Port,omitempty"`
	Password   *string        `json:"password,omitempty" yaml:"password,omitempty" toml:"Password,omitempty"`
	Prefix     *string        `json:"prefix,omitempty" yaml:"prefix,omitempty" toml:"Prefix,omitempty"`
	DefaultTtl *time.Duration `json:"default_ttl,omitempty" yaml:"default_ttl,omitempty" toml:"DefaultTTL,omitempty"`
}

Jump to

Keyboard shortcuts

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