memory

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// DefaultCleanupInterval is the default interval for cleaning up expired entries
	DefaultCleanupInterval = 10 * time.Minute
)

Variables

View Source
var (
	// Configuration errors
	ErrInvalidConfig = errors.New("memory backend requires memory.Config")
	ErrNilConfig     = errors.New("memory backend config cannot be nil")

	// Operation errors
	ErrSetFailed    = errors.New("failed to set key")
	ErrGetFailed    = errors.New("failed to get key")
	ErrDeleteFailed = errors.New("failed to delete key")
	ErrCloseFailed  = errors.New("failed to close memory backend")

	// CheckAndSet specific errors
	ErrCheckAndSetFailed = errors.New("failed to perform check-and-set operation")
	ErrInvalidValueType  = errors.New("invalid value type for check-and-set")
	ErrValueExpired      = errors.New("value has expired")

	// Internal errors
	ErrLockFailed      = errors.New("failed to acquire lock")
	ErrValueConversion = errors.New("failed to convert value to string")
)

Functions

func NewCheckAndSetFailedError added in v0.0.6

func NewCheckAndSetFailedError(key string, err error) error

CheckAndSet error functions

func NewCloseFailedError added in v0.0.6

func NewCloseFailedError(err error) error

func NewDeleteFailedError added in v0.0.6

func NewDeleteFailedError(key string, err error) error

func NewGetFailedError added in v0.0.6

func NewGetFailedError(key string, err error) error

Operation error functions

func NewInvalidConfigError added in v0.0.6

func NewInvalidConfigError(field string) error

Configuration error functions

func NewInvalidValueTypeError added in v0.0.6

func NewInvalidValueTypeError(key string, value any) error

func NewLockFailedError added in v0.0.6

func NewLockFailedError(key string, err error) error

Internal error functions

func NewNilConfigError added in v0.0.6

func NewNilConfigError() error

func NewSetFailedError added in v0.0.6

func NewSetFailedError(key string, err error) error

func NewValueConversionError added in v0.0.6

func NewValueConversionError(key string, value any, err error) error

func NewValueExpiredError added in v0.0.6

func NewValueExpiredError(key string) error

Types

type Backend added in v0.0.5

type Backend struct {
	// contains filtered or unexported fields
}

func New

func New() *Backend

New initializes a new in-memory storage instance with default (10 minutes) cleanup.

func NewWithCleanup added in v0.0.5

func NewWithCleanup(interval time.Duration) *Backend

NewWithCleanup initializes a new in-memory storage instance with custom cleanup interval. Set interval to 0 to disable automatic cleanup.

func (*Backend) CheckAndSet added in v0.0.5

func (m *Backend) CheckAndSet(ctx context.Context, key, oldValue, newValue string, expiration time.Duration) (bool, error)

CheckAndSet atomically sets key to newValue only if current value matches oldValue. This operation provides compare-and-swap (CAS) semantics for implementing optimistic locking.

Parameters:

  • ctx: Context for cancellation and timeouts
  • key: The storage key to operate on
  • oldValue: Expected current value. Use empty string "" for "set if not exists" semantics
  • newValue: New value to set if the current value matches oldValue
  • expiration: Time-to-live for the key. Use 0 for no expiration

Returns:

  • bool: true if the CAS succeeded (value was set), false if the compare failed and no write occurred
  • error: Any storage-related error (not including a compare mismatch)

Behavior:

  • If oldValue is "", the operation succeeds only if the key does not exist (or is expired)
  • If oldValue matches the current value, the key is updated to newValue
  • Expired keys are treated as non-existent for comparison purposes
  • All values are stored and compared as strings

Caller contract:

  • A (false, nil) return indicates the compare condition did not match (e.g., another writer won the race or the key already exists when using "set if not exists"). This is not an error. Callers may safely reload state and retry with backoff according to their contention policy.
  • A non-nil error indicates a storage/backend failure and should not be retried blindly.

func (*Backend) Cleanup added in v0.0.5

func (m *Backend) Cleanup()

Cleanup triggers an immediate cleanup of expired entries This method is exported for manual cleanup when needed

func (*Backend) Close added in v0.0.5

func (m *Backend) Close() error

func (*Backend) Delete added in v0.0.5

func (m *Backend) Delete(ctx context.Context, key string) error

func (*Backend) Get added in v0.0.5

func (m *Backend) Get(ctx context.Context, key string) (string, error)

func (*Backend) Set added in v0.0.5

func (m *Backend) Set(ctx context.Context, key string, value string, expiration time.Duration) error

Jump to

Keyboard shortcuts

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