-
Notifications
You must be signed in to change notification settings - Fork 3
/
option.go
182 lines (156 loc) · 4.62 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
173
174
175
176
177
178
179
180
181
182
// Copyright 2020 Ye Zi Jie. 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.
//
// Author: FishGoddess
// Email: fishgoddess@qq.com
// Created at 2021/04/05 15:58:16
package cachego
import (
"context"
"math/bits"
"time"
"github.com/FishGoddess/cachego/internal/config"
)
// Option is a function which initializes Config.
type Option func(conf *config.Config)
// applyOptions applies opts to conf.
func applyOptions(conf *config.Config, opts ...Option) *config.Config {
for _, applyOption := range opts {
applyOption(conf)
}
return conf
}
// WithMapSize is an option setting initializing map size of cache.
func WithMapSize(mapSize uint) Option {
return func(conf *config.Config) {
conf.MapSize = int(mapSize)
}
}
// WithSegmentSize is an option setting initializing segment size of cache.
// segmentSize must be the pow of 2 (such as 64) or the segments may be uneven.
func WithSegmentSize(segmentSize uint) Option {
if bits.OnesCount(segmentSize) > 1 {
panic("cachego: segmentSize must be the pow of 2 (such as 64) or the segments may be uneven.")
}
return func(conf *config.Config) {
conf.SegmentSize = int(segmentSize)
}
}
// WithAutoGC is an option turning on automatically gc.
func WithAutoGC(d time.Duration) Option {
return func(conf *config.Config) {
if d > 0 {
conf.GCDuration = d
}
}
}
// GetOption is a function which initializes GetConfig.
type GetOption func(conf *config.GetConfig)
// applyGetOptions applies opts to conf.
func applyGetOptions(conf *config.GetConfig, opts ...GetOption) *config.GetConfig {
for _, applyOption := range opts {
applyOption(conf)
}
return conf
}
// WithGetContext sets context to ctx.
func WithGetContext(ctx context.Context) GetOption {
return func(conf *config.GetConfig) {
conf.Ctx = ctx
}
}
// WithGetTTL sets the ttl of missed key if loaded to ttl.
func WithGetTTL(ttl time.Duration) GetOption {
return func(conf *config.GetConfig) {
conf.TTL = ttl
}
}
// WithGetNoTTL sets the ttl of missed key to no ttl.
func WithGetNoTTL() GetOption {
return func(conf *config.GetConfig) {
conf.TTL = config.NoTTL
}
}
// WithGetOnMissed sets onMissed to Get operation.
func WithGetOnMissed(onMissed func(ctx context.Context) (data interface{}, err error)) GetOption {
return func(conf *config.GetConfig) {
conf.OnMissed = onMissed
}
}
// WithGetNoSingleflight sets the single-flight mode to false.
func WithGetNoSingleflight() GetOption {
return func(conf *config.GetConfig) {
conf.Singleflight = false
}
}
// SetOption is a function which initializes SetConfig.
type SetOption func(conf *config.SetConfig)
// applySetOptions applies opts to conf.
func applySetOptions(conf *config.SetConfig, opts ...SetOption) *config.SetConfig {
for _, applyOption := range opts {
applyOption(conf)
}
return conf
}
// WithSetTTL sets the ttl of key to ttl.
func WithSetTTL(ttl time.Duration) SetOption {
return func(conf *config.SetConfig) {
if ttl > 0 {
conf.TTL = ttl
}
}
}
// WithSetNoTTL sets the ttl of key to no ttl.
func WithSetNoTTL() SetOption {
return func(conf *config.SetConfig) {
conf.TTL = config.NoTTL
}
}
// AutoSetOption is a function which initializes AutoSetConfig.
type AutoSetOption func(conf *config.AutoSetConfig)
// applyAutoSetOptions applies opts to conf.
func applyAutoSetOptions(conf *config.AutoSetConfig, opts ...AutoSetOption) *config.AutoSetConfig {
for _, applyOption := range opts {
applyOption(conf)
}
return conf
}
// WithAutoSetContext sets context to ctx.
func WithAutoSetContext(ctx context.Context) AutoSetOption {
return func(conf *config.AutoSetConfig) {
conf.Ctx = ctx
}
}
// WithAutoSetTTL sets the ttl of key to ttl.
func WithAutoSetTTL(ttl time.Duration) AutoSetOption {
return func(conf *config.AutoSetConfig) {
if ttl > 0 {
conf.TTL = ttl
}
}
}
// WithAutoSetNoTTL sets the ttl of key to no ttl.
func WithAutoSetNoTTL() AutoSetOption {
return func(conf *config.AutoSetConfig) {
conf.TTL = config.NoTTL
}
}
// WithAutoSetGap sets the gap between two set operations to gap.
func WithAutoSetGap(gap time.Duration) AutoSetOption {
return func(conf *config.AutoSetConfig) {
if gap > 0 {
conf.Gap = gap
}
}
}