edge/pkg/storage/driver/cache/lfu/options.go
William Petit a276b92a03
All checks were successful
arcad/edge/pipeline/head This commit looks good
arcad/edge/pipeline/pr-master This commit looks good
feat: implement lfu based cache strategy
2024-01-10 13:16:52 +01:00

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
}
}