-
Notifications
You must be signed in to change notification settings - Fork 4
/
option.go
172 lines (149 loc) · 4.69 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2023 FishGoddess. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cachego
import (
"time"
)
// Option applies to config and sets some values to config.
type Option func(conf *config)
func (o Option) applyTo(conf *config) {
o(conf)
}
func applyOptions(conf *config, opts []Option) {
for _, opt := range opts {
opt.applyTo(conf)
}
}
// WithCacheName returns an option setting the cacheName of config.
func WithCacheName(cacheName string) Option {
return func(conf *config) {
conf.cacheName = cacheName
}
}
// WithLRU returns an option setting the type of cache to lru.
// Notice that lru cache must have max entries limit, so you have to specify a maxEntries.
func WithLRU(maxEntries int) Option {
return func(conf *config) {
conf.cacheType = lru
conf.maxEntries = maxEntries
}
}
// WithLFU returns an option setting the type of cache to lfu.
// Notice that lfu cache must have max entries limit, so you have to specify a maxEntries.
func WithLFU(maxEntries int) Option {
return func(conf *config) {
conf.cacheType = lfu
conf.maxEntries = maxEntries
}
}
// WithShardings returns an option setting the sharding count of cache.
// Negative value means no sharding.
func WithShardings(shardings int) Option {
return func(conf *config) {
conf.shardings = shardings
}
}
// WithDisableSingleflight returns an option turning off singleflight mode of cache.
func WithDisableSingleflight() Option {
return func(conf *config) {
conf.singleflight = false
}
}
// WithGC returns an option setting the duration of cache gc.
// Negative value means no gc.
func WithGC(gcDuration time.Duration) Option {
return func(conf *config) {
conf.gcDuration = gcDuration
}
}
// WithMaxScans returns an option setting the max scans of cache.
// Negative value means no limit.
func WithMaxScans(maxScans int) Option {
return func(conf *config) {
conf.maxScans = maxScans
}
}
// WithMaxEntries returns an option setting the max entries of cache.
// Negative value means no limit.
func WithMaxEntries(maxEntries int) Option {
return func(conf *config) {
conf.maxEntries = maxEntries
}
}
// WithNow returns an option setting the now function of cache.
// A now function should return a nanosecond unix time.
func WithNow(now func() int64) Option {
return func(conf *config) {
if now != nil {
conf.now = now
}
}
}
// WithHash returns an option setting the hash function of cache.
// A hash function should return the hash code of key.
func WithHash(hash func(key string) int) Option {
return func(conf *config) {
if hash != nil {
conf.hash = hash
}
}
}
// WithRecordMissed returns an option setting the recordMissed of config.
func WithRecordMissed(recordMissed bool) Option {
return func(conf *config) {
conf.recordMissed = recordMissed
}
}
// WithRecordHit returns an option setting the recordHit of config.
func WithRecordHit(recordHit bool) Option {
return func(conf *config) {
conf.recordHit = recordHit
}
}
// WithRecordGC returns an option setting the recordGC of config.
func WithRecordGC(recordGC bool) Option {
return func(conf *config) {
conf.recordGC = recordGC
}
}
// WithRecordLoad returns an option setting the recordLoad of config.
func WithRecordLoad(recordLoad bool) Option {
return func(conf *config) {
conf.recordLoad = recordLoad
}
}
// WithReportMissed returns an option setting the reportMissed of config.
func WithReportMissed(reportMissed func(reporter *Reporter, key string)) Option {
return func(conf *config) {
conf.reportMissed = reportMissed
}
}
// WithReportHit returns an option setting the reportHit of config.
func WithReportHit(reportHit func(reporter *Reporter, key string, value interface{})) Option {
return func(conf *config) {
conf.reportHit = reportHit
}
}
// WithReportGC returns an option setting the reportGC of config.
func WithReportGC(reportGC func(reporter *Reporter, cost time.Duration, cleans int)) Option {
return func(conf *config) {
conf.reportGC = reportGC
}
}
// WithReportLoad returns an option setting the reportLoad of config.
func WithReportLoad(reportLoad func(reporter *Reporter, key string, value interface{}, ttl time.Duration, err error)) Option {
return func(conf *config) {
conf.reportLoad = reportLoad
}
}