Documentation
¶
Overview ¶
Package goredis provides a tag-based cache invalidation layer over go-redis.
Usage:
Cache a value with tags – later invalidate all keys sharing a tag at once:
user, err := goredis.CacheGetOrFetch(ctx, rdb, "user:42", 5*time.Minute, func() (*User, error) { return db.GetUser(42) }, "users", "admins", )
Invalidate every cached key tagged "admins" (e.g. after an update):
goredis.InvalidateByTags(ctx, rdb, "admins")
How it works:
- Each tag is a Redis Set keyed as "goredis:tag:<tagname>" containing all cache keys registered under that tag.
- CacheSet uses a Redis pipeline to atomically SET the value and SADD the key to each tag's Set in a single round-trip.
- InvalidateByTags fans out SMembers lookups across goroutines (one per tag), collects every referenced cache key, DEL them all at once, then cleans up the tag Sets.
- Pass nil for rdb to disable caching (useful when Redis is not configured).
Functions:
CacheGetOrFetch[T] – cache-aside pattern with optional tag registration CacheGet[T] – retrieve a cached value CacheSet – store a value and register it under tags InvalidateByTags – delete all cache keys associated with tags HashRequest – generate a deterministic key from a request struct
Index ¶
- Constants
- func CacheGet[T any](ctx context.Context, rdb *redis.Client, key string) (T, bool)
- func CacheGetOrFetch[T any](ctx context.Context, rdb *redis.Client, key string, ttl time.Duration, ...) (T, error)
- func CacheSet(ctx context.Context, rdb *redis.Client, key string, value any, ...) error
- func HashRequest(req any) string
- func InvalidateByTags(ctx context.Context, rdb *redis.Client, tags ...string) error
Constants ¶
View Source
const (
GoRdisCacheKey = "goredis"
)
Variables ¶
This section is empty.
Functions ¶
func CacheGetOrFetch ¶
func HashRequest ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.