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

89 lines
2.0 KiB
Go
Raw Normal View History

package cache
import (
"time"
"forge.cadoles.com/arcad/edge/pkg/storage"
"forge.cadoles.com/arcad/edge/pkg/storage/driver/cache/lfu"
"forge.cadoles.com/arcad/edge/pkg/storage/driver/cache/lfu/memory"
)
type Options struct {
CacheTTL time.Duration
BlobCacheStore lfu.Store[string, []byte]
// Maximum total size of cached data
BlobCacheSize int
BlobInfoCacheStore lfu.Store[string, storage.BlobInfo]
// Maximum number of blob infos
BlobInfoCacheSize int
BlobBucketCacheStore lfu.Store[string, storage.BlobBucket]
// Maximum number of blob bucket
BlobBucketCacheSize int
}
type OptionFunc func(opts *Options)
func NewOptions(funcs ...OptionFunc) *Options {
defaultTTL := 60 * time.Minute
opts := &Options{
CacheTTL: defaultTTL,
BlobCacheStore: memory.NewStore[string, []byte](),
BlobCacheSize: 1e+9, // 1Gb
BlobInfoCacheStore: memory.NewStore[string, storage.BlobInfo](),
BlobInfoCacheSize: 256,
BlobBucketCacheStore: memory.NewStore[string, storage.BlobBucket](),
BlobBucketCacheSize: 16,
}
for _, fn := range funcs {
fn(opts)
}
return opts
}
func WithCacheTTL(ttl time.Duration) OptionFunc {
return func(opts *Options) {
opts.CacheTTL = ttl
}
}
func WithBlobBucketCacheSize(size int) OptionFunc {
return func(opts *Options) {
opts.BlobBucketCacheSize = size
}
}
func WithBlobBucketCacheStore(store lfu.Store[string, storage.BlobBucket]) OptionFunc {
return func(opts *Options) {
opts.BlobBucketCacheStore = store
}
}
func WithBlobInfoCacheSize(size int) OptionFunc {
return func(opts *Options) {
opts.BlobInfoCacheSize = size
}
}
func WithBlobInfoCacheStore(store lfu.Store[string, storage.BlobInfo]) OptionFunc {
return func(opts *Options) {
opts.BlobInfoCacheStore = store
}
}
func WithBlobCacheSize(size int) OptionFunc {
return func(opts *Options) {
opts.BlobCacheSize = size
}
}
func WithBlobCacheStore(store lfu.Store[string, []byte]) OptionFunc {
return func(opts *Options) {
opts.BlobCacheStore = store
}
}