2021-08-18 13:48:58 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2022-06-06 08:01:12 +00:00
|
|
|
"fmt"
|
2021-08-18 13:48:58 +00:00
|
|
|
"time"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2021-08-18 13:48:58 +00:00
|
|
|
"github.com/bluele/gcache"
|
2022-06-06 08:01:12 +00:00
|
|
|
"go.uber.org/zap"
|
2021-08-18 13:48:58 +00:00
|
|
|
)
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// BucketCache contains cache with objects and the lifetime of cache entries.
|
2021-09-10 06:56:56 +00:00
|
|
|
type BucketCache struct {
|
2022-06-06 08:01:12 +00:00
|
|
|
cache gcache.Cache
|
|
|
|
logger *zap.Logger
|
2021-09-10 06:56:56 +00:00
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
const (
|
|
|
|
// DefaultBucketCacheSize is a default maximum number of entries in cache.
|
|
|
|
DefaultBucketCacheSize = 1e3
|
2022-04-13 16:56:58 +00:00
|
|
|
// DefaultBucketCacheLifetime is a default lifetime of entries in cache.
|
2021-09-10 06:56:56 +00:00
|
|
|
DefaultBucketCacheLifetime = time.Minute
|
2021-08-18 13:48:58 +00:00
|
|
|
)
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// DefaultBucketConfig returns new default cache expiration values.
|
2022-06-06 08:01:12 +00:00
|
|
|
func DefaultBucketConfig(logger *zap.Logger) *Config {
|
|
|
|
return &Config{
|
|
|
|
Size: DefaultBucketCacheSize,
|
|
|
|
Lifetime: DefaultBucketCacheLifetime,
|
|
|
|
Logger: logger,
|
|
|
|
}
|
2021-09-10 06:56:56 +00:00
|
|
|
}
|
2021-08-18 13:48:58 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
// NewBucketCache creates an object of BucketCache.
|
|
|
|
func NewBucketCache(config *Config) *BucketCache {
|
|
|
|
gc := gcache.New(config.Size).LRU().Expiration(config.Lifetime).Build()
|
2022-06-06 08:01:12 +00:00
|
|
|
return &BucketCache{cache: gc, logger: config.Logger}
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// Get returns a cached object.
|
2023-11-16 12:10:51 +00:00
|
|
|
func (o *BucketCache) Get(ns, bktName string) *data.BucketInfo {
|
2023-11-23 07:44:50 +00:00
|
|
|
entry, err := o.cache.Get(formKey(ns, bktName))
|
2021-08-18 13:48:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
result, ok := entry.(*data.BucketInfo)
|
2021-08-18 13:48:58 +00:00
|
|
|
if !ok {
|
2023-08-23 11:07:52 +00:00
|
|
|
o.logger.Warn(logs.InvalidCacheEntryType, zap.String("actual", fmt.Sprintf("%T", entry)),
|
2022-06-06 12:03:51 +00:00
|
|
|
zap.String("expected", fmt.Sprintf("%T", result)))
|
2021-08-18 13:48:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put puts an object to cache.
|
2023-11-23 07:44:50 +00:00
|
|
|
func (o *BucketCache) Put(bkt *data.BucketInfo) error {
|
|
|
|
return o.cache.Set(formKey(bkt.Zone, bkt.Name), bkt)
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes an object from cache.
|
2023-11-23 07:44:50 +00:00
|
|
|
func (o *BucketCache) Delete(bkt *data.BucketInfo) bool {
|
|
|
|
return o.cache.Remove(formKey(bkt.Zone, bkt.Name))
|
|
|
|
}
|
|
|
|
|
2024-03-01 13:09:33 +00:00
|
|
|
func formKey(zone, name string) string {
|
|
|
|
return name + "." + zone
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|