frostfs-s3-gw/api/cache/objects.go
Leonard Lyubich 34a221c5c9 [#346] Upgrade NeoFS SDK Go library
Core changes:
  - `object.ID` moved to new package `oid`;
  - `object.Address` moved to new package `address`;
  - `pool.Object` interface changes.

Additionally:
  - Set container owner in `Agent.IssueSecret`.
  - Remove no longer needed fields from `GetObjectParams`
  - `Length` and `Offset` are never assigned. These values
  are set in `Range` field.
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2022-03-04 00:14:30 +03:00

57 lines
1.5 KiB
Go

package cache
import (
"time"
"github.com/bluele/gcache"
"github.com/nspcc-dev/neofs-sdk-go/object"
"github.com/nspcc-dev/neofs-sdk-go/object/address"
)
// ObjectsCache provides lru cache for objects.
type ObjectsCache struct {
cache gcache.Cache
}
const (
// DefaultObjectsCacheLifetime is a default lifetime of entries in objects' cache.
DefaultObjectsCacheLifetime = time.Minute * 5
// DefaultObjectsCacheSize is a default maximum number of entries in objects' in cache.
DefaultObjectsCacheSize = 1e6
)
// DefaultObjectsConfig return new default cache expiration values.
func DefaultObjectsConfig() *Config {
return &Config{Size: DefaultObjectsCacheSize, Lifetime: DefaultObjectsCacheLifetime}
}
// New creates an object of ObjectHeadersCache.
func New(config *Config) *ObjectsCache {
gc := gcache.New(config.Size).LRU().Expiration(config.Lifetime).Build()
return &ObjectsCache{cache: gc}
}
// Get returns cached object.
func (o *ObjectsCache) Get(address *address.Address) *object.Object {
entry, err := o.cache.Get(address.String())
if err != nil {
return nil
}
result, ok := entry.(object.Object)
if !ok {
return nil
}
return &result
}
// Put puts an object to cache.
func (o *ObjectsCache) Put(obj object.Object) error {
return o.cache.Set(obj.ContainerID().String()+"/"+obj.ID().String(), obj)
}
// Delete deletes an object from cache.
func (o *ObjectsCache) Delete(address *address.Address) bool {
return o.cache.Remove(address.String())
}