memory

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ModelRepository

type ModelRepository[Model model.Model[ID], ID model.ID] struct {
	// contains filtered or unexported fields
}

ModelRepository is thread-safe in-memory model repository. Useful for testing.

func NewModelRepository

func NewModelRepository[Model model.Model[ID], ID model.ID](opts ...ModelRepositoryOption) *ModelRepository[Model, ID]

NewModelReository returns an in-memory model repository.

func (*ModelRepository[Model, ID]) Delete

func (r *ModelRepository[Model, ID]) Delete(ctx context.Context, m Model) error

Delete deletes the given model from the repository.

func (*ModelRepository[Model, ID]) Fetch

func (r *ModelRepository[Model, ID]) Fetch(ctx context.Context, id ID) (Model, error)

Fetch returns the model from the repository if its exists. Otherwise model.ErrNotFound is returned. If the model is currently in use by r.Use(), Fetch waits for the model to become free. If ctx is canceled before the model becomes free, ctx.Err() is returned.

func (*ModelRepository[Model, ID]) Models

func (r *ModelRepository[Model, ID]) Models() map[ID]Model

Models returns all models in the repository.

func (*ModelRepository[Model, ID]) Save

func (r *ModelRepository[Model, ID]) Save(ctx context.Context, m Model) error

Save saves the model to the repository.

func (*ModelRepository[Model, ID]) Use

func (r *ModelRepository[Model, ID]) Use(ctx context.Context, id ID, fn func(Model) error) error

Use fetches the given model from the repository, calls the provided function with it, and finally saves the model back to the repository. Use ensures that a single model is not operated on by multiple goroutines at the same time to avoid optimistic concurrency issues.

type ModelRepositoryOption

type ModelRepositoryOption func(*modelRepositoryOptions)

ModelRepositoryOption is an option for a model repository.

func ModelFactory

func ModelFactory[Model model.Model[ID], ID model.ID](factory func(ID) Model) ModelRepositoryOption

ModelFactory returns a ModelRepositoryOption that provides a factory function for the models to a model repository. The repository will use the function to create the model if it doesn't exist in the repository instead of returning model.ErrNotFound.

Jump to

Keyboard shortcuts

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