forked from viney-shih/go-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
83 lines (70 loc) · 2.57 KB
/
options.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
package cache
// MarshalFunc specifies the algorithm during marshaling the value to bytes.
// The default is json.Marshal.
type MarshalFunc func(interface{}) ([]byte, error)
// UnmarshalFunc specifies the algorithm during unmarshaling the bytes to the value.
// The default is json.Unmarshal
type UnmarshalFunc func([]byte, interface{}) error
// FactoryOptions is an alias for functional argument.
type FactoryOptions func(opts *factoryOptions)
// factoryOptions contains all options which will be applied when calling NewFactory().
type factoryOptions struct {
marshalFunc MarshalFunc
unmarshalFunc UnmarshalFunc
onCacheHit func(prefix string, key string, count int)
onCacheMiss func(prefix string, key string, count int)
onLCCostAdd func(prefix string, key string, cost int)
onLCCostEvict func(prefix string, key string, cost int)
pubsub Pubsub
}
// WithMarshalFunc sets up the specified marshal function.
// Needs to consider with unmarshal function at the same time.
func WithMarshalFunc(f MarshalFunc) FactoryOptions {
return func(opts *factoryOptions) {
opts.marshalFunc = f
}
}
// WithUnmarshalFunc sets up the specified unmarshal function.
// Needs to consider with marshal function at the same time.
func WithUnmarshalFunc(f UnmarshalFunc) FactoryOptions {
return func(opts *factoryOptions) {
opts.unmarshalFunc = f
}
}
// WithPubSub is used to evict keys in local cache
func WithPubSub(pb Pubsub) FactoryOptions {
return func(opts *factoryOptions) {
opts.pubsub = pb
}
}
// OnCacheHitFunc sets up the callback function on cache hitted
func OnCacheHitFunc(f func(prefix string, key string, count int)) FactoryOptions {
return func(opts *factoryOptions) {
opts.onCacheHit = f
}
}
// OnCacheMissFunc sets up the callback function on cache missed
func OnCacheMissFunc(f func(prefix string, key string, count int)) FactoryOptions {
return func(opts *factoryOptions) {
opts.onCacheMiss = f
}
}
// OnLocalCacheCostAddFunc sets up the callback function on adding the cost of key in local cache
func OnLocalCacheCostAddFunc(f func(prefix string, key string, cost int)) FactoryOptions {
return func(opts *factoryOptions) {
opts.onLCCostAdd = f
}
}
// OnLocalCacheCostEvictFunc sets up the callback function on evicting the cost of key in local cache
func OnLocalCacheCostEvictFunc(f func(prefix string, key string, cost int)) FactoryOptions {
return func(opts *factoryOptions) {
opts.onLCCostEvict = f
}
}
func loadFactoryOptions(options ...FactoryOptions) *factoryOptions {
opts := &factoryOptions{}
for _, option := range options {
option(opts)
}
return opts
}