cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2025 License: MIT Imports: 7 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 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

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 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

func NewManager

func NewManager(config *Config) *Manager

NewManager creates a new cache manager

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) Forget

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

Forget removes a value from the default cache store

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) 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) 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

Jump to

Keyboard shortcuts

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