mem

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

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

Go to latest
Published: Jul 10, 2019 License: MIT Imports: 7 Imported by: 0

README

gokv/mem

GoDoc Build Status

An in-memory key-value store implementing the github.com/gokv/store Store interface.

The focus is on readability and simplicity.

Usage

func main() {
	s := mem.New()
	defer s.Close() // close the mem.Store to avoid leaking goroutines

	err := s.SetWithTimeout(context.Background(), "key", Value{p:1}, timeout)
	if err != nil {
		panic(err)
	}

	var v Value // Value is a type that implements json.Marshaler/Unmarshaler
	ok, err := s.Get(context.Background(), "key", &v)

	if err != nil {
		panic(err)
	}

	if !ok {
		panic(errors.New("Value not found!"))
	}

	fmt.Println("The retrieved value is %q", v)
}

Documentation

Overview

Package mem defines Store, an in-memory key-value store that implements the Store interface defined in "github.com/gokv/store".

Index

Constants

This section is empty.

Variables

View Source
var ErrKeyExists = errors.New("the key already exists")

ErrKeyExists is returned when the Add method generates a non-unique ID.

Functions

This section is empty.

Types

type Store

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

Store implements an in-memory key-value store. It is implemented as a Go map and protected by a mutex. The zero value is not ready to use: initialise with New.

Store is safe for concurrent use.

func New

func New() *Store

New initialises the map underlying Store.

func (*Store) Add

func (s *Store) Add(ctx context.Context, v json.Marshaler) (string, error)

Add persists a new object and returns its unique UUIDv4 key. Err is non-nil in case of failure.

func (*Store) Cleanup

func (s *Store) Cleanup(ctx context.Context)

func (*Store) Close

func (s *Store) Close() error

Close releases the resources associated with the Store.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, k string) (bool, error)

Delete removes the corresponding entry if present. Returns a non-nil error if the key is not known or if the context is Done.

func (*Store) Get

func (s *Store) Get(ctx context.Context, k string, v json.Unmarshaler) (bool, error)

Get returns the value corresponding the key, and a nil error. If no match is found, returns (false, nil).

func (*Store) GetAll

func (s *Store) GetAll(ctx context.Context, c store.Collection) error

GetAll returns all values. Error is non-nil if the context is Done.

func (*Store) Ping

func (s *Store) Ping(ctx context.Context) error

Ping always returns nil if the context is not Done.

func (*Store) Set

func (s *Store) Set(ctx context.Context, k string, v json.Marshaler) error

Set assigns the given value to the given key, possibly overwriting. The returned error is not nil if the context is Done.

func (*Store) SetWithDeadline

func (s *Store) SetWithDeadline(ctx context.Context, k string, v json.Marshaler, deadline time.Time) error

SetWithDeadline assigns the given value to the given key, possibly overwriting. The assigned key will clear after deadline.

func (*Store) SetWithTimeout

func (s *Store) SetWithTimeout(ctx context.Context, k string, v json.Marshaler, timeout time.Duration) error

SetWithTimeout assigns the given value to the given key, possibly overwriting. The assigned key will clear after timeout. The lifespan starts when this function is called.

Jump to

Keyboard shortcuts

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