cache

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package cache provides Gombit's backend-neutral cache boundary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRedisClient

func NewRedisClient(cfg config.RedisConfig) (*redis.Client, error)

NewRedisClient builds a go-redis client from typed configuration.

Types

type Cache

type Cache interface {
	Get(ctx context.Context, key string, dst any) (bool, error)
	Set(ctx context.Context, key string, value any, ttl time.Duration) error
	Delete(ctx context.Context, keys ...string) error
	Increment(ctx context.Context, key string, delta int64) (int64, error)
}

Cache is the value-oriented cache contract used by framework features.

func Namespace

func Namespace(namespace string, next Cache) Cache

Namespace prefixes all keys before forwarding operations to next.

type Driver

type Driver string

Driver names an opened cache driver.

const (
	// DriverMemory stores cache values in process memory.
	DriverMemory Driver = Driver(config.CacheDriverMemory)
	// DriverRedis stores cache values in Redis.
	DriverRedis Driver = Driver(config.CacheDriverRedis)
	// DriverNoop disables persistence while preserving cache call sites.
	DriverNoop Driver = Driver(config.CacheDriverNoop)
)

type Memory

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

Memory stores cache values in process memory.

func NewMemory

func NewMemory(opts ...MemoryOption) *Memory

NewMemory creates an empty in-process cache.

func (*Memory) Close added in v0.1.10

func (m *Memory) Close() error

Close stops the janitor goroutine started by WithJanitor, if any. Safe to call on a Memory with no janitor, and safe to call more than once.

func (*Memory) Delete

func (m *Memory) Delete(ctx context.Context, keys ...string) error

Delete implements Cache.

func (*Memory) Get

func (m *Memory) Get(ctx context.Context, key string, dst any) (bool, error)

Get implements Cache.

func (*Memory) Increment

func (m *Memory) Increment(ctx context.Context, key string, delta int64) (int64, error)

Increment implements Cache.

func (*Memory) Set

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

Set implements Cache.

type MemoryOption added in v0.1.10

type MemoryOption func(*Memory)

MemoryOption configures a Memory cache created by NewMemory.

func WithJanitor added in v0.1.10

func WithJanitor(interval time.Duration) MemoryOption

WithJanitor starts a background goroutine that sweeps expired entries every interval, stopped by Close. Without this option, Memory never runs a goroutine and only reclaims an expired key when that same key is read again via Get — the caller must opt in for proactive reclamation of write-heavy, rarely-re-read keys (rate limits, one-time tokens, nonces).

Close must be called to stop the goroutine. cache.Open wires this automatically for the memory driver, and framework.App closes it on shutdown for a cache the App opened itself. If you construct a Memory with WithJanitor yourself and hand it to framework.WithCache (or keep it outside framework.App entirely), framework.App does not close a cache it did not open — you are responsible for calling Close, or the goroutine (and the Memory it holds) leaks for the life of the process.

type Noop

type Noop struct{}

Noop disables persistence while preserving cache call sites.

func (Noop) Delete

func (Noop) Delete(ctx context.Context, keys ...string) error

Delete implements Cache.

func (Noop) Get

func (Noop) Get(ctx context.Context, key string, dst any) (bool, error)

Get implements Cache.

func (Noop) Increment

func (Noop) Increment(ctx context.Context, key string, delta int64) (int64, error)

Increment implements Cache.

func (Noop) Set

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

Set implements Cache.

type Redis

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

Redis stores cache values in Redis.

func NewRedis

func NewRedis(client *redis.Client) *Redis

NewRedis wraps a go-redis client with Gombit's cache contract.

func (*Redis) Client

func (r *Redis) Client() *redis.Client

Client returns the underlying go-redis client.

func (*Redis) Delete

func (r *Redis) Delete(ctx context.Context, keys ...string) error

Delete implements Cache.

func (*Redis) Get

func (r *Redis) Get(ctx context.Context, key string, dst any) (bool, error)

Get implements Cache.

func (*Redis) Increment

func (r *Redis) Increment(ctx context.Context, key string, delta int64) (int64, error)

Increment implements Cache.

func (*Redis) Set

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

Set implements Cache.

type Store

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

Store is an opened cache with driver metadata and optional Redis escape hatch.

func Open

func Open(cfg config.CacheConfig) (*Store, error)

Open opens the configured cache driver.

func (*Store) Close

func (s *Store) Close() error

Close closes driver-owned resources.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, keys ...string) error

Delete implements Cache.

func (*Store) Driver

func (s *Store) Driver() Driver

Driver returns the configured driver.

func (*Store) Get

func (s *Store) Get(ctx context.Context, key string, dst any) (bool, error)

Get implements Cache.

func (*Store) Increment

func (s *Store) Increment(ctx context.Context, key string, delta int64) (int64, error)

Increment implements Cache.

func (*Store) Redis

func (s *Store) Redis() *redis.Client

Redis returns the underlying go-redis client when the Redis driver is enabled.

func (*Store) Set

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

Set implements Cache.

Jump to

Keyboard shortcuts

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