edge/pkg/storage/driver/cache/lfu/options.go

58 lines
1.2 KiB
Go

package lfu
import "time"
type GetValueSizeFunc[V any] func(value V) (int, error)
type LogFunc func(format string, values ...any)
func DefaultLogFunc(format string, values ...any) {
}
type Options[K comparable, V any] struct {
GetValueSize GetValueSizeFunc[V]
Capacity int
Log LogFunc
TTL time.Duration
}
type OptionsFunc[K comparable, V any] func(opts *Options[K, V])
func DefaultOptions[K comparable, V any](funcs ...OptionsFunc[K, V]) *Options[K, V] {
opts := &Options[K, V]{
GetValueSize: DefaultGetValueSize[V],
Capacity: 100,
Log: DefaultLogFunc,
TTL: 0,
}
for _, fn := range funcs {
fn(opts)
}
return opts
}
func WithCapacity[K comparable, V any](capacity int) OptionsFunc[K, V] {
return func(opts *Options[K, V]) {
opts.Capacity = capacity
}
}
func WithGetValueSize[K comparable, V any](getValueSize GetValueSizeFunc[V]) OptionsFunc[K, V] {
return func(opts *Options[K, V]) {
opts.GetValueSize = getValueSize
}
}
func WithLog[K comparable, V any](fn LogFunc) OptionsFunc[K, V] {
return func(opts *Options[K, V]) {
opts.Log = fn
}
}
func WithTTL[K comparable, V any](ttl time.Duration) OptionsFunc[K, V] {
return func(opts *Options[K, V]) {
opts.TTL = ttl
}
}