-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcaching_rule.go
45 lines (34 loc) · 1.24 KB
/
caching_rule.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
package gbox
import (
"encoding/json"
"github.com/caddyserver/caddy/v2"
"github.com/jensneuse/graphql-go-tools/pkg/graphql"
"github.com/jensneuse/graphql-go-tools/pkg/pool"
)
type CachingRule struct {
// GraphQL type to cache
// ex: `User` will cache all query results have type User
// ex: `User { is_admin }` will cache all query results have type User and have field `is_admin`.
// If not set this rule will match all types.
Types graphql.RequestTypes `json:"types,omitempty"`
// how long query results that match the rule types should be store.
MaxAge caddy.Duration `json:"max_age,omitempty"`
// how long stale query results that match the rule types should be served while fresh data is already being fetched in the background.
Swr caddy.Duration `json:"swr,omitempty"`
// Varies name apply to query results that match the rule types.
// If not set query results will cache public.
Varies []string `json:"varies,omitempty"`
}
type CachingRules map[string]*CachingRule
func (rules CachingRules) hash() (uint64, error) {
if rules == nil {
return 0, nil
}
hash := pool.Hash64.Get()
hash.Reset()
defer pool.Hash64.Put(hash)
if err := json.NewEncoder(hash).Encode(rules); err != nil {
return 0, err
}
return hash.Sum64(), nil
}