Documentation
¶
Index ¶
- Variables
- type Cache
- func (c *Cache[T]) Close()
- func (c *Cache[T]) Delete(ctx context.Context, key string) error
- func (c *Cache[T]) Fetch(ctx context.Context, key string, ttl time.Duration, ...) (T, error)
- func (c *Cache[T]) InvalidateTags(ctx context.Context, tags ...string) error
- func (c *Cache[T]) Set(ctx context.Context, key string, value T, ttl time.Duration) error
- type JSONSerializer
- type Metrics
- type MsgPackSerializer
- type Option
- func WithCacheDeleteDisabled(disabled bool) Option
- func WithCacheReadDisabled(disabled bool) Option
- func WithCircuitBreaker(enable bool) Option
- func WithCircuitBreakerMaxFailures(failures uint32) Option
- func WithCircuitBreakerTimeout(timeout time.Duration) Option
- func WithExpirationJitter(jitter float64) Option
- func WithL1TTL(ttl time.Duration) Option
- func WithLockRetries(retries int) Option
- func WithLockSleep(sleep time.Duration) Option
- func WithLockTTL(ttl time.Duration) Option
- func WithLogger(l *slog.Logger) Option
- func WithMetrics(m Metrics) Option
- func WithNamespace(ns string) Option
- func WithNegativeCacheTTL(ttl time.Duration) Option
- func WithRefreshTimeout(timeout time.Duration) Option
- func WithSerializer(s Serializer) Option
- func WithStaleTTL(ttl time.Duration) Option
- func WithStaleWhileRevalidate(enable bool) Option
- type Options
- type Serializer
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("gorge: resource not found") ErrLuaScriptFailure = errors.New("gorge: unexpected response from lua script") )
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[T any] struct { // contains filtered or unexported fields }
func (*Cache[T]) InvalidateTags ¶
type JSONSerializer ¶
type JSONSerializer struct{}
JSONSerializer is the default implementation using JSON.
type Metrics ¶
type Metrics interface {
IncL1Hits()
IncL1Misses()
IncL2Hits()
IncL2Misses()
IncDBFetches()
IncDBErrors()
ObserveDBFetchLatency(d time.Duration)
ObserveL2HitLatency(d time.Duration)
}
Metrics defines the interface for tracking cache performance.
type MsgPackSerializer ¶
type MsgPackSerializer struct{}
MsgPackSerializer uses the vmihailenco/msgpack library for serialization. It is generally faster and produces smaller output than JSON.
func (MsgPackSerializer) Marshal ¶
func (s MsgPackSerializer) Marshal(v interface{}) ([]byte, error)
Marshal implements the Serializer interface.
func (MsgPackSerializer) Unmarshal ¶
func (s MsgPackSerializer) Unmarshal(data []byte, v interface{}) error
Unmarshal implements the Serializer interface.
type Option ¶
type Option func(*Options)
Option is a function to configure Options.
func WithCacheDeleteDisabled ¶
WithCacheDeleteDisabled enables or disables cache deletion.
func WithCacheReadDisabled ¶
WithCacheReadDisabled enables or disables cache reading.
func WithCircuitBreaker ¶
WithCircuitBreaker enables the circuit breaker.
func WithCircuitBreakerMaxFailures ¶
WithCircuitBreakerMaxFailures sets the number of consecutive failures before opening the circuit.
func WithCircuitBreakerTimeout ¶
WithCircuitBreakerTimeout sets the period of time to wait before transitioning from open to half-open.
func WithExpirationJitter ¶
WithExpirationJitter Set expiration jitter ratio (0.0 to 1.0).
func WithLockRetries ¶
WithLockRetries sets the number of retries for lock acquisition
func WithLockSleep ¶
WithLockSleep sets the wait time between lock acquisition attempts.
func WithLockTTL ¶
WithLockTTL sets the TTL for distributed lock.
func WithLogger ¶
WithLogger allows integrating the application's logger.
func WithMetrics ¶
WithMetrics WithMetrics allows integrating a metrics system.
func WithNamespace ¶
WithNamespace sets a prefix for all keys in Redis.
func WithNegativeCacheTTL ¶
WithNegativeCacheTTL Set TTL for negative cache ("not found" entries).
func WithRefreshTimeout ¶
WithRefreshTimeout sets the timeout for background refresh operations.
func WithSerializer ¶
func WithSerializer(s Serializer) Option
WithSerializer allows using a custom serializer (e.g., MsgPack).
func WithStaleTTL ¶
WithStaleTTL sets the TTL threshold to trigger SWR.
func WithStaleWhileRevalidate ¶
WithStaleWhileRevalidate enables or disables SWR mode.
type Options ¶
type Options struct {
Namespace string
L1TTL time.Duration
InvalidationChannel string
L1Config *ristretto.Config
Serializer Serializer
Logger *slog.Logger
Metrics Metrics
EnableStaleWhileRevalidate bool
// If the remaining TTL of a key is less than this value, SWR will be triggered. Default is 1 minute.
StaleTTL time.Duration
NegativeCacheTTL time.Duration
ExpirationJitter float64
// TTL for distributed lock. Must be long enough for the slowest fn() execution.
LockTTL time.Duration
// Wait time between failed lock acquisition attempts.
LockSleep time.Duration
// Number of retries for acquiring a distributed lock
LockRetries int
// Skip cache reading entirely, go directly to DB. Useful when Redis is failing.
DisableCacheRead bool
// Skip cache deletion entirely.
DisableCacheDelete bool
// Timeout for background refresh operations. Default is 10 seconds.
RefreshTimeout time.Duration
// Circuit Breaker settings
EnableCircuitBreaker bool
// Number of consecutive failures before opening the circuit.
CircuitBreakerMaxFailures uint32
// Period of time to wait before transitioning from open to half-open.
CircuitBreakerTimeout time.Duration
}
Options contains all configuration for the cache client.
func NewDefaultOptions ¶
func NewDefaultOptions() *Options
NewDefaultOptions creates a default configuration.
