forked from mercari/datastore
/
option.go
97 lines (75 loc) · 2.48 KB
/
option.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package rediscache
import (
"context"
"time"
"go.mercari.io/datastore"
"go.mercari.io/datastore/dsmiddleware/storagecache"
)
// WithIncludeKinds creates a ClientOption that selects the Kind specified as the cache target.
func WithIncludeKinds(kinds ...string) CacheOption {
return &withIncludeKinds{kinds}
}
type withIncludeKinds struct{ kinds []string }
func (w *withIncludeKinds) Apply(o *cacheHandler) {
o.stOpts.Filters = append(o.stOpts.Filters, func(ctx context.Context, key datastore.Key) bool {
for _, incKind := range w.kinds {
if key.Kind() == incKind {
return true
}
}
return false
})
}
// WithExcludeKinds creates a ClientOption that selects the Kind unspecified as the cache target.
func WithExcludeKinds(kinds ...string) CacheOption {
return &withExcludeKinds{kinds}
}
type withExcludeKinds struct{ kinds []string }
func (w *withExcludeKinds) Apply(o *cacheHandler) {
o.stOpts.Filters = append(o.stOpts.Filters, func(ctx context.Context, key datastore.Key) bool {
for _, excKind := range w.kinds {
if key.Kind() == excKind {
return false
}
}
return true
})
}
// WithKeyFilter creates a ClientOption that selects the Keys specified as the cache target.
func WithKeyFilter(f storagecache.KeyFilter) CacheOption {
return &withKeyFilter{f}
}
type withKeyFilter struct{ f storagecache.KeyFilter }
func (w *withKeyFilter) Apply(o *cacheHandler) {
o.stOpts.Filters = append(o.stOpts.Filters, func(ctx context.Context, key datastore.Key) bool {
return w.f(ctx, key)
})
}
// WithLogger creates a ClientOption that uses the specified logger.
func WithLogger(logf func(ctx context.Context, format string, args ...interface{})) CacheOption {
return &withLogger{logf}
}
type withLogger struct {
logf func(ctx context.Context, format string, args ...interface{})
}
func (w *withLogger) Apply(o *cacheHandler) {
o.logf = w.logf
}
// WithExpireDuration creates a ClientOption to expire at a specified time.
func WithExpireDuration(d time.Duration) CacheOption {
return &withExpireDuration{d}
}
type withExpireDuration struct{ d time.Duration }
func (w *withExpireDuration) Apply(o *cacheHandler) {
o.expireDuration = w.d
}
// WithCacheKey creates a ClientOption that specifies how to generate a cache key from datastore.Key.
func WithCacheKey(f func(key datastore.Key) string) CacheOption {
return &withCacheKey{f}
}
type withCacheKey struct {
cacheKey func(key datastore.Key) string
}
func (w *withCacheKey) Apply(o *cacheHandler) {
o.cacheKey = w.cacheKey
}