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 `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 ¶
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 ¶
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
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
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 ¶
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.
func NewDriver ¶ added in v2.73.0
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.
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.