78 lines
1.6 KiB
Go
78 lines
1.6 KiB
Go
package lcw
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
type options struct {
|
|
maxKeys int
|
|
maxValueSize int
|
|
maxKeySize int
|
|
maxCacheSize int64
|
|
ttl time.Duration
|
|
}
|
|
|
|
// Option func type
|
|
type Option func(o *options) error
|
|
|
|
// MaxValSize functional option defines the largest value's size allowed to be cached
|
|
// By default it is 0, which means unlimited.
|
|
func MaxValSize(max int) Option {
|
|
return func(o *options) error {
|
|
if max < 0 {
|
|
return errors.New("negative max value size")
|
|
}
|
|
o.maxValueSize = max
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// MaxKeySize functional option defines the largest key's size allowed to be used in cache
|
|
// By default it is 0, which means unlimited.
|
|
func MaxKeySize(max int) Option {
|
|
return func(o *options) error {
|
|
if max < 0 {
|
|
return errors.New("negative max key size")
|
|
}
|
|
o.maxKeySize = max
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// MaxKeys functional option defines how many keys to keep.
|
|
// By default it is 0, which means unlimited.
|
|
func MaxKeys(max int) Option {
|
|
return func(o *options) error {
|
|
if max < 0 {
|
|
return errors.New("negative max keys")
|
|
}
|
|
o.maxKeys = max
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// MaxCacheSize functional option defines the total size of cached data.
|
|
// By default it is 0, which means unlimited.
|
|
func MaxCacheSize(max int64) Option {
|
|
return func(o *options) error {
|
|
if max < 0 {
|
|
return errors.New("negative max cache size")
|
|
}
|
|
o.maxCacheSize = max
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// TTL functional option defines duration.
|
|
// Works for ExpirableCache only
|
|
func TTL(ttl time.Duration) Option {
|
|
return func(o *options) error {
|
|
if ttl < 0 {
|
|
return errors.New("negative ttl")
|
|
}
|
|
o.ttl = ttl
|
|
return nil
|
|
}
|
|
}
|