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"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2024-04-16 08:20:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2024-10-18 13:59:52 +00:00
|
|
|
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
|
|
|
}
|
2024-02-06 13:44:49 +00:00
|
|
|
|
|
|
|
AccessBoxCacheValue struct {
|
2024-04-16 08:20:35 +00:00
|
|
|
Box *accessbox.Box
|
|
|
|
Attributes []object.Attribute
|
|
|
|
PutTime time.Time
|
2024-10-18 13:59:52 +00:00
|
|
|
Address *oid.Address
|
2024-02-06 13:44:49 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2024-02-06 13:44:49 +00:00
|
|
|
// NewAccessBoxCache creates an object of AccessBoxCache.
|
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
|
|
|
}
|
|
|
|
|
2024-02-06 13:44:49 +00:00
|
|
|
// Get returns a cached accessbox.
|
2024-08-30 12:05:32 +00:00
|
|
|
func (o *AccessBoxCache) Get(accessKeyID string) *AccessBoxCacheValue {
|
|
|
|
entry, err := o.cache.Get(accessKeyID)
|
2021-09-08 07:08:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-06 13:44:49 +00:00
|
|
|
result, ok := entry.(*AccessBoxCacheValue)
|
2021-09-08 07:08:56 +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-09-08 07:08:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2024-02-06 13:44:49 +00:00
|
|
|
// Put stores an accessbox to cache.
|
2024-10-18 13:59:52 +00:00
|
|
|
func (o *AccessBoxCache) Put(accessKeyID string, val *AccessBoxCacheValue) error {
|
2024-08-30 12:05:32 +00:00
|
|
|
return o.cache.Set(accessKeyID, val)
|
2024-02-06 13:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes an accessbox from cache.
|
2024-08-30 12:05:32 +00:00
|
|
|
func (o *AccessBoxCache) Delete(accessKeyID string) {
|
|
|
|
o.cache.Remove(accessKeyID)
|
2021-09-08 07:08:56 +00:00
|
|
|
}
|