cache

package
v0.9.10 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DriverMemory   = "memory"
	DriverFile     = "file"
	DriverRedis    = "redis"
	DriverDatabase = "database"
)

Driver types

Variables

This section is empty.

Functions

func Decrement

func Decrement(key string, value int64) (int64, error)

Decrement decrements a numeric value in the default cache.

func Flush

func Flush() error

Flush removes all values from the default cache.

func Forever

func Forever(key string, value interface{}) error

Forever stores a value in the default cache indefinitely.

func Forget

func Forget(key string) error

Forget removes a value from the default cache.

func Get

func Get(key string) (interface{}, bool)

Get retrieves a value from the default cache.

func GetString

func GetString(key string) (string, bool)

GetString retrieves a string value from the default cache.

func Has

func Has(key string) bool

Has checks if a key exists in the default cache.

func Increment

func Increment(key string, value int64) (int64, error)

Increment increments a numeric value in the default cache.

func InitFromEnv added in v0.9.5

func InitFromEnv() error

InitFromEnv sets up the cache manager with configuration from environment.

func Initialize

func Initialize() error

Initialize sets up the cache manager with configuration from environment.

func Many

func Many(keys []string) map[string]interface{}

Many retrieves multiple values from the default cache.

func Put

func Put(key string, value interface{}, ttl time.Duration) error

Put stores a value in the default cache with TTL.

func PutMany

func PutMany(items map[string]interface{}, ttl time.Duration) error

PutMany stores multiple values in the default cache.

func Remember

func Remember(key string, ttl time.Duration, callback func() interface{}) (interface{}, error)

Remember gets from cache or computes and stores.

func RememberForever

func RememberForever(key string, callback func() interface{}) (interface{}, error)

RememberForever gets from cache or computes and stores forever.

func SetDefaultManager added in v0.9.6

func SetDefaultManager(m *Manager)

SetDefaultManager sets the global default cache manager. Used by velocity.Default() to wire the App's cache into the global.

func SetEventDispatcher added in v0.4.0

func SetEventDispatcher(fn func(event interface{}) error)

SetEventDispatcher sets the function used to dispatch events. This is called by the events package to wire up event dispatching.

Types

type Cache

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

	// GetString retrieves a string value from the cache
	GetString(key string) (string, bool)

	// Put stores a value in the cache with a TTL
	Put(key string, value interface{}, ttl time.Duration) error

	// Forever stores a value in the cache indefinitely
	Forever(key string, value interface{}) error

	// Forget removes a value from the cache
	Forget(key string) error

	// Flush removes all values from the cache
	Flush() error

	// Increment increments a numeric value
	Increment(key string, value int64) (int64, error)

	// Decrement decrements a numeric value
	Decrement(key string, value int64) (int64, error)

	// Remember gets from cache or computes and stores
	Remember(key string, ttl time.Duration, callback func() interface{}) (interface{}, error)

	// RememberForever gets from cache or computes and stores forever
	RememberForever(key string, callback func() interface{}) (interface{}, error)

	// Many retrieves multiple values
	Many(keys []string) map[string]interface{}

	// PutMany stores multiple values
	PutMany(items map[string]interface{}, ttl time.Duration) error

	// Has checks if a key exists
	Has(key string) bool
}

Cache defines the interface for cache operations

type CacheForgotten added in v0.4.0

type CacheForgotten struct {
	Context  context.Context
	Key      string
	Store    string
	TraceID  string // APM trace ID
	SpanID   string // APM span ID
	ParentID string // Parent span ID for correlation
}

CacheForgotten is dispatched when a key is removed from the cache

func (*CacheForgotten) Name added in v0.4.0

func (e *CacheForgotten) Name() string

Name returns the event name

type CacheHit added in v0.4.0

type CacheHit struct {
	Context  context.Context
	Key      string
	Store    string
	TraceID  string // APM trace ID
	SpanID   string // APM span ID
	ParentID string // Parent span ID for correlation
}

CacheHit is dispatched when a cache lookup finds the key

func (*CacheHit) Name added in v0.4.0

func (e *CacheHit) Name() string

Name returns the event name

type CacheMiss added in v0.4.0

type CacheMiss struct {
	Context  context.Context
	Key      string
	Store    string
	TraceID  string // APM trace ID
	SpanID   string // APM span ID
	ParentID string // Parent span ID for correlation
}

CacheMiss is dispatched when a cache lookup does not find the key

func (*CacheMiss) Name added in v0.4.0

func (e *CacheMiss) Name() string

Name returns the event name

type CacheWritten added in v0.4.0

type CacheWritten struct {
	Context  context.Context
	Key      string
	Store    string
	TTL      time.Duration // 0 means forever
	TraceID  string        // APM trace ID
	SpanID   string        // APM span ID
	ParentID string        // Parent span ID for correlation
}

CacheWritten is dispatched when a value is written to the cache

func (*CacheWritten) Name added in v0.4.0

func (e *CacheWritten) Name() string

Name returns the event name

type Config

type Config struct {
	Default string
	Stores  map[string]StoreConfig
	Prefix  string
}

Config holds cache configuration

type Manager

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

Manager manages multiple cache stores

func GetManager

func GetManager() *Manager

GetManager returns the default cache manager. If not explicitly initialized, falls back to an in-memory cache manager.

func NewManager

func NewManager(config *Config) *Manager

NewManager creates a new cache manager with lazy store initialization.

func NewManagerFromConfig added in v0.9.5

func NewManagerFromConfig(config *Config) (*Manager, error)

NewManagerFromConfig creates a new cache manager and eagerly initializes all configured stores, returning an error if any store fails to initialize.

func (*Manager) Close

func (m *Manager) Close() error

Close closes all cache stores

func (*Manager) Decrement

func (m *Manager) Decrement(key string, value int64) (int64, error)

Decrement decrements a numeric value in the default cache store

func (*Manager) DefaultStore

func (m *Manager) DefaultStore() (Store, error)

DefaultStore returns the default cache store

func (*Manager) Flush

func (m *Manager) Flush() error

Flush removes all values from the default cache store

func (*Manager) Forever

func (m *Manager) Forever(key string, value interface{}) error

Forever stores a value in the default cache store indefinitely

func (*Manager) ForeverWithContext added in v0.4.0

func (m *Manager) ForeverWithContext(ctx context.Context, key string, value interface{}) error

ForeverWithContext stores a value in the default cache store indefinitely with context

func (*Manager) Forget

func (m *Manager) Forget(key string) error

Forget removes a value from the default cache store

func (*Manager) ForgetWithContext added in v0.4.0

func (m *Manager) ForgetWithContext(ctx context.Context, key string) error

ForgetWithContext removes a value from the default cache store with context

func (*Manager) Get

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

Get retrieves a value from the default cache store

func (*Manager) GetString

func (m *Manager) GetString(key string) (string, bool)

GetString retrieves a string value from the default cache store

func (*Manager) GetWithContext added in v0.4.0

func (m *Manager) GetWithContext(ctx context.Context, key string) (interface{}, bool)

GetWithContext retrieves a value from the default cache store with context

func (*Manager) Has

func (m *Manager) Has(key string) bool

Has checks if a key exists in the default cache store

func (*Manager) Increment

func (m *Manager) Increment(key string, value int64) (int64, error)

Increment increments a numeric value in the default cache store

func (*Manager) Many

func (m *Manager) Many(keys []string) map[string]interface{}

Many retrieves multiple values from the default cache store

func (*Manager) Put

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

Put stores a value in the default cache store

func (*Manager) PutMany

func (m *Manager) PutMany(items map[string]interface{}, ttl time.Duration) error

PutMany stores multiple values in the default cache store

func (*Manager) PutWithContext added in v0.4.0

func (m *Manager) PutWithContext(ctx context.Context, key string, value interface{}, ttl time.Duration) error

PutWithContext stores a value in the default cache store with context

func (*Manager) Remember

func (m *Manager) Remember(key string, ttl time.Duration, callback func() interface{}) (interface{}, error)

Remember gets from default cache or computes and stores

func (*Manager) RememberForever

func (m *Manager) RememberForever(key string, callback func() interface{}) (interface{}, error)

RememberForever gets from default cache or computes and stores forever

func (*Manager) Store

func (m *Manager) Store(name string) (Store, error)

Store returns a cache store by name

type Store

type Store interface {
	Cache
	GetPrefix() string
}

Store represents a cache store with a prefix

func GetStore

func GetStore(name string) (Store, error)

GetStore returns a specific cache store.

type StoreConfig

type StoreConfig struct {
	Driver string
	Prefix string
	// Additional driver-specific config can be added here
	Path     string // For file driver
	Host     string // For Redis driver
	Port     int    // For Redis driver
	Password string // For Redis driver
	Database int    // For Redis driver
	Table    string // For database driver
}

StoreConfig holds configuration for a specific store

Directories

Path Synopsis
Package testing provides test helpers for the cache package.
Package testing provides test helpers for the cache package.

Jump to

Keyboard shortcuts

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