-
Notifications
You must be signed in to change notification settings - Fork 5
/
caching_purger.go
69 lines (53 loc) · 2.06 KB
/
caching_purger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package gbox
import (
"context"
"fmt"
"strconv"
"github.com/eko/gocache/v2/store"
"github.com/jensneuse/graphql-go-tools/pkg/graphql"
"github.com/pkg/errors"
"go.uber.org/zap"
)
func (c *Caching) PurgeQueryResultBySchema(ctx context.Context, schema *graphql.Schema) error {
hash, _ := schema.Hash()
tag := fmt.Sprintf(cachingTagSchemaHashPattern, hash)
return c.purgeQueryResultByTags(ctx, []string{tag})
}
func (c *Caching) PurgeQueryResultByOperationName(ctx context.Context, name string) error {
return c.purgeQueryResultByTags(ctx, []string{fmt.Sprintf(cachingTagOperationPattern, name)})
}
func (c *Caching) PurgeQueryResultByTypeName(ctx context.Context, name string) error {
return c.purgeQueryResultByTags(ctx, []string{fmt.Sprintf(cachingTagTypePattern, name)})
}
func (c *Caching) PurgeQueryResultByTypeField(ctx context.Context, typeName, fieldName string) error {
return c.purgeQueryResultByTags(ctx, []string{fmt.Sprintf(cachingTagTypeFieldPattern, typeName, fieldName)})
}
func (c *Caching) PurgeQueryResultByTypeKey(ctx context.Context, typeName, fieldName string, value interface{}) error {
var cacheKey string
switch v := value.(type) {
case string:
cacheKey = fmt.Sprintf(cachingTagTypeKeyPattern, typeName, fieldName, v)
return c.purgeQueryResultByTags(ctx, []string{cacheKey})
case int:
cacheKey = fmt.Sprintf(cachingTagTypeKeyPattern, typeName, fieldName, strconv.Itoa(v))
return c.purgeQueryResultByTags(ctx, []string{cacheKey})
default:
return fmt.Errorf("only support purging type key value int or string, got %T", v)
}
}
func (c *Caching) purgeQueryResultByTags(ctx context.Context, tags []string) error {
var err error
c.logger.Debug("purging query result by tags", zap.Strings("tags", tags))
for _, t := range tags {
// because store invalidate method will be stopped on first error,
// so we need to invalidate tag by tag.
if e := c.store.Invalidate(ctx, store.InvalidateOptions{Tags: []string{t}}); e != nil {
if err == nil {
err = e
} else {
err = errors.WithMessage(err, e.Error())
}
}
}
return err
}