Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Storage ¶
type Storage interface {
// Save writes a new value for a given key.
// It includes the oldValue for potential optimistic locking or Compare-And-Swap (CAS) like operations,
// allowing implementations to prevent overwrites if the value has changed since it was last read.
Save(key string, newValue []byte, oldValue []byte) error
// Load retrieves the value associated with a key.
// Returns an error if the key does not exist.
Load(key string) ([]byte, error)
// Delete removes a key-value pair from the storage.
Delete(key string) error
// Close releases any resources held by the storage backend.
Close() error
}
Storage defines the interface for persistent storage backends. Implementations must ensure thread-safety where applicable for their storage mechanisms.
type Var ¶
type Var[T any] struct { // contains filtered or unexported fields }
Var is a generic variable that persists its value to storage.
func NewVar ¶
func NewVar[T any](m *VarManager, key string, defaultValue T) (*Var[T], error)
NewVar creates or retrieves a managed persistent variable. If a Var with the given key already exists, the existing instance is returned. Otherwise, a new Var is initialized with defaultValue.
Important: For the same key, NewVar always returns the identical *Var[T] instance.
func (*Var[T]) SetLazy ¶
func (v *Var[T]) SetLazy(newValue T)
SetLazy updates the value in memory only. The change will be written to storage during the next Sync or background save.
func (*Var[T]) Update ¶ added in v0.3.0
Update atomically modifies the value and immediately writes to storage if changed. Returns the new value and any storage error.
func (*Var[T]) UpdateLazy ¶ added in v0.3.0
UpdateLazy atomically modifies the value in memory using the transform function. Returns the new value. If 'changed' is false, no update occurs.
type VarManager ¶
type VarManager struct {
// contains filtered or unexported fields
}
VarManager manages a collection of persistent variables, providing lifecycle and synchronization mechanisms.
func NewVarManager ¶
func NewVarManager(storage Storage) *VarManager
NewVarManager creates a new manager for persistent variables, using the provided storage backend.
func (*VarManager) AutoSync ¶
func (m *VarManager) AutoSync(ctx context.Context, interval time.Duration)
AutoSync starts a goroutine that periodically saves all lazy variables to storage. It returns immediately if AutoSync is already running.
func (*VarManager) Close ¶
func (m *VarManager) Close() error
Close gracefully shuts down the manager, ensuring all pending changes are written to storage and stopping any active AutoSync routine.
func (*VarManager) StopAutoSync ¶
func (m *VarManager) StopAutoSync()
StopAutoSync halts the automatic synchronization process.
func (*VarManager) Sync ¶
func (m *VarManager) Sync() error
Sync forces all managed variables to write their pending changes to storage immediately.