-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.go
More file actions
148 lines (130 loc) · 4.07 KB
/
options.go
File metadata and controls
148 lines (130 loc) · 4.07 KB
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
package daramjwee
import (
"fmt"
"time"
)
// ConfigError represents an error that occurs during the configuration process.
type ConfigError struct {
Message string
}
// Error returns the error message for ConfigError.
func (e *ConfigError) Error() string {
return fmt.Sprintf("daramjwee: configuration error: %s", e.Message)
}
// Config holds all the configurable settings for the daramjwee cache.
// Option functions modify fields within this struct.
type Config struct {
HotStore Store
ColdStore Store
WorkerStrategy string
WorkerPoolSize int
WorkerQueueSize int
WorkerJobTimeout time.Duration
DefaultTimeout time.Duration
ShutdownTimeout time.Duration
PositiveFreshFor time.Duration
NegativeFreshFor time.Duration
ColdStorePositiveFreshFor time.Duration
ColdStoreNegativeFreshFor time.Duration
}
// Option is a function type that modifies the Config.
type Option func(cfg *Config) error
// WithHotStore sets the Store to be used as the Hot Tier.
// This option is mandatory.
func WithHotStore(store Store) Option {
return func(cfg *Config) error {
if store == nil {
return &ConfigError{"hot store cannot be nil"}
}
cfg.HotStore = store
return nil
}
}
// WithColdStore sets the Store to be used as the Cold Tier.
// This option is optional.
func WithColdStore(store Store) Option {
return func(cfg *Config) error {
cfg.ColdStore = store
return nil
}
}
// WithWorker specifies the worker strategy and detailed settings for background tasks.
// If not set, reasonable defaults ("pool", size 10) are used.
func WithWorker(strategyType string, poolSize int, queueSize int, jobTimeout time.Duration) Option {
return func(cfg *Config) error {
if strategyType == "" {
return &ConfigError{"worker strategy type cannot be empty"}
}
if poolSize <= 0 {
return &ConfigError{"worker pool size must be positive"}
}
if jobTimeout <= 0 {
return &ConfigError{"worker job timeout must be positive"}
}
cfg.WorkerStrategy = strategyType
cfg.WorkerPoolSize = poolSize
cfg.WorkerQueueSize = queueSize
cfg.WorkerJobTimeout = jobTimeout
return nil
}
}
// WithDefaultTimeout sets the default timeout for cache operations like Get and Set.
func WithDefaultTimeout(timeout time.Duration) Option {
return func(cfg *Config) error {
if timeout <= 0 {
return &ConfigError{"default timeout must be positive"}
}
cfg.DefaultTimeout = timeout
return nil
}
}
// WithShutdownTimeout sets the timeout for graceful shutdown of the cache.
func WithShutdownTimeout(timeout time.Duration) Option {
return func(cfg *Config) error {
if timeout <= 0 {
return &ConfigError{"Shutdown timeout must be positive"}
}
cfg.ShutdownTimeout = timeout
return nil
}
}
// WithCache sets the freshness duration for positive cache entries.
// If freshFor is 0, the cache entry is considered stale immediately and a background refresh will be triggered on access.
func WithCache(freshFor time.Duration) Option {
return func(cfg *Config) error {
if freshFor < 0 {
return &ConfigError{"positive cache TTL cannot be a negative value"}
}
cfg.PositiveFreshFor = freshFor
return nil
}
}
// WithNegativeCache sets the freshness duration for negative cache entries (e.g., for ErrNotFound).
// If freshFor is 0, the negative cache entry is considered stale immediately and a background refresh will be triggered on access.
func WithNegativeCache(freshFor time.Duration) Option {
return func(cfg *Config) error {
if freshFor < 0 {
return &ConfigError{"negative cache TTL cannot be a negative value"}
}
cfg.NegativeFreshFor = freshFor
return nil
}
}
func WithColdStorePositiveFreshFor(freshFor time.Duration) Option {
return func(cfg *Config) error {
if freshFor < 0 {
return &ConfigError{"cold store positive cache TTL cannot be a negative value"}
}
cfg.ColdStorePositiveFreshFor = freshFor
return nil
}
}
func WithColdStoreNegativeFreshFor(freshFor time.Duration) Option {
return func(cfg *Config) error {
if freshFor < 0 {
return &ConfigError{"cold store negative cache TTL cannot be a negative value"}
}
cfg.ColdStoreNegativeFreshFor = freshFor
return nil
}
}