Documentation
¶
Overview ¶
Package cache provides Gombit's backend-neutral cache boundary.
Index ¶
- func NewRedisClient(cfg config.RedisConfig) (*redis.Client, error)
- type Cache
- type Driver
- type Memory
- func (m *Memory) Close() error
- func (m *Memory) Delete(ctx context.Context, keys ...string) error
- func (m *Memory) Get(ctx context.Context, key string, dst any) (bool, error)
- func (m *Memory) Increment(ctx context.Context, key string, delta int64) (int64, error)
- func (m *Memory) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- type MemoryOption
- type Noop
- func (Noop) Delete(ctx context.Context, keys ...string) error
- func (Noop) Get(ctx context.Context, key string, dst any) (bool, error)
- func (Noop) Increment(ctx context.Context, key string, delta int64) (int64, error)
- func (Noop) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- type Redis
- func (r *Redis) Client() *redis.Client
- func (r *Redis) Delete(ctx context.Context, keys ...string) error
- func (r *Redis) Get(ctx context.Context, key string, dst any) (bool, error)
- func (r *Redis) Increment(ctx context.Context, key string, delta int64) (int64, error)
- func (r *Redis) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- type Store
- func (s *Store) Close() error
- func (s *Store) Delete(ctx context.Context, keys ...string) error
- func (s *Store) Driver() Driver
- func (s *Store) Get(ctx context.Context, key string, dst any) (bool, error)
- func (s *Store) Increment(ctx context.Context, key string, delta int64) (int64, error)
- func (s *Store) Redis() *redis.Client
- func (s *Store) Set(ctx context.Context, key string, value any, ttl time.Duration) error
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.
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
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.
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.
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis stores cache values in Redis.
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.