edge/pkg/storage/driver/cache/lfu/size.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

42 lines
705 B
Go

package lfu
import (
"github.com/pkg/errors"
)
type Measurable interface {
Size() (int, error)
}
func DefaultGetValueSize[V any](value V) (int, error) {
switch v := any(value).(type) {
case int:
return v, nil
case int8:
return int(v), nil
case int32:
return int(v), nil
case int64:
return int(v), nil
case float32:
return int(v), nil
case float64:
return int(v), nil
case []byte:
return len(v), nil
case string:
return len(v), nil
}
if measurable, ok := any(value).(Measurable); ok {
size, err := measurable.Size()
if err != nil {
return 0, errors.WithStack(err)
}
return size, nil
}
return 0, errors.Errorf("could not retrieve size of type '%T'", value)
}