cache

package
v0.13.2 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package cache provides a Redis + in-memory hybrid cache for nexus apps, ported from the oats_applicant implementation. A Manager always has the in-memory store ready; in "production" mode it also tries to keep a Redis connection, falling back to memory on outage and reconnecting on a 30s tick.

Typical wiring with fx:

fx.New(
    fx.Provide(zap.NewExample),
    cache.Module,                 // provides *cache.Manager + *cache.Config
    fx.Invoke(func(app *nexus.App, m *cache.Manager) {
        app.Register(m.AsResource("session-cache", "Hybrid redis/memory"))
    }),
)

Without fx, call NewConfig() + NewManager(cfg, logger) and Start().

Index

Constants

This section is empty.

Variables

View Source
var Module = fx.Module("nexus-cache",
	fx.Provide(
		NewConfig,
		Provide,
	),
)

Module provides *Config (from env) and *Manager into the Fx graph. Consume it by taking *cache.Manager in your constructors.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Environment controls Redis behavior. "production" attempts Redis and
	// keeps reconnecting; anything else stays on memory.
	Environment string

	RedisHost     string
	RedisPort     string
	RedisPassword string
	RedisDB       int

	// DefaultExpiry is go-cache's default TTL. CleanupExpiry is its GC tick.
	DefaultExpiry time.Duration
	CleanupExpiry time.Duration

	// ConnectTimeout caps the initial Redis ping during connect attempts.
	ConnectTimeout time.Duration

	// ReconnectInterval controls how often the manager retries Redis when
	// it's down. 0 defaults to 30s.
	ReconnectInterval time.Duration
}

Config holds cache configuration. Populate via NewConfig (env-driven) or construct directly.

func NewConfig

func NewConfig() *Config

NewConfig builds a Config from env vars: APP_ENV, REDIS_HOST, REDIS_PORT, REDIS_PASSWORD. Defaults: env=development, host=localhost, port=6379, db=0, 15m/10m expiries, 5s connect timeout, 30s reconnect.

func (*Config) RedisAddress

func (c *Config) RedisAddress() string

RedisAddress returns host:port, filling in localhost:6379 when blank.

type Manager

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

Manager is the live cache. Its Get/Set/Delete are safe for concurrent use; the underlying store flips between Redis and memory atomically under a mutex when connectivity changes.

func NewManager

func NewManager(cfg *Config, logger *zap.Logger) *Manager

NewManager constructs a Manager with the in-memory store initialized. Redis connection, when enabled via "production" mode, is attempted asynchronously in Start() so an unreachable Redis never delays boot. Call Start() to kick off the connect + reconnect loop.

func Provide

func Provide(lc fx.Lifecycle, cfg *Config, logger *zap.Logger) *Manager

Provide is the fx provider: builds a Manager from a *Config and ties its Start/Stop to the Fx lifecycle. Use via cache.Module.

func (*Manager) AsResource

func (m *Manager) AsResource(name, description string, opts ...resource.Option) resource.Resource

AsResource builds a nexus resource.Resource for this Manager. Mark it as default with extra options passed through. Backend ("redis" vs "memory") is reported live via WithDetails.

func (*Manager) Clear

func (m *Manager) Clear(ctx context.Context) error

Clear wipes every key in the active store.

func (*Manager) Delete

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

Delete removes key from the active store.

func (*Manager) Get

func (m *Manager) Get(ctx context.Context, key string, out any) error

Get deserializes the cached value under key into out. Returns an error if the key is missing or the cache isn't initialized.

func (*Manager) IsRedisConnected

func (m *Manager) IsRedisConnected() bool

IsRedisConnected reports whether Redis is the currently active store.

func (*Manager) Set

func (m *Manager) Set(ctx context.Context, key string, value any, ttl time.Duration) error

Set stores value under key with the given TTL.

func (*Manager) Start

func (m *Manager) Start()

Start kicks off the background reconnect/health loop and fires the first Redis connect attempt asynchronously. Safe to call once; the whole path is skipped outside production mode. Callers never block on Redis availability — the manager serves from memory until Redis comes up, then flips atomically under the mutex.

func (*Manager) Stop

func (m *Manager) Stop()

Stop cancels the background loop. Idempotent.

Jump to

Keyboard shortcuts

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