Documentation
¶
Overview ¶
Package memory provides a simple, thread-safe in-memory key-value store for the Ligo framework, designed for fast testing and local development without any external dependencies.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Module ¶
Module returns a Ligo module that registers a *Store[string, any] as a singleton provider via DI. It is a convenient zero-config drop-in for applications that only need a single untyped key-value store.
For typed stores (the common case), declare Provider[K, V]() directly inside your own module's ligo.Providers list instead.
app.Register(memory.Module(), myModule())
func Provider ¶
func Provider[K comparable, V any]() ligo.Provider
Provider returns a ligo.Provider that registers a *Store[K, V] as a singleton in the DI container. Other factories declared in the same module can then receive the store as an injected parameter.
Use this inside your module's ligo.Providers list:
func User() ligo.Module {
return ligo.NewModule("user",
ligo.Providers(
memory.Provider[string, *entity.User](),
ligo.Factory[repository.UserRepository](NewUserRepository),
),
)
}
Accept the store in your constructor:
func NewUserRepository(store *memory.Store[string, *entity.User]) repository.UserRepository {
return &UserRepository{store: store}
}
Types ¶
type Store ¶
type Store[K comparable, V any] struct { // contains filtered or unexported fields }
Store is a generic, thread-safe in-memory key-value store.
K must be a comparable type (e.g. string, int, UUID alias). V can be any type, typically a pointer to a domain entity. All methods are safe for concurrent use.
Example:
store := memory.New[string, *entity.User]()
store.Set("u1", &entity.User{ID: "u1", Name: "Alice"})
user, ok := store.Get("u1")
users := store.All()
func New ¶
func New[K comparable, V any]() *Store[K, V]
New creates a new empty Store.
Example:
store := memory.New[string, *entity.User]()
func (*Store[K, V]) All ¶
func (s *Store[K, V]) All() []V
All returns a snapshot of all stored values as a slice. The order of elements is not guaranteed.
func (*Store[K, V]) Delete ¶
Delete removes the entry for key. Returns true if the key existed and was removed, false if it was not found.
func (*Store[K, V]) Get ¶
Get retrieves the value associated with key. Returns the value and true if found, the zero value and false otherwise.
func (*Store[K, V]) Keys ¶
func (s *Store[K, V]) Keys() []K
Keys returns a snapshot of all stored keys as a slice. The order of elements is not guaranteed.