frostfs-s3-gw/api/cache/objects.go
Angira Kekteeva 1bc2e51cbc [#236] api: Refactor caches: ObjectsList, Objects
Move ObjectsList from layer to cache package
Rename object_cache.go to objects.go

Signed-off-by: Angira Kekteeva <kira@nspcc.ru>
2021-09-07 11:08:48 +03:00

62 lines
1.6 KiB
Go

package cache
import (
"time"
"github.com/bluele/gcache"
"github.com/nspcc-dev/neofs-api-go/pkg/object"
)
// ObjectsCache provides interface for lru cache for objects.
type ObjectsCache interface {
Get(address *object.Address) *object.Object
Put(obj object.Object) error
Delete(address *object.Address) bool
}
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
)
type (
// ObjectHeadersCache contains cache with objects and lifetime of cache entries.
ObjectHeadersCache struct {
cache gcache.Cache
lifetime time.Duration
}
)
// New creates an object of ObjectHeadersCache.
func New(cacheSize int, lifetime time.Duration) *ObjectHeadersCache {
gc := gcache.New(cacheSize).LRU().Build()
return &ObjectHeadersCache{cache: gc, lifetime: lifetime}
}
// Get returns cached object.
func (o *ObjectHeadersCache) Get(address *object.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 *ObjectHeadersCache) Put(obj object.Object) error {
return o.cache.SetWithExpire(obj.ContainerID().String()+"/"+obj.ID().String(), obj, o.lifetime)
}
// Delete deletes an object from cache.
func (o *ObjectHeadersCache) Delete(address *object.Address) bool {
return o.cache.Remove(address.String())
}