Documentation
¶
Overview ¶
Package cache provides Gombit's backend-neutral cache boundary.
Index ¶
- func DecrementExisting(ctx context.Context, c Cache, key string, delta int64) (int64, bool, error)
- func IncrementTTL(ctx context.Context, c Cache, key string, delta int64, ttl time.Duration) (int64, error)
- func NewRedisClient(cfg config.RedisConfig) (*redis.Client, error)
- func RedisOptions(cfg config.RedisConfig) *redis.Options
- type Cache
- type Counter
- type Driver
- type Memory
- func (m *Memory) Close() error
- func (m *Memory) DecrementExisting(ctx context.Context, key string, delta int64) (int64, bool, 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) IncrementTTL(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)
- func (m *Memory) Set(ctx context.Context, key string, value any, ttl time.Duration) error
- type MemoryOption
- type Noop
- func (Noop) DecrementExisting(ctx context.Context, key string, delta int64) (int64, bool, error)
- 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) IncrementTTL(ctx context.Context, key string, delta int64, ttl time.Duration) (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) DecrementExisting(ctx context.Context, key string, delta int64) (int64, bool, error)
- 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) IncrementTTL(ctx context.Context, key string, delta int64, ttl time.Duration) (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) DecrementExisting(ctx context.Context, key string, delta int64) (int64, bool, 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) IncrementTTL(ctx context.Context, key string, delta int64, ttl time.Duration) (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 DecrementExisting ¶ added in v0.9.0
DecrementExisting takes delta off key in c only if it exists, through c's Counter when it has one. For a Cache without one it decrements, then deletes the key when the decrement created it (the result is -delta: no counter was there): not atomic, so between the two another caller's increment of the new key can be lost with it (an undercount, never a negative counter left behind), and a counter that held exactly 0 is deleted rather than left at -delta (the same count, as far as a limit goes). found is false for a key that did not exist.
func IncrementTTL ¶ added in v0.9.0
func IncrementTTL(ctx context.Context, c Cache, key string, delta int64, ttl time.Duration) (int64, error)
IncrementTTL increments key in c by delta and gives it ttl when it has no expiry, through c's Counter when it has one. For a Cache without one it increments, then sets the counter again with ttl when the increment created it: not atomic, so an increment by another caller between the two can be lost (an undercount at the start of a window, never a counter that lives forever). ttl must be positive.
func NewRedisClient ¶
func NewRedisClient(cfg config.RedisConfig) (*redis.Client, error)
NewRedisClient builds a go-redis client from typed configuration.
func RedisOptions ¶ added in v0.5.0
func RedisOptions(cfg config.RedisConfig) *redis.Options
RedisOptions maps the typed Redis settings to go-redis options, for a caller that tunes them further before building a client (the job queue).
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 Counter ¶ added in v0.9.0
type Counter interface {
IncrementTTL(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)
DecrementExisting(ctx context.Context, key string, delta int64) (value int64, found bool, err error)
}
Counter is implemented by caches whose counters can expire: rate limits, attempt counters. IncrementTTL increments key by delta, as Increment does, and gives the key ttl when it has no expiry (it was just created, or written without one); a key that already expires keeps its deadline, so a fixed window is not stretched by every hit. Atomic: concurrent calls never lose an increment or leave a counter without its expiry. ttl must be positive: a counter that never expires is what Increment is for.
DecrementExisting takes delta off key only if the key exists (and has not expired), keeping its expiry: it never creates a counter, so giving back a place in a counter that was cleared or evicted meanwhile writes nothing (a create-on-miss decrement would leave a negative counter). found reports whether the key existed.
The built-in drivers (memory, redis, noop) and Namespace implement it; IncrementTTL and DecrementExisting (the functions) fall back, not atomically, for a Cache that does not.
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.
func (*Memory) DecrementExisting ¶ added in v0.9.0
func (m *Memory) DecrementExisting(ctx context.Context, key string, delta int64) (int64, bool, error)
DecrementExisting implements Counter.
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.
A non-positive interval means no janitor: no goroutine is started, as if the option were omitted (issue #436). time.NewTicker panics on such a value, and it would do so inside the janitor goroutine, where the caller cannot recover it, so an unset or misparsed interval used to crash the whole process.
type Noop ¶
type Noop struct{}
Noop disables persistence while preserving cache call sites.
func (Noop) DecrementExisting ¶ added in v0.9.0
DecrementExisting implements Counter: nothing exists.
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
Redis stores cache values in Redis.
func (*Redis) DecrementExisting ¶ added in v0.9.0
func (r *Redis) DecrementExisting(ctx context.Context, key string, delta int64) (int64, bool, error)
DecrementExisting implements Counter.
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) DecrementExisting ¶ added in v0.9.0
func (s *Store) DecrementExisting(ctx context.Context, key string, delta int64) (int64, bool, error)
DecrementExisting implements Counter.
func (*Store) IncrementTTL ¶ added in v0.9.0
func (s *Store) IncrementTTL(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)
IncrementTTL implements Counter.