cachefx

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: Apache-2.0 Imports: 10 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidFactoryConfig = errors.New("invalid config")
)

Functions

func Module

func Module(opts ...Option) fx.Option

Module provides the cache Factory and, with WithSweeper, registers the Sweeper to periodically evict expired entries from every cache the factory hands out.

Types

type Config

type Config struct {
	// URL selects and configures the backend. It is passed to the backend
	// unchanged, like Redis's: "memory://" serves an unbounded in-memory
	// cache, and "memory://?max_entries=N" caps every cache the factory hands
	// out at N entries with least-recently-used eviction. The cache backend
	// parses the URL, so a malformed N fails when the cache is created
	// (typically at app startup) instead of silently dropping the cap.
	// "redis://" connects to Redis, where eviction is a server-side concern.
	URL string

	// SweepInterval is how often the Sweeper evicts expired entries from every
	// cache created by the Factory. A non-positive value disables sweeping:
	// NewSweeper rejects it, so a module wired with withRun=true fails at
	// startup instead of silently never sweeping.
	SweepInterval time.Duration
}

Config controls the cache backend via a URL (e.g., "memory://", "redis://...") and the sweeper.

type Factory

type Factory interface {
	New(name string) (cache.Cache, error)
	WithName(name string) Factory

	// CleanupAll evicts expired entries from every cache handed out by this
	// factory, including the ones created by factories derived from it through
	// WithName.
	CleanupAll(ctx context.Context) error
}

func NewFactory

func NewFactory(config Config) (Factory, error)

type Option added in v0.2.0

type Option func(*moduleConfig)

Option configures Module.

func WithSweeper added in v0.2.0

func WithSweeper() Option

WithSweeper registers the Sweeper as a runnable, so Config.SweepInterval must be positive or the app fails at startup.

type Sweeper added in v0.2.0

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

Sweeper periodically evicts expired entries from every cache created by the Factory.

Eviction of expired entries is not guaranteed by every backend: the in-memory backend keeps expired items until an explicit Cleanup, and each New call returns a distinct instance, so the sweeper walks every cache the factory has handed out.

func NewSweeper added in v0.2.0

func NewSweeper(config Config, factory Factory, logger *zap.Logger) (*Sweeper, error)

NewSweeper creates a Sweeper running every config.SweepInterval.

It fails when the interval is not positive, so a module wired to run the sweeper fails at startup instead of silently never sweeping.

func (*Sweeper) Run added in v0.2.0

func (s *Sweeper) Run(ctx context.Context) error

Run evicts expired entries until ctx is canceled.

Directories

Path Synopsis
Package cache provides a flexible caching abstraction with multiple backend implementations.
Package cache provides a flexible caching abstraction with multiple backend implementations.

Jump to

Keyboard shortcuts

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