2023-10-24 22:52:33 +02:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage"
|
2023-11-29 11:10:29 +01:00
|
|
|
"github.com/allegro/bigcache/v3"
|
2023-11-30 19:09:51 +01:00
|
|
|
"github.com/hashicorp/golang-lru/v2/expirable"
|
2023-10-24 22:52:33 +02:00
|
|
|
"github.com/pkg/errors"
|
2023-11-30 19:09:51 +01:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
2023-10-24 22:52:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type BlobStore struct {
|
2023-11-30 19:09:51 +01:00
|
|
|
store storage.BlobStore
|
|
|
|
contentCache *bigcache.BigCache
|
|
|
|
bucketCache *expirable.LRU[string, storage.BlobBucket]
|
|
|
|
blobInfoCache *expirable.LRU[string, storage.BlobInfo]
|
2023-10-24 22:52:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBucket implements storage.BlobStore.
|
|
|
|
func (s *BlobStore) DeleteBucket(ctx context.Context, name string) error {
|
|
|
|
if err := s.store.DeleteBucket(ctx, name); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-11-30 19:09:51 +01:00
|
|
|
s.bucketCache.Remove(name)
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListBuckets implements storage.BlobStore.
|
|
|
|
func (s *BlobStore) ListBuckets(ctx context.Context) ([]string, error) {
|
|
|
|
buckets, err := s.store.ListBuckets(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buckets, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenBucket implements storage.BlobStore.
|
|
|
|
func (s *BlobStore) OpenBucket(ctx context.Context, name string) (storage.BlobBucket, error) {
|
2023-11-30 19:09:51 +01:00
|
|
|
bucket, ok := s.bucketCache.Get(name)
|
|
|
|
if ok {
|
|
|
|
logger.Debug(ctx, "found bucket in cache", logger.F("name", name))
|
|
|
|
|
|
|
|
return &BlobBucket{
|
|
|
|
bucket: bucket,
|
|
|
|
contentCache: s.contentCache,
|
|
|
|
blobInfoCache: s.blobInfoCache,
|
|
|
|
bucketCache: s.bucketCache,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
bucket, err := s.store.OpenBucket(ctx, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-11-30 19:09:51 +01:00
|
|
|
s.bucketCache.Add(name, bucket)
|
|
|
|
|
2023-10-24 22:52:33 +02:00
|
|
|
return &BlobBucket{
|
2023-11-30 19:09:51 +01:00
|
|
|
bucket: bucket,
|
|
|
|
contentCache: s.contentCache,
|
|
|
|
blobInfoCache: s.blobInfoCache,
|
|
|
|
bucketCache: s.bucketCache,
|
2023-10-24 22:52:33 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-11-30 19:09:51 +01:00
|
|
|
func NewBlobStore(store storage.BlobStore, funcs ...OptionFunc) (*BlobStore, error) {
|
|
|
|
options := NewOptions(funcs...)
|
|
|
|
|
2023-12-03 14:26:18 +01:00
|
|
|
contentCache, err := bigcache.New(context.Background(), options.BigCache)
|
2023-11-30 19:09:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
onBlobBucketEvict := func(key string, bucket storage.BlobBucket) {
|
|
|
|
ctx := context.Background()
|
|
|
|
logger.Debug(ctx, "evicting blob bucket from cache", logger.F("cacheKey", key))
|
|
|
|
|
|
|
|
if err := bucket.Close(); err != nil {
|
|
|
|
logger.Error(ctx, "could not close bucket", logger.E(errors.WithStack(err)))
|
|
|
|
}
|
2023-10-24 22:52:33 +02:00
|
|
|
}
|
2023-11-30 19:09:51 +01:00
|
|
|
|
|
|
|
bucketCache := expirable.NewLRU[string, storage.BlobBucket](options.BucketCacheSize, onBlobBucketEvict, options.CacheTTL)
|
|
|
|
blobInfoCache := expirable.NewLRU[string, storage.BlobInfo](options.BlobInfoCacheSize, nil, options.CacheTTL)
|
|
|
|
|
|
|
|
return &BlobStore{
|
|
|
|
store: store,
|
|
|
|
contentCache: contentCache,
|
|
|
|
bucketCache: bucketCache,
|
|
|
|
blobInfoCache: blobInfoCache,
|
|
|
|
}, nil
|
2023-10-24 22:52:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ storage.BlobStore = &BlobStore{}
|