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-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2023-03-07 14:38:08 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
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
|
|
|
// ObjectsNameCache provides lru cache for objects.
|
|
|
|
// This cache contains mapping nice names to object addresses.
|
2021-08-19 06:55:22 +00:00
|
|
|
// Key is bucketName+objectName.
|
2021-09-10 06:56:56 +00:00
|
|
|
type ObjectsNameCache struct {
|
2022-06-06 08:01:12 +00:00
|
|
|
cache gcache.Cache
|
|
|
|
logger *zap.Logger
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
const (
|
|
|
|
// DefaultObjectsNameCacheSize is a default maximum number of entries in cache.
|
|
|
|
DefaultObjectsNameCacheSize = 1e4
|
2022-04-13 16:56:58 +00:00
|
|
|
// DefaultObjectsNameCacheLifetime is a default lifetime of entries in cache.
|
2021-09-10 06:56:56 +00:00
|
|
|
DefaultObjectsNameCacheLifetime = time.Minute
|
2021-08-18 13:48:58 +00:00
|
|
|
)
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// DefaultObjectsNameConfig returns new default cache expiration values.
|
2022-06-06 08:01:12 +00:00
|
|
|
func DefaultObjectsNameConfig(logger *zap.Logger) *Config {
|
|
|
|
return &Config{
|
|
|
|
Size: DefaultObjectsNameCacheSize,
|
|
|
|
Lifetime: DefaultObjectsNameCacheLifetime,
|
|
|
|
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
|
|
|
// NewObjectsNameCache creates an object of ObjectsNameCache.
|
|
|
|
func NewObjectsNameCache(config *Config) *ObjectsNameCache {
|
|
|
|
gc := gcache.New(config.Size).LRU().Expiration(config.Lifetime).Build()
|
2022-06-06 08:01:12 +00:00
|
|
|
return &ObjectsNameCache{cache: gc, logger: config.Logger}
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
// Get returns a cached object. Returns nil if value is missing.
|
|
|
|
func (o *ObjectsNameCache) Get(key string) *oid.Address {
|
2021-08-18 13:48:58 +00:00
|
|
|
entry, err := o.cache.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
result, ok := entry.(oid.Address)
|
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
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
return &result
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put puts an object to cache.
|
2022-05-25 17:25:43 +00:00
|
|
|
func (o *ObjectsNameCache) Put(key string, address oid.Address) error {
|
2021-09-10 06:56:56 +00:00
|
|
|
return o.cache.Set(key, address)
|
2021-08-18 13:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes an object from cache.
|
2021-09-10 06:56:56 +00:00
|
|
|
func (o *ObjectsNameCache) Delete(key string) bool {
|
2021-08-18 13:48:58 +00:00
|
|
|
return o.cache.Remove(key)
|
|
|
|
}
|