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 ¶
- Variables
- type Config
- type Manager
- func (m *Manager) AsResource(name, description string, opts ...resource.Option) resource.Resource
- func (m *Manager) Clear(ctx context.Context) error
- func (m *Manager) Delete(ctx context.Context, key string) error
- func (m *Manager) Get(ctx context.Context, key string, out any) error
- func (m *Manager) IsRedisConnected() bool
- func (m *Manager) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- func (m *Manager) Start()
- func (m *Manager) Stop()
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
NewManager constructs a Manager with the in-memory store initialized. In "production" it also kicks off a Redis connect attempt. Call Start() to run the reconnect loop (NewManager alone doesn't spawn goroutines).
func Provide ¶
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 ¶
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) Get ¶
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 ¶
IsRedisConnected reports whether Redis is the currently active store.