package db import ( "context" "errors" "fmt" "time" "github.com/go-gorm/caches/v4" "github.com/redis/go-redis/v9" ) type redisCacher struct { rdb *redis.Client } func (c *redisCacher) Get(ctx context.Context, key string, q *caches.Query[any]) (*caches.Query[any], error) { res, err := c.rdb.Get(ctx, key).Result() if errors.Is(err, redis.Nil) { return nil, nil } if err != nil { return nil, err } if err := q.Unmarshal([]byte(res)); err != nil { return nil, err } return q, nil } func (c *redisCacher) Store(ctx context.Context, key string, val *caches.Query[any]) error { res, err := val.Marshal() if err != nil { return err } c.rdb.Set(ctx, key, res, 300*time.Second) // Set proper cache time return nil } func (c *redisCacher) Invalidate(ctx context.Context) error { var ( cursor uint64 keys []string ) for { var ( k []string err error ) k, cursor, err = c.rdb.Scan(ctx, cursor, fmt.Sprintf("%s*", caches.IdentifierPrefix), 0).Result() if err != nil { return err } keys = append(keys, k...) if cursor == 0 { break } } if len(keys) > 0 { if _, err := c.rdb.Del(ctx, keys...).Result(); err != nil { return err } } return nil }