|
package tache |
|
|
|
import ( |
|
"log/slog" |
|
"time" |
|
) |
|
|
|
|
|
type Options struct { |
|
Works int |
|
MaxRetry int |
|
Timeout *time.Duration |
|
PersistPath string |
|
PersistDebounce *time.Duration |
|
Running bool |
|
Logger *slog.Logger |
|
PersistReadFunction func() ([]byte, error) |
|
PersistWriteFunction func([]byte) error |
|
} |
|
|
|
|
|
func DefaultOptions() *Options { |
|
persistDebounce := 3 * time.Second |
|
return &Options{ |
|
Works: 5, |
|
|
|
PersistDebounce: &persistDebounce, |
|
Running: true, |
|
Logger: slog.Default(), |
|
} |
|
} |
|
|
|
|
|
type Option func(*Options) |
|
|
|
|
|
func WithOptions(opts Options) Option { |
|
return func(o *Options) { |
|
*o = opts |
|
} |
|
} |
|
|
|
|
|
func WithWorks(works int) Option { |
|
return func(o *Options) { |
|
o.Works = works |
|
} |
|
} |
|
|
|
|
|
func WithMaxRetry(maxRetry int) Option { |
|
return func(o *Options) { |
|
o.MaxRetry = maxRetry |
|
} |
|
} |
|
|
|
|
|
func WithTimeout(timeout time.Duration) Option { |
|
return func(o *Options) { |
|
o.Timeout = &timeout |
|
} |
|
} |
|
|
|
|
|
func WithPersistPath(path string) Option { |
|
return func(o *Options) { |
|
o.PersistPath = path |
|
} |
|
} |
|
|
|
func WithPersistFunction(r func() ([]byte, error), w func([]byte) error) Option { |
|
return func(o *Options) { |
|
o.PersistReadFunction = r |
|
o.PersistWriteFunction = w |
|
} |
|
} |
|
|
|
|
|
func WithPersistDebounce(debounce time.Duration) Option { |
|
return func(o *Options) { |
|
o.PersistDebounce = &debounce |
|
} |
|
} |
|
|
|
|
|
func WithRunning(running bool) Option { |
|
return func(o *Options) { |
|
o.Running = running |
|
} |
|
} |
|
|
|
|
|
func WithLogger(logger *slog.Logger) Option { |
|
return func(o *Options) { |
|
o.Logger = logger |
|
} |
|
} |
|
|