-
Notifications
You must be signed in to change notification settings - Fork 11
/
adapter.go
52 lines (43 loc) · 1.3 KB
/
adapter.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
package cache
import (
"context"
"time"
)
// Adapter is the interface communicating with shared/local caches.
type Adapter interface {
MGet(context context.Context, keys []string) ([]Value, error)
MSet(context context.Context, keyVals map[string][]byte, ttl time.Duration, options ...MSetOptions) error
Del(context context.Context, keys ...string) error
}
// MSetOptions is an alias for functional argument.
type MSetOptions func(opts *msetOptions)
type msetOptions struct {
onCostAdd func(key string, cost int)
onCostEvict func(key string, cost int)
}
// WithOnCostAddFunc sets up the callback when adding the cache with key and cost.
func WithOnCostAddFunc(f func(key string, cost int)) MSetOptions {
return func(opts *msetOptions) {
opts.onCostAdd = f
}
}
// WithOnCostEvictFunc sets up the callback when evicting the cache with key and cost.
func WithOnCostEvictFunc(f func(key string, cost int)) MSetOptions {
return func(opts *msetOptions) {
opts.onCostEvict = f
}
}
func loadMSetOptions(options ...MSetOptions) *msetOptions {
opts := &msetOptions{}
for _, option := range options {
option(opts)
}
return opts
}
// Value is returned by MGet()
type Value struct {
// Valid stands for existing in cache or not.
Valid bool
// Bytes stands for the return value in byte format.
Bytes []byte
}