2023-11-30 19:09:51 +01:00
|
|
|
package cache
|
|
|
|
|
2023-12-03 14:26:18 +01:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2024-01-06 16:48:14 +01:00
|
|
|
"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"
|
2023-12-03 14:26:18 +01:00
|
|
|
)
|
2023-11-30 19:09:51 +01:00
|
|
|
|
|
|
|
type Options struct {
|
2024-01-06 16:48:14 +01:00
|
|
|
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
|
2023-12-03 14:26:18 +01:00
|
|
|
BlobInfoCacheSize int
|
2024-01-06 16:48:14 +01:00
|
|
|
|
|
|
|
BlobBucketCacheStore lfu.Store[string, storage.BlobBucket]
|
|
|
|
// Maximum number of blob bucket
|
|
|
|
BlobBucketCacheSize int
|
2023-11-30 19:09:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type OptionFunc func(opts *Options)
|
|
|
|
|
|
|
|
func NewOptions(funcs ...OptionFunc) *Options {
|
2023-12-03 14:26:18 +01:00
|
|
|
defaultTTL := 60 * time.Minute
|
2023-11-30 19:09:51 +01:00
|
|
|
opts := &Options{
|
2024-01-06 16:48:14 +01:00
|
|
|
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,
|
2023-11-30 19:09:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range funcs {
|
|
|
|
fn(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithCacheTTL(ttl time.Duration) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.CacheTTL = ttl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 16:48:14 +01:00
|
|
|
func WithBlobBucketCacheSize(size int) OptionFunc {
|
2023-11-30 19:09:51 +01:00
|
|
|
return func(opts *Options) {
|
2024-01-06 16:48:14 +01:00
|
|
|
opts.BlobBucketCacheSize = size
|
2023-11-30 19:09:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-06 16:48:14 +01:00
|
|
|
func WithBlobBucketCacheStore(store lfu.Store[string, storage.BlobBucket]) OptionFunc {
|
2023-11-30 19:09:51 +01:00
|
|
|
return func(opts *Options) {
|
2024-01-06 16:48:14 +01:00
|
|
|
opts.BlobBucketCacheStore = store
|
2023-11-30 19:09:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithBlobInfoCacheSize(size int) OptionFunc {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.BlobInfoCacheSize = size
|
|
|
|
}
|
|
|
|
}
|
2024-01-06 16:48:14 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|