store

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package store provides in-memory storage implementation for the cache.

Package store provides a configurable caching store implementation

Package store provides interfaces and implementations for cache storage backends.

Index

Constants

View Source
const (
	DefaultMaxSize       = 10000
	DefaultMaxMemory     = int64(100 * 1024 * 1024) // 100MB
	DefaultMaxBatchSize  = 100
	DefaultMaxConcurrent = 10
)

Default values for store options

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry[V any] struct {
	Value       V         `json:"value"`
	Expires     time.Time `json:"expires"`
	CreatedAt   time.Time `json:"createdAt"`
	LastAccess  time.Time `json:"lastAccess"`
	AccessCount int64     `json:"accessCount"`
	Size        int64     `json:"size"` // Size of the entry in bytes
}

Entry represents a stored value with metadata. This is a public type that can be used by cache implementations.

type FileConfig

type FileConfig struct {
	// Directory is the base directory for storing files
	Directory string

	// FileExtension is the extension for data files
	FileExtension string

	// CompressionEnabled enables gzip compression
	CompressionEnabled bool

	// CompressionLevel sets the gzip compression level (1-9)
	CompressionLevel int

	// CleanupInterval is the interval for cleaning up expired files
	CleanupInterval time.Duration
}

FileConfig holds file-based storage configuration

func DefaultFileConfig

func DefaultFileConfig() *FileConfig

DefaultFileConfig returns a FileConfig with sensible defaults

type Option

type Option func(*Options) error

Option is a function that configures store options

func WithMaxMemory

func WithMaxMemory(maxMemory int64) Option

WithMaxMemory sets the maximum memory usage in bytes

func WithMaxSize

func WithMaxSize(size int) Option

WithMaxSize sets the maximum size of the store

func WithMemoryTracking

func WithMemoryTracking(enable bool) Option

WithMemoryTracking enables memory usage tracking

func WithTTLConfig

func WithTTLConfig(config ttl.Config) Option

WithTTLConfig sets the TTL configuration

type Options

type Options struct {
	// MaxSize is the maximum number of items the store can hold
	MaxSize int

	// TTLConfig is the configuration for TTL behavior
	TTLConfig ttl.Config

	// MaxMemory is the maximum memory usage in bytes (0 means unlimited)
	MaxMemory int64

	// EnableMemoryTracking enables memory usage tracking
	EnableMemoryTracking bool
}

Options represents store configuration options

func NewOptions

func NewOptions() *Options

NewOptions creates a new Options instance with default values

func (*Options) Apply

func (o *Options) Apply(opts ...Option) error

Apply applies the given options to the Options struct

type Stats

type Stats struct {
	Hits            atomic.Int64
	Misses          atomic.Int64
	Sets            atomic.Int64
	Deletes         atomic.Int64
	Clears          atomic.Int64
	Errors          atomic.Int64
	Size            atomic.Int64
	Capacity        atomic.Int64
	Evictions       atomic.Int64
	BatchOperations atomic.Int64
	BatchErrors     atomic.Int64
	BatchSuccess    atomic.Int64
}

Stats tracks cache statistics

type Store

type Store[K comparable, V any] interface {
	// Get retrieves a value from the store
	Get(ctx context.Context, key K) (V, bool)

	// Set stores a value in the store
	Set(ctx context.Context, key K, value V, ttl time.Duration) error

	// Delete removes a value from the store
	Delete(ctx context.Context, key K) error

	// Clear removes all values from the store
	Clear(ctx context.Context) error

	// Size returns the number of items in the store
	Size(ctx context.Context) int

	// Capacity returns the maximum number of items the store can hold
	Capacity(ctx context.Context) int

	// Keys returns all keys in the store
	Keys(ctx context.Context) []K

	// MemoryUsage returns the approximate memory usage in bytes
	MemoryUsage(ctx context.Context) int64

	// MaxMemory returns the maximum allowed memory usage in bytes
	MaxMemory(ctx context.Context) int64

	// Batch operations
	GetMany(ctx context.Context, keys []K) map[K]V
	SetMany(ctx context.Context, entries map[K]V, ttl time.Duration) error
	DeleteMany(ctx context.Context, keys []K) error

	// Close releases any resources used by the store
	Close(ctx context.Context) error
}

Store defines the interface for cache storage backends. This is the public interface that cache implementations can use.

func NewFileStore

func NewFileStore[K comparable, V any](ctx context.Context, config *FileConfig, opts ...Option) (Store[K, V], error)

NewFileStore creates a new file-based store

func NewMemoryStore

func NewMemoryStore[K comparable, V any](ctx context.Context, opts ...Option) (Store[K, V], error)

NewMemoryStore creates a new memory store

Directories

Path Synopsis
Package helper provides utility functions for store implementations
Package helper provides utility functions for store implementations

Jump to

Keyboard shortcuts

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