driver

package
v2.413.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package driver provides cache driver construction and related helpers for go-service.

It contains the `NewDriver` constructor used by DI wiring to build a cache backend implementation from `cache/config.Config`.

Disabled / nil behavior

When caching is disabled (i.e. the cache config is nil), `NewDriver` returns a nil Driver and a nil error.

Supported kinds

The driver kind is selected by `Config.Kind`. Supported values are implementation-dependent, but this package currently includes built-in constructors for common backends (for example Redis and an in-memory sync driver).

The built-in Redis backend resolves its URL from a go-service "source string", constructs a go-redis client, and instruments that client via `cache/telemetry` before exposing it through the cachego Redis adapter. Redis configuration is strict by design: `Config.Options["url"]` must exist and be a string. The standard config fixtures provide that shape; callers that build config manually should validate it before calling `NewDriver`.

The built-in `sync` driver comes from the upstream cachego dependency and currently has whole-second TTL resolution. Callers should not rely on sub-second expiration with that backend.

If the configured kind is unknown, `NewDriver` returns `ErrNotFound`.

Errors

This package re-exports `cachego.ErrCacheExpired` as `ErrExpired` and provides `IsExpiredError` / `IsMissingError` helpers to classify backend-specific miss conditions in a backend-agnostic way.

Index

Constants

View Source
const ErrExpired = cachego.ErrCacheExpired

ErrExpired is an alias for cachego.ErrCacheExpired.

Drivers may return this error (or wrap it) to indicate that a cache entry exists but is expired. Use IsExpiredError to classify this condition.

Variables

View Source
var ErrNotFound = errors.New("cache: driver not found")

ErrNotFound is returned when the configured cache driver kind is unknown.

Functions

func IsExpiredError added in v2.243.0

func IsExpiredError(err error) bool

IsExpiredError reports whether err represents an expired cache entry.

This helper exists so higher-level code can treat expired entries as cache misses regardless of the underlying backend implementation.

func IsMissingError added in v2.303.6

func IsMissingError(err error) bool

IsMissingError reports whether err represents a missing cache entry.

This helper normalizes the miss semantics of the backends currently supported by this package, including Redis nil replies and the in-memory sync driver's plain "key not found" error.

Types

type Driver

type Driver = cachego.Cache

Driver is an alias for cachego.Cache.

It is the minimal interface used by the cache facade (`cache.Cache`) for persistence operations: fetch/save/delete/flush.

The alias preserves the upstream cachego interface shape exactly.

func NewDriver added in v2.73.0

func NewDriver(params DriverParams) (Driver, error)

NewDriver constructs a cache Driver for the configured backend.

Disabled behavior

If cfg is nil (caching disabled), NewDriver returns (nil, nil). Callers are expected to tolerate a nil Driver.

Configuration expectations

NewDriver dispatches on cfg.Kind. Some backends expect specific keys to be present in cfg.Options. For example, the "redis" backend expects:

  • options["url"] to be a string "source string" (e.g. "env:REDIS_URL" or "file:/path/to/url" or a literal URL)

The URL is read via fs.ReadSource, parsed using redis/go-redis ParseURL, and then the client is instrumented for tracing and metrics via `cache/telemetry` when those telemetry providers are enabled.

The Redis client is closed from the supplied lifecycle's OnStop hook.

Instrumentation errors are treated as fatal configuration/runtime errors and are converted into panics via runtime.Must, matching the existing repository convention for mandatory telemetry wiring in internal constructors.

Backends

Supported kinds include:

  • "redis": Redis backend using github.com/redis/go-redis
  • "sync": in-memory backend using github.com/faabiosr/cachego/sync

The built-in `sync` backend currently inherits whole-second TTL resolution from the upstream cachego dependency, so callers should not rely on sub-second expiration with that backend.

If cfg.Kind is unknown, NewDriver returns ErrNotFound.

type DriverParams added in v2.389.0

type DriverParams struct {
	di.In
	Lifecycle di.Lifecycle
	FS        *os.FS
	Config    *cache.Config
}

DriverParams defines dependencies for constructing a Driver.

Jump to

Keyboard shortcuts

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