keyvaluestore

package module
v0.0.0-...-a3c16e7 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: MIT Imports: 7 Imported by: 8

README

KeyValueStore Driver

This is the keyvaluestore driver package. It is used as a middleware between Redis and our service usecases. The Interface in interface.go is implemented by both the actual driver, as well as a mocked driver, to allow for a mocked keyvaluestore to be injected into tests.

Creating a Keyvaluestore

import "git.science.uu.nl/datastrophe/keyvaluestore"

keyValueStore := keyvaluestore.NewDriver()

keyValueStore.Connect()

Creating a Mock Keyvaluestore

import "git.science.uu.nl/datastrophe/keyvaluestore"

mockKeyValueStore := keyvaluestore.NewMockDriver()

Setting Key -> Value

import "git.science.uu.nl/datastrophe/keyvaluestore"

keyValueStore.Set(context.Background(), "key", "value")

Getting Value for Key

import "git.science.uu.nl/datastrophe/keyvaluestore"

keyValueStore.Get(context.Background(), "key", keyvaluestore.String)

Testing

To test this package we wrote a bash script. This script starts up a Redis Docker container and then runs the package tests on that container. Once all tests have run the Docker container is killed and deleted.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyNotFound = errors.New("key not present in kvs")

ErrKeyNotFound is self explanatory

View Source
var KeyAlreadyExists = errors.New("key already present in kvs")

Functions

This section is empty.

Types

type Driver

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

Driver implements the keyvaluestore interface

func (*Driver) Connect

func (d *Driver) Connect(address string, password string) error

Connect opens the connection to the keyvaluestore

Return: error, returns a potential error

func (*Driver) Delete

func (d *Driver) Delete(ctx context.Context, key string) error

Delete deletes the key value pair from the key value store

context: context.Context, the context in which the delete operation should take place
key: string, the key for which to delete the value from the key value store
Returns: a possible error

func (*Driver) Disconnect

func (d *Driver) Disconnect() error

Disconnect disconnects from the keyvaluestore

Return: error, returns a potential error

func (*Driver) Get

func (d *Driver) Get(ctx context.Context, key string, t Type) (value interface{}, err error)

Get gets the value for the given key

ctx: context.Context, the context in which to perform the get operation
key: string, the key for which to get the value
Returns the value retrieved from the keyvaluestore

func (*Driver) GetMap

func (d *Driver) GetMap(ctx context.Context, key string) (map[string]interface{}, error)

GetMap gets the map for the given key

ctx: context.Context, the context in which to perform the get operation
key: string, the key for which to get the value
Returns the map retrieved from the keyvaluestore

func (*Driver) Set

func (d *Driver) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error

Set sets the value for the given key

ctx: context.Context, the context in which to perform the set operation
key: string, the key for which you want to store the value
value: interface{}, the value you want to store
expiration: time.Duration, the expiration time of the value
Return: an error if something goes wrong

func (*Driver) SetForever

func (d *Driver) SetForever(ctx context.Context, key string, value interface{}) error

Set sets the value for the given key

ctx: context.Context, the context in which to perform the set operation
key: string, the key for which you want to store the value
value: interface{}, the value you want to store
Return: an error if something goes wrong

func (*Driver) SetMap

func (d *Driver) SetMap(ctx context.Context, key string, value map[string]interface{}, expiration time.Duration) error

SetMap sets the map for the given key

ctx: context.Context, the context in which to perform the set operation
key: string, the key for which you want to store the value
value: map[string]interface{}, the value you want to store
expiration: time.Duration, the expiration time of the value
Return: an error if something goes wrong

func (*Driver) SetMapWithEnvDuration

func (d *Driver) SetMapWithEnvDuration(ctx context.Context, key string, value map[string]interface{}, expirationEnvVar string, expirationFallback time.Duration) error

SetMapWithEnvDuration sets the map for the given key with the expiration time set by an environment variable

ctx: context.Context, the context in which to perform the set operation
key: string, the key for which you want to store the value
value: map[string]interface{}, the value you want to store
expirationEnvVar: string, the environment variable that contains the expiration time
expirationFallback: time.Duration, the fallback expiration time
Return: an error if something goes wrong

func (*Driver) SetWithEnvDuration

func (d *Driver) SetWithEnvDuration(ctx context.Context, key string, value interface{}, expirationEnvVar string, expirationFallback time.Duration) error

SetMapWithEnvDuration sets the map for the given key with the expiration time set by an environment variable

ctx: context.Context, the context in which to perform the set operation
key: string, the key for which you want to store the value
value: map[string]interface{}, the value you want to store
expirationEnvVar: string, the environment variable that contains the expiration time
expirationFallback: time.Duration, the fallback expiration time
Return: an error if something goes wrong

type Interface

type Interface interface {
	Connect(address string, password string) error
	GetMap(ctx context.Context, key string) (value map[string]interface{}, err error)
	Get(ctx context.Context, key string, t Type) (value interface{}, err error)
	SetForever(ctx context.Context, key string, value interface{}) error
	SetMap(ctx context.Context, key string, value map[string]interface{}, expiration time.Duration) error
	Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
	SetMapWithEnvDuration(ctx context.Context, key string, value map[string]interface{}, expirationEnvVar string, expirationFallback time.Duration) error
	SetWithEnvDuration(ctx context.Context, key string, value interface{}, expirationEnvVar string, expirationFallback time.Duration) error
	Delete(ctx context.Context, key string) error
	Disconnect() error
	// contains filtered or unexported methods
}

Interface is an interface for a key value storage

func NewDriver

func NewDriver() Interface

NewDriver creates a new keyvaluestore driver

Return: Interface, returns a new driver

func NewMockDriver

func NewMockDriver() Interface

NewMockDriver creates a key value store driver (mock)

Return: the interface for the new mock driver

type MockDriver

type MockDriver struct {
	Data map[string]interface{}
}

A MockDriver implements methods to set key-value data (mock)

func (*MockDriver) Connect

func (kvs *MockDriver) Connect(address string, password string) error

Connect connects to the keyvaluestore

Return: if there's an error

func (*MockDriver) Delete

func (kvs *MockDriver) Delete(ctx context.Context, key string) error

Delete deletes the key value pair from the key value store (mock)

ctx: context.Context, the context for the driver
key: string, the key for the driver
Return: if there's an error

func (*MockDriver) Disconnect

func (kvs *MockDriver) Disconnect() error

Disconnect disconnects from the key value store (mock)

Return: if there's an error

func (*MockDriver) Get

func (kvs *MockDriver) Get(ctx context.Context, key string, t Type) (interface{}, error)

Get gets the value for the supplied key from the key value store (mock)

ctx: context.Context, the context for the driver
key: string, the key for the driver
t: Type, the type of the driver
Return: (interface{}, error), the interface and any error that occurred

func (*MockDriver) GetMap

func (kvs *MockDriver) GetMap(ctx context.Context, key string) (map[string]interface{}, error)

func (*MockDriver) Set

func (kvs *MockDriver) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error

func (*MockDriver) SetForever

func (kvs *MockDriver) SetForever(ctx context.Context, key string, value interface{}) error

Set sets a key to a value in the key value store. Expects a non-pointer as value. (mock)

ctx: context.Context, the context for the driver
key: string, the key for the driver
value: interface{}, the value for the driver
Return: if there's an error

func (*MockDriver) SetMap

func (kvs *MockDriver) SetMap(ctx context.Context, key string, value map[string]interface{}, expiration time.Duration) error

func (*MockDriver) SetMapWithEnvDuration

func (kvs *MockDriver) SetMapWithEnvDuration(ctx context.Context, key string, value map[string]interface{}, expirationEnvVar string, expirationFallback time.Duration) error

func (*MockDriver) SetWithEnvDuration

func (kvs *MockDriver) SetWithEnvDuration(ctx context.Context, key string, value interface{}, expirationEnvVar string, expirationFallback time.Duration) error

type Type

type Type int

Type describes the return type expected from the key value store

const (
	// String is a string
	String Type = iota
	// Int is a int
	Int
	// Float32 is a float32
	Float32
	// Float64 is a float64
	Float64
	// Bool is a bool
	Bool
	// Bytes is a byte array
	Bytes
)

Jump to

Keyboard shortcuts

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