Documentation
¶
Overview ¶
Package gsredis is the go-redis UniversalClient adapter for the gscore graceful-shutdown engine.
redis.UniversalClient.Close() releases the underlying connection pool. This adapter registers Close() as a ctx-aware closer on any gscore-compatible manager, bounding the call with a per-closer timeout so the shutdown sequence cannot stall on a slow client.
Recommended placement is PhasePostDB (value 4): any txctx.OnCommit callback that writes to Redis after a DB commit must have completed by then, so it is safe to tear the client down.
Index ¶
Constants ¶
const DefaultTimeout = 5 * time.Second
DefaultTimeout is used by Register when timeout=0 is passed.
Variables ¶
var ErrCloseTimedOut = errors.New("gsredis: client.Close() timed out")
ErrCloseTimedOut is returned from the registered closer if Close() did not return within the per-closer timeout. The manager logs it but continues with the rest of the shutdown sequence.
Functions ¶
func InstrumentAndRegister ¶
func InstrumentAndRegister(client redis.UniversalClient, mgr CloserRegistrar, phase gscore.Phase, timeout time.Duration) error
InstrumentAndRegister instruments client with OpenTelemetry tracing and metrics via apmcore, then registers its Close for graceful shutdown. Use this instead of Register when apmcore.SetupOTelSDK has been called.
Returns the error from apmcore.InstrumentRedis if instrumentation fails; the caller may choose to ignore it since tracing is best-effort.
func Register ¶
func Register(client redis.UniversalClient, mgr CloserRegistrar, phase gscore.Phase, timeout time.Duration)
Register attaches client to mgr so that during phase the manager will call client.Close() bounded by timeout. timeout=0 falls back to DefaultTimeout.
name is used in log lines; pass something distinguishable when you have multiple Redis clients (e.g. "redis-cache", "redis-rate-limit"). Empty name is auto-filled by the manager.
Calling Register twice with the same client will close it twice; go-redis returns an error on the second call, which the manager logs.
Types ¶
type CloserRegistrar ¶
type CloserRegistrar = gscore.CloserRegistrar
CloserRegistrar is the subset of gscore.Manager used by gsredis helpers. It is a type alias for gscore.CloserRegistrar; *gscore.Manager satisfies it directly.
type RedisCacher ¶ added in v0.8.0
type RedisCacher struct {
// contains filtered or unexported fields
}
RedisCacher implements caches.Cacher using a go-redis UniversalClient.
Tag-based invalidation: Store indexes each cache key under its tags via SADD tag:<tag> <key>. Invalidate resolves tags to evict from three sources:
- event.Tags — explicit tags set via WithInvalidateTags on the context.
- table tags — "<table>" for each table in event.Tables, always. This matches entries tagged with the plain table name (the common TagsFunc setup) so any mutation on a table evicts its cached SELECTs.
- entity tags — "<table>:<id>" for each combination of event.Tables x event.EntityIDs, for entries tagged at entity granularity via WithTags.
Tag→key index entries are only built when the caller configures caches.Config.TagsFunc (or uses caches.WithTags at query sites) — the Store path has no access to table information on its own. Without tags on Store, invalidation is a no-op and the caller must rely on TTL expiry.
func NewRedisCacher ¶ added in v0.8.0
func NewRedisCacher(client redis.UniversalClient, ttl time.Duration) *RedisCacher
NewRedisCacher creates a RedisCacher backed by client with the given TTL. ttl=0 disables expiry (keys persist until explicitly invalidated).
func (*RedisCacher) Invalidate ¶ added in v0.8.0
func (r *RedisCacher) Invalidate(ctx context.Context, event *caches.InvalidationEvent) error