2021-09-10 06:56:56 +00:00
|
|
|
package cache
|
2021-09-08 07:08:56 +00:00
|
|
|
|
|
|
|
import (
|
2022-06-06 08:01:12 +00:00
|
|
|
"fmt"
|
2021-09-08 07:08:56 +00:00
|
|
|
"time"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2021-09-08 07:08:56 +00:00
|
|
|
"github.com/bluele/gcache"
|
2022-06-06 08:01:12 +00:00
|
|
|
"go.uber.org/zap"
|
2021-09-08 07:08:56 +00:00
|
|
|
)
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
type (
|
2022-04-13 16:56:58 +00:00
|
|
|
// AccessBoxCache stores an access box by its address.
|
2021-09-10 06:56:56 +00:00
|
|
|
AccessBoxCache struct {
|
2022-06-06 08:01:12 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
cache gcache.Cache
|
2021-09-10 06:56:56 +00:00
|
|
|
}
|
2021-09-08 07:08:56 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
// Config stores expiration params for cache.
|
|
|
|
Config struct {
|
|
|
|
Size int
|
|
|
|
Lifetime time.Duration
|
2022-06-06 08:01:12 +00:00
|
|
|
Logger *zap.Logger
|
2021-09-10 06:56:56 +00:00
|
|
|
}
|
|
|
|
)
|
2021-09-08 07:08:56 +00:00
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
const (
|
|
|
|
// DefaultAccessBoxCacheSize is a default maximum number of entries in cache.
|
|
|
|
DefaultAccessBoxCacheSize = 100
|
2022-04-13 16:56:58 +00:00
|
|
|
// DefaultAccessBoxCacheLifetime is a default lifetime of entries in cache.
|
2021-09-10 06:56:56 +00:00
|
|
|
DefaultAccessBoxCacheLifetime = 10 * time.Minute
|
|
|
|
)
|
2021-09-08 07:08:56 +00:00
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// DefaultAccessBoxConfig returns new default cache expiration values.
|
2022-06-06 08:01:12 +00:00
|
|
|
func DefaultAccessBoxConfig(logger *zap.Logger) *Config {
|
|
|
|
return &Config{
|
|
|
|
Size: DefaultAccessBoxCacheSize,
|
|
|
|
Lifetime: DefaultAccessBoxCacheLifetime,
|
|
|
|
Logger: logger,
|
|
|
|
}
|
2021-09-08 07:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAccessBoxCache creates an object of BucketCache.
|
2021-09-10 06:56:56 +00:00
|
|
|
func NewAccessBoxCache(config *Config) *AccessBoxCache {
|
2021-09-08 07:08:56 +00:00
|
|
|
gc := gcache.New(config.Size).LRU().Expiration(config.Lifetime).Build()
|
|
|
|
|
2022-06-06 08:01:12 +00:00
|
|
|
return &AccessBoxCache{cache: gc, logger: config.Logger}
|
2021-09-08 07:08:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// Get returns a cached object.
|
2022-05-25 17:25:43 +00:00
|
|
|
func (o *AccessBoxCache) Get(address oid.Address) *accessbox.Box {
|
2022-06-28 12:56:41 +00:00
|
|
|
entry, err := o.cache.Get(address)
|
2021-09-08 07:08:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := entry.(*accessbox.Box)
|
|
|
|
if !ok {
|
2022-06-06 08:01:12 +00:00
|
|
|
o.logger.Warn("invalid cache entry type", zap.String("actual", fmt.Sprintf("%T", entry)),
|
2022-06-06 12:03:51 +00:00
|
|
|
zap.String("expected", fmt.Sprintf("%T", result)))
|
2021-09-08 07:08:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put stores an object to cache.
|
2022-05-25 17:25:43 +00:00
|
|
|
func (o *AccessBoxCache) Put(address oid.Address, box *accessbox.Box) error {
|
2022-06-28 12:56:41 +00:00
|
|
|
return o.cache.Set(address, box)
|
2021-09-08 07:08:56 +00:00
|
|
|
}
|