memstore

package module
v0.0.0-...-81a309e Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2024 License: MIT Imports: 4 Imported by: 1

README

memstore

This repository provides an implementation of an in-memory storage that supports IsDirty/Save methods, as well as a standardized encapsulation of CacheKey. Based on this storage model, the repository also implements a set of "resource management" interfaces for managing resources efficiently.

feature

In-Memory Storage

This repository provides a lightweight in-memory storage implementation that supports key-value storage and provides IsDirty/Save methods to easily determine if the cache has expired and manually save data. This storage method can effectively improve the performance of the application in cases where memory is limited.

CacheKey Encapsulation

CacheKey is a commonly used concept, and this repository provides a standardized encapsulation of CacheKey to facilitate the management and maintenance of CacheKey, avoiding data errors and performance degradation caused by mixed-up CacheKeys.

Resource Management

Based on the in-memory storage and CacheKey encapsulation, this repository implements a set of "resource management" interfaces that provide common operations such as loading, saving, and deleting resources, and also supports multiple data types for storage and management, making it easy to develop and maintain applications.

We welcome everyone to use and contribute to the code!

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUserNotFound is returned when a user is not found
	ErrUserNotFound = fmt.Errorf("user not found")

	// ErrInvalidInput is returned when the input is invalid
	ErrInvalidInput = fmt.Errorf("invalid input")
	// ErrInvalidUser is returned when the user is invalid
	ErrInvalidUser = fmt.Errorf("invalid user")
	// ErrStatusError is returned when the status is invalid
	ErrStatusError = fmt.Errorf("status error")
)

Functions

This section is empty.

Types

type DataMap

type DataMap[T any] map[string]T

DataMap is a map that maps a resource's saving name to the resource

type Dumper

type Dumper[T any] interface {
	// Dump dumps memory data to a permanent storage
	Dump(ctx context.Context, permanentKey string, data map[UID]DataMap[T]) error
	// Load loads data from a permanent storage to memory
	Load(ctx context.Context, permanentKey string, out *map[UID]DataMap[T]) error
}

Dumper is a function that dumps memory data to a permanent storage, or loads data from a permanent storage to memory

type InMemoryStorage

type InMemoryStorage[TData StorableType] struct {
	// PersistentKey is the permanent key of the storage
	PersistentKey string

	// Dumper is a function that dumps memory data to a permanent storage,
	Dumper Dumper[TData]
	// contains filtered or unexported fields
}

InMemoryStorage is an in-memory implementation of Storage

func NewInMemoryStorage

func NewInMemoryStorage[TData StorableType](persistentKey string) *InMemoryStorage[TData]

NewInMemoryStorage creates a new instance of InMemoryResourceStorage

func (*InMemoryStorage[TData]) Delete

func (s *InMemoryStorage[TData]) Delete(user string, storeName string) error

Delete deletes a resource for a given user

func (*InMemoryStorage[TData]) Get

func (s *InMemoryStorage[TData]) Get(user string, out *TData) error

Get retrieves a resource for a given user

func (*InMemoryStorage[TData]) IsDirty

func (s *InMemoryStorage[TData]) IsDirty() bool

IsDirty returns true if the storage has been modified since

func (*InMemoryStorage[TData]) List

func (s *InMemoryStorage[TData]) List(user UID) ([]string, error)

List retrieves all resources' StoreName() for a given user

func (*InMemoryStorage[TData]) Load

func (s *InMemoryStorage[TData]) Load(ctx context.Context) error

Load loads the storage from permanent storage

func (*InMemoryStorage[TData]) Save

func (s *InMemoryStorage[TData]) Save(ctx context.Context) error

Save persists the storage to permanent storage if the storage is not dirty, this function does nothing

func (*InMemoryStorage[TData]) Set

func (s *InMemoryStorage[TData]) Set(user string, in *TData) error

Set stores a resource for a given user

func (*InMemoryStorage[TData]) Update

func (s *InMemoryStorage[TData]) Update(user string, storeName string, updateFn func(*TData) (*TData, error)) error

Update updates a resource for a given user

type SavingFunc

type SavingFunc[T any] func(storageName string, data DataMap[T]) error

SavingFunc is a function that saves the storage to permanent storage

type StorableType

type StorableType interface {
	// StoreName the name in the users' storage, must be unique in a storage
	// only used for set/get in the users data, not for persisting usage (save/load)
	StoreName() string
}

StorableType is an interface that all types that can be stored must implement

type Storage

type Storage[DataType StorableType] interface {
	// Get retrieves a resource for a given user
	Get(user string, out *DataType) error
	// List retrieves all resources' StoreName() for a given user
	List(user string) ([]string, error)

	// Set sets a resource for a given user
	Set(user string, in *DataType) error

	// Update updates a resource for a given user, using the updateFn
	// to ensure that the resource is updated atomically (CAS)
	Update(user string, storeName string, updateFn func(org *DataType) (updated *DataType, err error)) error
	// Delete deletes a resource for a given user
	Delete(user string, storeName string) error

	// IsDirty returns true if the storage has been modified since
	IsDirty() bool

	// Save persists the storage to permanent storage
	Save(ctx context.Context) error

	// Load loads the storage from permanent storage
	Load(ctx context.Context) error
}

Storage is an interface that all storage implementations must implement

type UID

type UID = string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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