2021-08-10 11:19:26 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2022-06-06 08:01:12 +00:00
|
|
|
"fmt"
|
2021-08-10 11:19:26 +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"
|
2023-03-07 14:38:08 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2021-08-10 11:19:26 +00:00
|
|
|
"github.com/bluele/gcache"
|
2022-06-06 08:01:12 +00:00
|
|
|
"go.uber.org/zap"
|
2021-08-10 11:19:26 +00:00
|
|
|
)
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
// ObjectsCache provides lru cache for objects.
|
|
|
|
type ObjectsCache struct {
|
2022-06-06 08:01:12 +00:00
|
|
|
cache gcache.Cache
|
|
|
|
logger *zap.Logger
|
2021-08-10 11:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-08-16 09:58:09 +00:00
|
|
|
// DefaultObjectsCacheLifetime is a default lifetime of entries in objects' cache.
|
2021-08-10 11:19:26 +00:00
|
|
|
DefaultObjectsCacheLifetime = time.Minute * 5
|
2022-04-13 16:56:58 +00:00
|
|
|
// DefaultObjectsCacheSize is a default maximum number of entries in objects' cache.
|
2021-08-10 11:19:26 +00:00
|
|
|
DefaultObjectsCacheSize = 1e6
|
|
|
|
)
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// DefaultObjectsConfig returns new default cache expiration values.
|
2022-06-06 08:01:12 +00:00
|
|
|
func DefaultObjectsConfig(logger *zap.Logger) *Config {
|
|
|
|
return &Config{
|
|
|
|
Size: DefaultObjectsCacheSize,
|
|
|
|
Lifetime: DefaultObjectsCacheLifetime,
|
|
|
|
Logger: logger,
|
|
|
|
}
|
2021-09-10 06:56:56 +00:00
|
|
|
}
|
2021-08-10 11:19:26 +00:00
|
|
|
|
|
|
|
// New creates an object of ObjectHeadersCache.
|
2021-09-10 06:56:56 +00:00
|
|
|
func New(config *Config) *ObjectsCache {
|
|
|
|
gc := gcache.New(config.Size).LRU().Expiration(config.Lifetime).Build()
|
2022-06-06 08:01:12 +00:00
|
|
|
return &ObjectsCache{cache: gc, logger: config.Logger}
|
2021-08-10 11:19:26 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 11:30:37 +00:00
|
|
|
// GetObject returns a cached object info.
|
2022-08-08 22:32:18 +00:00
|
|
|
func (o *ObjectsCache) GetObject(address oid.Address) *data.ExtendedObjectInfo {
|
2022-06-28 12:56:41 +00:00
|
|
|
entry, err := o.cache.Get(address)
|
2022-05-24 11:30:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-08 22:32:18 +00:00
|
|
|
result, ok := entry.(*data.ExtendedObjectInfo)
|
2022-05-24 11:30:37 +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-24 09:01:39 +00:00
|
|
|
zap.String("expected", fmt.Sprintf("%T", result)))
|
2022-05-24 11:30:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObject puts an object info to cache.
|
2022-08-08 22:32:18 +00:00
|
|
|
func (o *ObjectsCache) PutObject(obj *data.ExtendedObjectInfo) error {
|
|
|
|
return o.cache.Set(obj.ObjectInfo.Address(), obj)
|
2022-05-24 11:30:37 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 11:19:26 +00:00
|
|
|
// Delete deletes an object from cache.
|
2022-05-25 17:25:43 +00:00
|
|
|
func (o *ObjectsCache) Delete(address oid.Address) bool {
|
2022-06-28 12:56:41 +00:00
|
|
|
return o.cache.Remove(address)
|
2021-08-10 11:19:26 +00:00
|
|
|
}
|