cache

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: May 26, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Get retrieves a value from the cache
	Get(key string) (interface{}, bool)

	// Set adds a value to the cache with the given TTL
	Set(key string, value interface{}, ttl time.Duration)

	// Delete removes a value from the cache
	Delete(key string)

	// Clear removes all items from the cache
	Clear()

	// Prefetch queues a key for background prefetching
	Prefetch(key string)

	// Close closes the cache
	Close() error

	// GetMetrics returns cache metrics
	GetMetrics() *Metrics
}

Cache is the interface for cache implementations

func NewBadgerCache

func NewBadgerCache(opts *Options) (Cache, error)

NewBadgerCache creates a new BadgerDB-backed cache

func NewBoltCache

func NewBoltCache(opts *Options) (Cache, error)

NewBoltCache creates a new BoltDB-backed cache

func NewCache

func NewCache(cacheType CacheType, opts *Options) (Cache, error)

NewCache creates a new cache of the specified type

func NewMemoryCache

func NewMemoryCache(opts *Options) Cache

NewMemoryCache creates a new in-memory cache

func NewNullCache

func NewNullCache() Cache

NewNullCache creates a new null cache

type CacheType

type CacheType string

CacheType represents the type of cache

const (
	// MemoryCacheType is an in-memory cache
	MemoryCacheType CacheType = "memory"

	// BadgerCacheType is a BadgerDB-backed cache
	BadgerCacheType CacheType = "badger"

	// BoltCacheType is a BoltDB-backed cache
	BoltCacheType CacheType = "bolt"

	// NullCacheType is a no-op cache
	NullCacheType CacheType = "null"
)

type InvalidationAction

type InvalidationAction string

InvalidationAction is the action to take when invalidating cache items

const (
	// InvalidateDelete deletes matching items
	InvalidateDelete InvalidationAction = "delete"

	// InvalidateRefresh refreshes matching items
	InvalidateRefresh InvalidationAction = "refresh"

	// InvalidateExpire expires matching items
	InvalidateExpire InvalidationAction = "expire"
)

type InvalidationCondition

type InvalidationCondition string

InvalidationCondition is the condition for invalidation

const (
	// OnWrite invalidates on write operations
	OnWrite InvalidationCondition = "write"

	// OnRead invalidates on read operations
	OnRead InvalidationCondition = "read"

	// OnTime invalidates based on time
	OnTime InvalidationCondition = "time"
)

type InvalidationPattern

type InvalidationPattern struct {
	// Pattern is the pattern to match cache keys
	Pattern string

	// Action is the action to take when the pattern matches
	Action InvalidationAction

	// Condition is the condition for invalidation
	Condition InvalidationCondition
}

InvalidationPattern represents a pattern for cache invalidation

type Manager

type Manager struct {
	// Cache is the underlying cache implementation
	Cache Cache

	// Options are the cache manager options
	Options *ManagerOptions
	// contains filtered or unexported fields
}

Manager manages the cache with advanced features

func NewManager

func NewManager(cache Cache, opts *ManagerOptions) *Manager

NewManager creates a new cache manager

func (*Manager) AddInvalidationPattern

func (m *Manager) AddInvalidationPattern(pattern InvalidationPattern)

AddInvalidationPattern adds an invalidation pattern

func (*Manager) Close

func (m *Manager) Close() error

Close closes the cache manager

func (*Manager) Delete

func (m *Manager) Delete(key string)

Delete removes a value from the cache

func (*Manager) Get

func (m *Manager) Get(key string) (interface{}, bool)

Get retrieves a value from the cache

func (*Manager) GetMetrics

func (m *Manager) GetMetrics() *Metrics

GetMetrics returns cache metrics

func (*Manager) Prefetch

func (m *Manager) Prefetch(req PrefetchRequest)

Prefetch queues a key for background prefetching

func (*Manager) Set

func (m *Manager) Set(key string, value interface{}, ttl time.Duration)

Set adds a value to the cache with the given TTL

type ManagerOptions

type ManagerOptions struct {
	// PrefetchConcurrency is the number of concurrent prefetch operations
	PrefetchConcurrency int

	// PrefetchQueueSize is the size of the prefetch queue
	PrefetchQueueSize int

	// EnablePrefetching enables background prefetching
	EnablePrefetching bool

	// EnableInvalidation enables cache invalidation
	EnableInvalidation bool

	// DefaultTTL is the default time-to-live for cached items
	DefaultTTL time.Duration

	// RefreshBeforeExpiry is the duration before expiry to refresh items
	RefreshBeforeExpiry time.Duration

	// RefreshCallback is called to refresh a cached item
	RefreshCallback func(ctx context.Context, key string) (interface{}, error)
}

ManagerOptions configures the cache manager

func DefaultManagerOptions

func DefaultManagerOptions() *ManagerOptions

DefaultManagerOptions returns the default manager options

type Metrics

type Metrics struct {
	// Gets is the number of Get operations
	Gets int64

	// Sets is the number of Set operations
	Sets int64

	// Hits is the number of cache hits
	Hits int64

	// Misses is the number of cache misses
	Misses int64

	// Deletes is the number of Delete operations
	Deletes int64

	// Clears is the number of Clear operations
	Clears int64

	// Errors is the number of errors
	Errors int64

	// Prefetches is the number of prefetch requests
	Prefetches int64

	// PrefetchesProcessed is the number of processed prefetch requests
	PrefetchesProcessed int64

	// Size is the current size of the cache in bytes
	Size int64
	// contains filtered or unexported fields
}

Metrics tracks cache performance metrics

func NewMetrics

func NewMetrics() *Metrics

NewMetrics creates a new metrics instance

func (*Metrics) Add

func (m *Metrics) Add(metric *int64, value int64)

Add adds a value to a metric

func (*Metrics) Get

func (m *Metrics) Get(metric *int64) int64

Get gets a metric value

func (*Metrics) HitRatio

func (m *Metrics) HitRatio() float64

HitRatio returns the cache hit ratio

func (*Metrics) Inc

func (m *Metrics) Inc(metric *int64)

Inc increments a metric

func (*Metrics) Reset

func (m *Metrics) Reset()

Reset resets all metrics

type Options

type Options struct {
	// CacheDir is the directory to store cached data
	CacheDir string

	// DefaultTTL is the default time-to-live for cached items
	DefaultTTL time.Duration

	// MaxSize is the maximum size of the cache in bytes (0 = unlimited)
	MaxSize int64

	// MemoryLimit is the maximum memory usage in bytes (0 = unlimited)
	MemoryLimit int64

	// ReadOnly opens the cache in read-only mode
	ReadOnly bool

	// EnablePrefetching enables background prefetching
	EnablePrefetching bool

	// PrefetchConcurrency is the number of concurrent prefetch operations
	PrefetchConcurrency int

	// EnableCompression enables compression for cached values
	EnableCompression bool

	// EnableEncryption enables encryption for cached values
	EnableEncryption bool

	// EncryptionKey is the key used for encryption
	EncryptionKey []byte

	// EnableMetrics enables collection of cache metrics
	EnableMetrics bool
}

Options configures the cache behavior

func DefaultOptions

func DefaultOptions() *Options

DefaultOptions returns the default cache options

type PrefetchRequest

type PrefetchRequest struct {
	// Key is the cache key
	Key string

	// Priority is the priority of the request (higher = more important)
	Priority int

	// Callback is called to fetch the item if not in cache
	Callback func(ctx context.Context) (interface{}, error)
}

PrefetchRequest represents a request to prefetch a cache item

Jump to

Keyboard shortcuts

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