memory

package module
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 2 Imported by: 0

README

ligo-memory

A simple in-memory store for Ligo, designed for fast testing and local development without external dependencies.

Go Version License Tests No dependencies

Install

go get github.com/linkeunid/ligo-memory

Quick start

Register a typed store in your module and inject it into a constructor:

import (
    "github.com/linkeunid/ligo"
    memory "github.com/linkeunid/ligo-memory"
)

func UserModule() ligo.Module {
    return ligo.NewModule("user",
        ligo.Providers(
            memory.Provider[string, *User](),
            ligo.Factory[UserRepository](NewUserRepository),
        ),
    )
}

func NewUserRepository(store *memory.Store[string, *User]) UserRepository {
    return &userRepo{store: store}
}

For a zero-config drop-in, use memory.Module() which registers a *Store[string, any]:

app.Register(memory.Module(), myModule())

See also

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

func Module() ligo.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]) Clear

func (s *Store[K, V]) Clear()

Clear removes all entries from the store.

func (*Store[K, V]) Delete

func (s *Store[K, V]) Delete(key K) bool

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

func (s *Store[K, V]) Get(key K) (V, bool)

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.

func (*Store[K, V]) Len

func (s *Store[K, V]) Len() int

Len returns the number of entries currently in the store.

func (*Store[K, V]) Set

func (s *Store[K, V]) Set(key K, value V)

Set stores value under key, overwriting any existing entry.

Jump to

Keyboard shortcuts

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