storage

package
v0.0.0-...-f523dad Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package storage implements flexible storage backends with interface abstractions.

Package storage implements SQLite storage provider.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoTTL = errors.New("no TTL set")

ErrNoTTL is returned when no TTL is set for a key

Functions

This section is empty.

Types

type CompositeProvider

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

CompositeProvider combines multiple storage providers

func NewCompositeProvider

func NewCompositeProvider(primary Provider, fallback Provider) *CompositeProvider

NewCompositeProvider creates a new composite storage provider

func (*CompositeProvider) Clear

func (c *CompositeProvider) Clear(ctx context.Context) error

Clear implements Provider.Clear

func (*CompositeProvider) Close

func (c *CompositeProvider) Close(ctx context.Context) error

Close implements Provider.Close

func (*CompositeProvider) Delete

func (c *CompositeProvider) Delete(ctx context.Context, key string) error

Delete implements Provider.Delete

func (*CompositeProvider) Exists

func (c *CompositeProvider) Exists(ctx context.Context, key string) (bool, error)

Exists implements Provider.Exists

func (*CompositeProvider) GetStats

func (c *CompositeProvider) GetStats(ctx context.Context) (*Stats, error)

GetStats implements Provider.GetStats

func (*CompositeProvider) GetTTL

func (c *CompositeProvider) GetTTL(ctx context.Context, key string) (*time.Duration, error)

GetTTL implements Provider.GetTTL

func (*CompositeProvider) Load

func (c *CompositeProvider) Load(ctx context.Context, key string) (interface{}, error)

Load implements Provider.Load with fallback

func (*CompositeProvider) Save

func (c *CompositeProvider) Save(ctx context.Context, key string, value interface{}, ttl *time.Duration) error

Save implements Provider.Save with fallback

func (*CompositeProvider) SetTTL

func (c *CompositeProvider) SetTTL(ctx context.Context, key string, ttl time.Duration) error

SetTTL implements Provider.SetTTL

type Entry

type Entry struct {
	Key       string                 `json:"key"`
	Value     interface{}            `json:"value"`
	CreatedAt time.Time              `json:"created_at"`
	ExpiresAt *time.Time             `json:"expires_at,omitempty"`
	Size      int64                  `json:"size_bytes"`
	Metadata  map[string]interface{} `json:"metadata,omitempty"`
}

Entry represents a stored value with metadata

type Error

type Error struct {
	Op      string `json:"operation"`
	Key     string `json:"key"`
	Message string `json:"message"`
	Code    string `json:"error_code"`
}

Error represents storage-specific errors

func (Error) Error

func (e Error) Error() string

type Event

type Event struct {
	Type      EventType   `json:"type"`
	Key       string      `json:"key"`
	Timestamp time.Time   `json:"timestamp"`
	Data      interface{} `json:"data,omitempty"`
}

Event represents storage lifecycle events

type EventHandler

type EventHandler func(event Event)

EventHandler handles storage events

type EventType

type EventType string

EventType defines the type of storage event

const (
	EventCreated EventType = "created"
	EventUpdated EventType = "updated"
	EventDeleted EventType = "deleted"
	EventExpired EventType = "expired"
	EventCleared EventType = "cleared"
)

Event type constants

type Manager

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

Manager manages multiple storage providers

func NewManager

func NewManager() *Manager

NewManager creates a new storage manager

func (*Manager) CloseAll

func (sm *Manager) CloseAll(ctx context.Context) error

CloseAll closes all storage providers

func (*Manager) CreateProvider

func (sm *Manager) CreateProvider(ctx context.Context, name string, factoryName string, options Options) error

CreateProvider creates a storage provider using a registered factory

func (*Manager) GetProvider

func (sm *Manager) GetProvider(name string) (Provider, error)

GetProvider returns a storage provider by name GetProvider returns a provider by name

func (*Manager) GetStats

func (sm *Manager) GetStats(ctx context.Context) (map[string]*Stats, error)

GetStats returns combined stats from all providers

func (*Manager) ListProviders

func (sm *Manager) ListProviders() []string

ListProviders returns a list of all provider names

func (*Manager) RegisterFactory

func (sm *Manager) RegisterFactory(name string, factory ProviderFactory)

RegisterFactory registers a storage provider factory

func (*Manager) RemoveProvider

func (sm *Manager) RemoveProvider(ctx context.Context, name string) error

RemoveProvider removes a storage provider

type Options

type Options struct {
	DefaultTTL      *time.Duration         `json:"default_ttl"`
	MaxKeys         int64                  `json:"max_keys"`
	MaxSize         int64                  `json:"max_size_bytes"`
	CleanupInterval time.Duration          `json:"cleanup_interval"`
	Compression     bool                   `json:"compression"`
	Encryption      bool                   `json:"encryption"`
	Metadata        map[string]interface{} `json:"metadata,omitempty"`
}

Options configures storage behavior

type Provider

type Provider interface {
	// Save stores a value with an optional TTL
	Save(ctx context.Context, key string, value interface{}, ttl *time.Duration) error

	// Load retrieves a value by key
	Load(ctx context.Context, key string) (interface{}, error)

	// Delete removes a value by key
	Delete(ctx context.Context, key string) error

	// Exists checks if a key exists
	Exists(ctx context.Context, key string) (bool, error)

	// GetTTL returns the remaining TTL for a key
	GetTTL(ctx context.Context, key string) (*time.Duration, error)

	// SetTTL updates the TTL for a key
	SetTTL(ctx context.Context, key string, ttl time.Duration) error

	// Clear removes all data from storage
	Clear(ctx context.Context) error

	// GetStats returns storage statistics
	GetStats(ctx context.Context) (*Stats, error)

	// Close closes the storage provider
	Close(ctx context.Context) error
}

Provider defines the interface for storage backends.

type ProviderFactory

type ProviderFactory interface {
	// Create creates a new storage provider with the given options
	Create(ctx context.Context, options Options) (Provider, error)

	// GetType returns the type of storage provider this factory creates
	GetType() string
}

ProviderFactory creates storage providers

type SQLiteStorage

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

SQLiteStorage implements Provider using SQLite

func NewSQLiteStorage

func NewSQLiteStorage(dbPath string, options Options) (*SQLiteStorage, error)

NewSQLiteStorage creates a new SQLite storage provider

func (*SQLiteStorage) AddEventHandler

func (s *SQLiteStorage) AddEventHandler(handler EventHandler)

AddEventHandler adds an event handler

func (*SQLiteStorage) Clear

func (s *SQLiteStorage) Clear(ctx context.Context) error

Clear removes all data from storage

func (*SQLiteStorage) Close

func (s *SQLiteStorage) Close(_ context.Context) error

Close closes the storage provider

func (*SQLiteStorage) Delete

func (s *SQLiteStorage) Delete(ctx context.Context, key string) error

Delete removes a value by key

func (*SQLiteStorage) Exists

func (s *SQLiteStorage) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists

func (*SQLiteStorage) GetStats

func (s *SQLiteStorage) GetStats(ctx context.Context) (*Stats, error)

GetStats returns storage statistics

func (*SQLiteStorage) GetTTL

func (s *SQLiteStorage) GetTTL(ctx context.Context, key string) (*time.Duration, error)

GetTTL returns the remaining TTL for a key

func (*SQLiteStorage) Load

func (s *SQLiteStorage) Load(ctx context.Context, key string) (interface{}, error)

Load retrieves a value by key

func (*SQLiteStorage) Save

func (s *SQLiteStorage) Save(ctx context.Context, key string, value interface{}, ttl *time.Duration) error

Save stores a value with optional TTL

func (*SQLiteStorage) SetTTL

func (s *SQLiteStorage) SetTTL(ctx context.Context, key string, ttl time.Duration) error

SetTTL updates the TTL for a key

type SQLiteStorageEntry

type SQLiteStorageEntry struct {
	Key       string     `json:"key"`
	Value     []byte     `json:"value"`
	CreatedAt time.Time  `json:"created_at"`
	ExpiresAt *time.Time `json:"expires_at,omitempty"`
	Size      int64      `json:"size_bytes"`
	Metadata  []byte     `json:"metadata,omitempty"`
}

SQLiteStorageEntry represents a storage entry in SQLite

type SQLiteStorageFactory

type SQLiteStorageFactory struct{}

SQLiteStorageFactory implements StorageProviderFactory

func NewSQLiteStorageFactory

func NewSQLiteStorageFactory() *SQLiteStorageFactory

NewSQLiteStorageFactory creates a new SQLite storage factory

func (*SQLiteStorageFactory) Create

func (f *SQLiteStorageFactory) Create(_ context.Context, options Options) (Provider, error)

Create creates a new SQLite storage provider Create creates a new sqlite provider from options

func (*SQLiteStorageFactory) GetType

func (f *SQLiteStorageFactory) GetType() string

GetType returns the type of storage provider

type Stats

type Stats struct {
	TotalKeys    int64     `json:"total_keys"`
	TotalSize    int64     `json:"total_size_bytes"`
	ExpiredKeys  int64     `json:"expired_keys"`
	MemoryUsage  int64     `json:"memory_usage_bytes"`
	LastCleanup  time.Time `json:"last_cleanup"`
	ProviderType string    `json:"provider_type"`
}

Stats provides information about storage usage

Jump to

Keyboard shortcuts

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