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"
|
2021-11-15 12:56:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
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-04-13 16:56:58 +00:00
|
|
|
// Get returns a cached object.
|
2022-05-25 17:25:43 +00:00
|
|
|
func (o *ObjectsCache) Get(address oid.Address) *object.Object {
|
|
|
|
entry, err := o.cache.Get(address.EncodeToString())
|
2021-08-10 11:19:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result, ok := entry.(object.Object)
|
|
|
|
if !ok {
|
2022-06-06 08:01:12 +00:00
|
|
|
o.logger.Warn("invalid cache entry type", zap.String("actual", fmt.Sprintf("%T", entry)),
|
|
|
|
zap.String("expected", "object.Object"))
|
2021-08-10 11:19:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put puts an object to cache.
|
2021-09-10 06:56:56 +00:00
|
|
|
func (o *ObjectsCache) Put(obj object.Object) error {
|
2022-06-06 08:01:12 +00:00
|
|
|
cnrID, ok := obj.ContainerID()
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("empty container id")
|
|
|
|
}
|
|
|
|
objID, ok := obj.ID()
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("empty object id")
|
|
|
|
}
|
2022-05-25 17:25:43 +00:00
|
|
|
|
|
|
|
var addr oid.Address
|
|
|
|
addr.SetContainer(cnrID)
|
|
|
|
addr.SetObject(objID)
|
|
|
|
|
|
|
|
return o.cache.Set(addr.EncodeToString(), obj)
|
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 {
|
|
|
|
return o.cache.Remove(address.EncodeToString())
|
2021-08-10 11:19:26 +00:00
|
|
|
}
|