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"
|
|
|
|
|
|
|
|
"github.com/bluele/gcache"
|
2022-05-24 11:30:37 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/data"
|
2022-05-25 17:25:43 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
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.
|
|
|
|
func (o *ObjectsCache) GetObject(address oid.Address) *data.ObjectInfo {
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := entry.(*data.ObjectInfo)
|
|
|
|
if !ok {
|
2022-06-24 09:01:39 +00:00
|
|
|
o.logger.Warn("invalid cache entry type", zap.String("actual", fmt.Sprintf("%T", entry)),
|
|
|
|
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.
|
|
|
|
func (o *ObjectsCache) PutObject(obj *data.ObjectInfo) error {
|
2022-06-28 12:56:41 +00:00
|
|
|
return o.cache.Set(obj.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
|
|
|
}
|